spring mvc 接收表单 bean

spring MVC如何接收表单bean 呢?

之前项目中MVC框架一直用struts2,所以我也就按照struts2 的思维来思考

页面loginInput.jsp:

Html代码   收藏代码spinner.gif
  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>  
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
    <center>
        <font color="red" >${message }</font>

        <form action="<%=path %>/user/loginVerify">
            <table>

                <tr>
                    <td>身份证:</td>
                    <td> <input type="text" name="user.identity"  /> </td>
                </tr>
                <tr>
                    <td>用户编号:</td>
                    <td><input type="text" name="user.studentID"  /> </td>
                </tr>
                <tr>
                    <td colspan="2">
                    <input type="submit"  value="login"/>
                    </td>
                </tr>
            </table>
        </form>
        
    </center>
    
</body>
</html>

 控制器LoginController 中登录的方法:

Java代码   收藏代码spinner.gif
  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.     }  
/***
     * 校验登录用户
     * 
     * @param session
     * @param user
     * @return
     * @throws UnsupportedEncodingException
     * @throws Exception
     */
    @RequestMapping(value = "/loginVerify")
    public String login(User user, HttpSession session,
            Map<String, Object> map,Model model) throws UnsupportedEncodingException,
            Exception {
        User user2 = null;
        if (user.getIdentity() == null) {
            map.put("message", "请输入身份证");
            return "loginInput";
        }
        map.put("identity", user.getIdentity());
        model.addAttribute("identity", user.getIdentity());
        System.out.println("identity:"+session.getAttribute("identity"));
        user2 = this.userDao.getByIdentityAndStudentID(new User(user.getIdentity(),
                user.getStudentID()));
        System.out.println("user2:" + user2);
        if (user2 != null) {
            return "welcome";
        } else {
            map.put("message", "身份证和用户编号有误,请重新登录");
            return "loginInput";
        }

    }

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

实体类User

Java代码   收藏代码spinner.gif
  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. }  
package com.springmvc.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/***
 * father class
 * @author huangwei
 *
 */
@Entity
public  class User {
    private int id;
    /**
     * 身份证
     */
    private String identity;
    /***
     * 用户编号
     */
    private String studentID;
    private String username;
    
    public User() {
        super();
    }

    public User(String identity, String studentID) {
        super();
        this.identity = identity;
        this.studentID = studentID;
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getIdentity() {
        return identity;
    }

    public void setIdentity(String identity) {
        this.identity = identity;
    }

    public String getStudentID() {
        return studentID;
    }
    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
}

 

原来,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不同的地方!

转载于:https://www.cnblogs.com/jpfss/p/8986470.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值