Sunday, January 19, 2014

Spring Course: V65 - V75 Working with Web Forms

V65: Creating a Form

Nothing in this video, we just added a jsp page with a form inside:


V66: Getting Form Value

here we will do a mapping between the form fields and the Offer class, this mapping will give us an offer object directly:

1- The Offer class looks like this:

as you can see the name of the fields are name, email and text.

2- we should give the form fields the same names

3- now in the action doCreate

as you can see we pass Offer to the docreate, spring will do the mapping for us

V67 & V68 Adding Resources
here we will add a resource folder, inside this folder we will put all our css and js files, after adding this folder we will tell spring that the resource folder is here:

1- add resource folder in Webcontent

2- now we have to tell spring that we have resources folder here
as you can see we added <mvc:resources> we provide the location, the mapping is the url we will using, as you can see we used "/static/**" double start which means looks for all the folders inside resouces, so now you can bring any resources using this url for example:
localhost:8084/spring/static/css/main.css

V69 Adding Hibernate Form Validation Support

here we will talk about an easy way to validate the form data:
1- use BindingResult


as you can see here we added BindingResult as input to the doCreate method, and we added @Valid to the Offer class, you can use result.hasErrors() to check if there is any errors.

2- you need some jars for @Valid to work, add validation-api.jar to the pom.xml. Also you need to add org.hibernate.jar and hibernate-validator.jar for BindingResult to work

3- now you add the validation you want in Offer class


V70 More Form Validation Tags

in this tutorial we introduce more validation tags to the Offer class


V71: Making Form Remember Values

Currently, if there is any form validation error, the form fields value will be deleted, in this tutorial we will show how we can make spring remember the values:

1- the first thing we should use spring forms tag lib


as you can see we added a taglib, and we used the sf:form and sf:input, sf:textarea.
in addition we use the path attribute, actually the path attribute is the offer property name
and also we add a commandName="offer" this is something that should be passed to createOffer.jsp
we pass this value like this

2- in OfferController
 the createOffer takes us to the createoffer.jsp page, here we should add the commandName which is offer, as you can see we use model.addAttribute for that

and now the form will be able to remember the values


V72: Displaying Form Validations Errors
here we will a new tag to the previous form to show errors:


here as you can see we use sf:errors tag

V73 Creating Custom Validation Errors
you can create your custom validation like @Size or @Pattern
you can watch the video if you want to check the steps, what he did is he opened the source file of one of the validator (e.g @Size) and copied the code.

V74 Hooking UP The Controller and Database Code:
noting new here, he created a create function to create a record in the database, and he called that function

V75 Exception Handling in Spring MVC
lets say that we wanna handle exceptions in our controller, so in case of exceptions we wanna redirect the user to error page

you can for example handle all database exception that might happen in the controller by adding this

as you can see we added a method with  @ExceptionHandler annotation, and the type of exception that should handle, the method return "error" which is the error.jsp page

now this exception handler will work only for this controller, however you can define a global handler for all controllers by adding a class like this


as you can see we annotaed the class @ControllerAdvice,


No comments:

Post a Comment