Harshit
Harshit

Reputation: 5157

Spring MVC Display Date variable

I am trying to run a project in Spring MVC. Here is the code

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/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=UTF-8">
        <title>Welcome to Spring Web MVC project</title>
    </head>

    <body>
        <h1>Spring 3 Register!</h1>
        <a href="register.htm">click</a>
        <form:form action="${pageContext.request.contextPath}/register" method="POST" modelAttribute="userForm">
            <table>
                <tr>
                    <td colspan="2" align="center">Spring MVC Form Demo - Registration</td>
                </tr>
                <tr>
                    <td>User Name</td>
                    <td><form:input path="username" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><form:password path="password" /></td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td><form:input path="email" /></td>
                </tr>
                <tr>
                    <td>BirthDate (mm/dd/yyyy)</td>
                    <td><form:input path="birthDate" /></td>
                </tr>
                <tr>
                    <td>Profession</td>
                    <td><form:select path="profession" items="${professionList}" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="Register" /></td>
                </tr>

            </table>
        </form:form>
    </body>
</html>

RegistrationController.java

package RegisterInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Harshit Shrivastava
 */
import RegisterInfo.model.User;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;

@Controller
@RequestMapping(value = "/register")
public class RegistrationController {

    @RequestMapping(method = RequestMethod.GET)
    public String viewRegistration(Model model)
    {
        User userForm = new User();
                model.addAttribute("userForm", new User());

                List<String> professionList = new ArrayList();
                professionList.add("Developer");
                professionList.add("Designer");
                professionList.add("IT Manager");
                model.put("professionList", professionList);
        return "index";
    }
    @InitBinder
    public void initBinder(WebDataBinder binder) {
         SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
         sdf.setLenient(true);
         binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView processRegistration(@ModelAttribute("userForm") User user, Map<String, Object> model)
    {       
        model.addAttribute("username", user.getUserName());
        model.addAttribute("password", user.getPassword());
        model.addAttribute("birthDate", user.getBirthDate());
        model.addAttribute("email", user.getEmail());
        model.addAttribute("profession", user.getProfession());                 
            return new ModelAndView("RegisterSuccess","userForm",new User());
    }
}

User.java

package RegisterInfo.model;

import java.util.Date;

public class User {
    private String username;
    private String password;
    private String email;
    private Date birthDate;
    private String profession;

    public String getUserName()
    {
        return username;
    }
    public void setUserName(String username)
    {
        this.username = username;
    }
        public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
        public String getEmail()
    {
        return email;
    }
    public void setEmail(String email)
    {
        this.email = email;
    }
        public Date getBirthDate()
    {
        return birthDate;
    }
    public void setBirthDate(Date birthDate)
    {
        this.birthDate = birthDate;
    }
        public String getProfession()
    {
        return profession;
    }
    public void setProfession(String profession)
    {
        this.profession = profession;
    }
}

RegisterSuccess.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/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=UTF-8">
        <title>Welcome to Spring Web MVC project</title>
    </head>

    <body>
        <h1>Register Success!</h1>
            <table>
                <tr>
                    <td>User Name</td>
                    <td>${username}</td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td>${password}</td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td>${email}</td>
                </tr>
                <tr>
                    <td>BirthDate (mm/dd/yyyy)</td>
                    <td>${birthDate}</td>
                </tr>
                <tr>
                    <td>Profession</td>
                    <td>${profession}</td>
                </tr>
            </table>
    </body>
</html>

In the above program, I am able to print all the fields but not date i.e. BirthDate. Why it is not getting printed?

Upvotes: 0

Views: 1366

Answers (1)

Rozart
Rozart

Reputation: 1778

You forgot to add the model, that you filled with attributes, to newly created ModelAndView that you return in processRegistration method. You got NullPointerException when rendering your view, because there were no attributes to display in the Model.

Here's the fixed method:

@RequestMapping(method = RequestMethod.POST)
public ModelAndView processRegistration(@ModelAttribute("userForm") User user, Map<String, Object> model)
{       
    model.addAttribute("username", user.getUserName());
    model.addAttribute("password", user.getPassword());
    model.addAttribute("birthDate", user.getBirthDate());
    model.addAttribute("email", user.getEmail());
    model.addAttribute("profession", user.getProfession());
    model.addAttribute("userForm", new User());                 
    return new ModelAndView("RegisterSuccess",model);
}

Additionally if NullPointerException still occurs you can do such thing in your RegisterSuccess.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=UTF-8">
    <title>Welcome to Spring Web MVC project</title>
</head>

<body>
<h1>Register Success!</h1>
<table>
    <tr>
        <td>User Name</td>
        <td><c:if test="${not empty username}"> ${username}</c:if></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><c:if test="${not empty password}">${password}</c:if></td>
    </tr>
    <tr>
        <td>Email</td>
        <td><c:if test="${not empty email}">${email}</c:if></td>
    </tr>
    <tr>
        <td>BirthDate (mm/dd/yyyy)</td>
        <td><c:if test="${not empty birthDate}">${birthDate}</c:if></td>
    </tr>
    <tr>
        <td>Profession</td>
        <td><c:if test="${not empty profession}">${profession}</c:if></td>
    </tr>
</table>
</body>
</html>

If you see that one of the parameters is not displayed, then it means that it has null value.

Upvotes: 1

Related Questions