zoukankan      html  css  js  c++  java
  • 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.     }  
  • 相关阅读:
    AtCoder Beginner Contest 205
    Codeforces Round #725 (Div. 3)
    Educational Codeforces Round 110 (Rated for Div. 2)【A
    Codeforces Round #722 (Div. 2)
    AtCoder Beginner Contest 203(Sponsored by Panasonic)
    AISing Programming Contest 2021(AtCoder Beginner Contest 202)
    PTA 520 钻石争霸赛 2021
    Educational Codeforces Round 109 (Rated for Div. 2)【ABCD】
    AtCoder Beginner Contest 200 E
    Educational Codeforces Round 108 (Rated for Div. 2)【ABCD】
  • 原文地址:https://www.cnblogs.com/jpfss/p/8986470.html
Copyright © 2011-2022 走看看