Wednesday, January 7, 2015

Validation

Every web site should have validation for client and server site. Simple validation by client site I describe in previous post, here I'll create server validation. At first I added to pom.xml file dependency:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.1.Final</version>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>

Next in domain file adding annotation to the fields in object like:
- @NotNull
- @Size
- @MaxSize
- @MinSize
- @Unique
- @Email

All of the validations used above are part of the JSR-303 API, except for “NotEmpty” and “Email”, which are Hibernate-Validator annotations.
Next in controller file adding to method @Valid annotation and BindingResult

Simply adding annotation @Valid tells Spring to validate the “Person” object.  I also add a BindingResult argument. BindingREsult is Spring’s object which holds the result of the validation, binding and contains errors that may have occurred. The BindingResult must come right after the model object that is validated or else Spring will fail to validate the object and throw an exception.

When Spring sees @Valid, it tries to find the validator for the object being validated. Spring automatically picks up validation annotations if you have “annotation-driven” enabled. Spring then invokes the validator and puts any errors in the BindingResult and adds the BindingResult to the view model.

Monday, January 5, 2015

Create user - registration

Today I' describe how to create registration form and controller to manage it. My view createPerson.jsp it's simple form with field represent field in Person table in database and looks:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
      pageEncoding="ISO-8859-1"%>  
 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Create person</title>  
 </head>  
 <body>  
      <div align="top">  
           <form:form commandName="person" action="createPerson" method="POST">  
                <table border="0">  
                     <tr>  
                          <td align="center"><h2>Registration</h2></td>  
                     </tr>  
                     <div align="left">  
                     <tr>  
                          <td>User Name:</td>  
                          <td><form:input path="firstName" tabindex="1" required="true" />  
                               <form:errors path="firstName" /></td>  
                     </tr>  
                     <br />  
                     <tr>  
                          <td>Last Name:</td>  
                          <td><form:input path="lastName" tabindex="2" required="true" />  
                               <form:errors path="lastName" /></td>  
                     </tr>  
                     <br />  
                     <tr>  
                          <td>Email:</td>  
                          <td><form:input path="email" tabindex="3" required="true" />  
                               <form:errors path="email" /></td>  
                     </tr>  
                     <br />  
                     <tr>  
                          <td>Password:</td>  
                          <td><form:input path="password" value="user" tabindex="4" required="true" />  
                                                        <form:errors path="password" /></td>  
                     </tr>  
                     <br />  
                     <tr>  
                          <td>Role:</td>  
                          <td><form:input path="role" tabindex="5" required="true" />  
                               <form:errors path="role" /></td>  
                     </tr>  
                     <tr>  
                          <td>Telephone:</td>  
                          <td><form:input path="telephone" tabindex="6" required="true" />  
                               <form:errors path="telephone" /></td>  
                     </tr>  
                     <br />  
                     <tr>  
                          <td>Street:</td>  
                          <td><form:input path="street" tabindex="7" required="true" />  
                               <form:errors path="street" /></td>  
                     </tr>  
                     <br />  
                     <tr>  
                          <td>City:</td>  
                          <td><form:input path="city" tabindex="8" required="true" /> <form:errors  
                                    path="city" /></td>  
                     </tr>  
                     <br />  
                     <tr>  
                          <td>Zipcode:</td>  
                          <td><form:input path="zipcode" tabindex="9" required="true" />  
                               <form:errors path="zipcode" /></td>  
                     </tr>  
                     <br />  
                     <tr>  
                          <td colspan="2" align="center"><input type="submit"     value="createPerson" /></td>  
                     </tr>  
                     </div>  
                </table>  
           </form:form>  
      </div>  
 </body>  
 </html>  

At the top there is define form, action and what method use: <form:form commandName="person" action="createPerson" method="POST">  .  Each of field have tag <tabindex=1>  twhich allows move the cursor between fields by Tab key. Every field also have simple validation by client side.

In PersonController there are autowired method defined in PersonDAO file, so we have method for create new user which get data from the jsp file and put them to model. Source code looks:

package com.pizza.delivery.controllers;  
 import java.util.Map;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.stereotype.Controller;  
 import org.springframework.ui.Model;  
 import org.springframework.web.bind.annotation.ModelAttribute;  
 import org.springframework.web.bind.annotation.RequestMapping;  
 import org.springframework.web.bind.annotation.RequestMethod;  
 import org.springframework.web.servlet.ModelAndView;  
 import com.pizza.delivery.domain.Person;  
 import com.pizza.delivery.services.PersonDAO;  
 @Controller  
 @RequestMapping(value = "/createPerson")  
 public class PersonController {  
      @Autowired  
      private PersonDAO<Person> personDAO;  
      @RequestMapping( method = RequestMethod.GET )  
      public String createPerson(Model model){  
      Person person = new Person();  
      model.addAttribute("person", person);  
      return "createPerson";  
      }  
      @RequestMapping( method = RequestMethod.POST)  
      public ModelAndView saveNewPerson(@ModelAttribute("userForm") Person person, Map<String, Object> model){  
           personDAO.create(person);  
           String message = null;  
           return new ModelAndView("home", "message", message);  
      }  
 }