码迷,mamicode.com
首页 > 编程语言 > 详细

spring mvc 接收表单 bean

时间:2018-05-03 19:36:38      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:mode   word   tst   control   page   public   www   splay   verify   

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.     }  

 我认为页面表单中name为user.identity 和user.studentID的元素会自动注入到上述方法的变量User user 中,结果没有!!!?

实体类User

Java代码  技术分享图片
  1. package com.springmvc.entity;  
  2.   
  3. import javax.persistence.Entity;  
  4. import javax.persistence.GeneratedValue;  
  5. import javax.persistence.Id;  
  6.   
  7. /*** 
  8.  * father class 
  9.  * @author huangwei 
  10.  * 
  11.  */  
  12. @Entity  
  13. public  class User {  
  14.     private int id;  
  15.     /** 
  16.      * 身份证 
  17.      */  
  18.     private String identity;  
  19.     /*** 
  20.      * 用户编号 
  21.      */  
  22.     private String studentID;  
  23.     private String username;  
  24.       
  25.     public User() {  
  26.         super();  
  27.     }  
  28.   
  29.     public User(String identity, String studentID) {  
  30.         super();  
  31.         this.identity = identity;  
  32.         this.studentID = studentID;  
  33.     }  
  34.   
  35.     @Id  
  36.     @GeneratedValue  
  37.     public int getId() {  
  38.         return id;  
  39.     }  
  40.     public void setId(int id) {  
  41.         this.id = id;  
  42.     }  
  43.     public String getIdentity() {  
  44.         return identity;  
  45.     }  
  46.   
  47.     public void setIdentity(String identity) {  
  48.         this.identity = identity;  
  49.     }  
  50.   
  51.     public String getStudentID() {  
  52.         return studentID;  
  53.     }  
  54.     public void setStudentID(String studentID) {  
  55.         this.studentID = studentID;  
  56.     }  
  57.   
  58.     public String getUsername() {  
  59.         return username;  
  60.     }  
  61.   
  62.     public void setUsername(String username) {  
  63.         this.username = username;  
  64.     }  
  65.       
  66. }  

 

原来,spring MVC 跟struts2的注入方式不一样!!

后来我把页面中的name属性改为identity 和studentID 就好了:

<tr>

<td>身份证:</td>

<td> <input type="text" name="identity"  /> </td>

</tr>

<tr>

<td>用户编号:</td>

<td><input type="text" name="studentID"  /> </td>

</tr>

 

这就是spring MVC与struts2 ioc不同的地方!

spring mvc 接收表单 bean

标签:mode   word   tst   control   page   public   www   splay   verify   

原文地址:https://www.cnblogs.com/jpfss/p/8986470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!