天天看点

spring mvc 接收表单 bean

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

页面logininput.jsp:

spring mvc 接收表单 bean

<?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>  

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

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

                    <td colspan="2">  

                    <input type="submit"  value="login"/>  

                    </td>  

            </table>  

        </form>  

    </center>  

</body>  

</html>  

 控制器logincontroller 中登录的方法:

spring mvc 接收表单 bean

/*** 

     * 校验登录用户 

     *  

     * @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", "身份证和用户编号有误,请重新登录");  

    }  

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

实体类user:

spring mvc 接收表单 bean

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) {  

        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) {  

    public string getstudentid() {  

        return studentid;  

    public void setstudentid(string 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>

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

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

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