Spring MVC HTTP状态400 – 错误请求
我试图在Spring MVC中运行一个项目。 这是代码
的index.jsp
Welcome to Spring Web MVC project Spring 3 Register!
click
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 professionList = new ArrayList(); professionList.add("Developer"); professionList.add("Designer"); professionList.add("IT Manager"); model.put("professionList", professionList); return "index"; } @RequestMapping(method = RequestMethod.POST) public String processRegistration(@ModelAttribute("userForm") User user, Map model) { System.out.println("Username : " + user.getUserName()); model.put("userForm", new User()); return "index"; } }
User.java
package RegisterInfo.model; /** * * @author Harshit Shrivastava */ 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; } }
调度员servlet.xml中
indexController
在上面的程序中,每当我提交表单时,我总是会收到此错误
错误:
HTTP Status 400 - Bad Request The request sent by the client was syntactically incorrect
代码有什么问题?
您必须在提交HTTP POST时绑定Date
。 Spring不知道这是一个Date
,它将它视为一个String
。
添加这个:
@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); sdf.setLenient(true); binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true)); }
给你的控制器。