spring mvc 接收表單 bean


spring MVC如何接收表單bean 呢?

之前項目中MVC框架一直用struts2,所以我也就按照struts2 的思維來思考

頁面loginInput.jsp:

Html代碼   收藏代碼
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  3.     pageEncoding="UTF-8"%>  
  4. <%  
  5.     String path = request.getContextPath();  
  6.     String basePath = request.getScheme() + "://"  
  7.             + request.getServerName() + ":" + request.getServerPort()  
  8.             + path + "/";  
  9. %>  
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  11. <html xmlns="http://www.w3.org/1999/xhtml">  
  12. <head>  
  13. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  14. <title>Insert title here</title>  
  15. </head>  
  16. <body>  
  17.     <center>  
  18.         <font color="red" >${message }</font>  
  19.   
  20.         <form action="<%=path %>/user/loginVerify">  
  21.             <table>  
  22.   
  23.                 <tr>  
  24.                     <td>身份證:</td>  
  25.                     <td> <input type="text" name="user.identity"  /> </td>  
  26.                 </tr>  
  27.                 <tr>  
  28.                     <td>用戶編號:</td>  
  29.                     <td><input type="text" name="user.studentID"  /> </td>  
  30.                 </tr>  
  31.                 <tr>  
  32.                     <td colspan="2">  
  33.                     <input type="submit"  value="login"/>  
  34.                     </td>  
  35.                 </tr>  
  36.             </table>  
  37.         </form>  
  38.           
  39.     </center>  
  40.       
  41. </body>  
  42. </html>  

 控制器LoginController 中登錄的方法:

Java代碼   收藏代碼
  1. /*** 
  2.      * 校驗登錄用戶 
  3.      *  
  4.      * @param session 
  5.      * @param user 
  6.      * @return 
  7.      * @throws UnsupportedEncodingException 
  8.      * @throws Exception 
  9.      */  
  10.     @RequestMapping(value = "/loginVerify")  
  11.     public String login(User user, HttpSession session,  
  12.             Map<String, Object> map,Model model) throws UnsupportedEncodingException,  
  13.             Exception {  
  14.         User user2 = null;  
  15.         if (user.getIdentity() == null) {  
  16.             map.put("message""請輸入身份證");  
  17.             return "loginInput";  
  18.         }  
  19.         map.put("identity", user.getIdentity());  
  20.         model.addAttribute("identity", user.getIdentity());  
  21.         System.out.println("identity:"+session.getAttribute("identity"));  
  22.         user2 = this.userDao.getByIdentityAndStudentID(new User(user.getIdentity(),  
  23.                 user.getStudentID()));  
  24.         System.out.println("user2:" + user2);  
  25.         if (user2 != null) {  
  26.             return "welcome";  
  27.         } else {  
  28.             map.put("message""身份證和用戶編號有誤,請重新登錄");  
  29.             return "loginInput";  
  30.         }  
  31.   
  32.     }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM