输入地址http://localhost:8080/Contact/contact/login_form.do,跳到登陆界面
1 @RequestMapping(value = "/login_form.do") 2 public String getLoginForm(Model model) { 3 logger.info("getRegForm method invoked"); 4 model.addAttribute("contact", new Contact()); 5 return "loginform"; 6 }
输入账号密码进行登陆,可成功登陆
1 @RequestMapping(value = "/login.do") 2 public String login(@ModelAttribute Contact contact, Model model) { 3 logger.info("login method invoked and receive the param :" + JSON.toJSONString(contact)); 4 contact = contactService.login(contact.getName(), contact.getPassword()); 5 logger.info(JSON.toJSONStringWithDateFormat(contact, "yyyy-MM-dd")); 6 model.addAttribute("contact", contact); 7 return "personalMsg"; 8 }
一下是登陆后跳转到的personalMsg.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 personalMsg 11 <br> 12 13 <br> 14 15 <a href="update_form/${contact.id }.do">${contact.name }</a> 16 17 </body> 18 </html>
点击a标签,调用修改表单,这里也没有问题
1 @RequestMapping(value = "/update_form/{id}") 2 public String getUpdateForm(@PathVariable int id, Model model) { 3 logger.info("getContact method invoked and receive param :"); 4 Contact contact = contactService.getContactById(id); 5 logger.info("contact:" + JSON.toJSONStringWithDateFormat(contact, "yyyy-MM-dd")); 6 model.addAttribute("contact", contact); 7 return "updateform"; 8 }
更改完数据,点击修改调用,这里开始报错
1 @RequestMapping(value = "/update") 2 public String update(@ModelAttribute @Valid Contact contact, BindingResult bindingResult, Model model) { 3 logger.info("update method invoked and receive the param :" 4 + JSON.toJSONStringWithDateFormat(contact, "yyyy-MM-dd")); 5 if (bindingResult.hasErrors()) { 6 return "updateform"; 7 } 8 contact = contactService.getContactById(contact.getId()); 9 model.addAttribute("contact",contact); 10 return "personalMsg"; 11 }
1 [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver]Resolving exception from handler [public java.lang.String com.cmbc.controller.ContactController.getUpdateForm(int,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "update" 2 [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver]Resolving exception from handler [public java.lang.String com.cmbc.controller.ContactController.getUpdateForm(int,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "update" 3 [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver]Resolving exception from handler [public java.lang.String com.cmbc.controller.ContactController.getUpdateForm(int,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "update" 4 [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver]Resolving exception from handler [public java.lang.String com.cmbc.controller.ContactController.getUpdateForm(int,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "update" 5 [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver]Resolving exception from handler [public java.lang.String com.cmbc.controller.ContactController.getUpdateForm(int,org.springframework.ui.Model)]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "update" 6 [org.springframework.web.servlet.DispatcherServlet]Null ModelAndView returned to DispatcherServlet with name 'springMvc': assuming HandlerAdapter completed request handling
同样问题,实体类对应和前台输入的类型是一致的都是String,报错TypeMismatchException,解释到无法转换成Long类型
– no_delay_su 4年前