天天看點

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