我正在尝试在Spring MVC中运行一个项目。这是代码
的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>
在上面的程序中,我可以打印所有字段但不能打印日期,即BirthDate。为什么不打印?
答案 0 :(得分:1)
您忘记将已填充属性的model
添加到以ModelAndView
方法返回的新创建的processRegistration
。您在渲染视图时获得了NullPointerException
,因为Model
中没有要显示的属性。
这是固定方法:
@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);
}
此外,如果仍然发生NullPointerException,您可以在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>
如果您看到其中一个参数未显示,则表示它具有空值。