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);  
      }  
 }  










No comments:

Post a Comment