当前位置: 代码迷 >> J2EE >> 使用struts+spring+hibernate时,action没法取得前台输入的值
  详细解决方案

使用struts+spring+hibernate时,action没法取得前台输入的值

热度:87   发布时间:2016-04-17 23:11:36.0
使用struts+spring+hibernate时,action无法取得前台输入的值
本帖最后由 mycstc 于 2015-05-20 21:35:54 编辑
求大神帮忙看看。密码能正确的传值,而用户名一直是null,而且把<s:password>也就是密码框这一句给删了之后用户名却能正确的传值了
login.jsp  

<s:form action="login.action" method="post">
        <s:textfield name="users.usrname"  label="用户名" size="21"/>
        <s:password name="users.usrpass" label="密码" size="21"/>
        <s:submit value="登陆"/>
    </s:form>

UserAction.java

public class UserAction extends ActionSupport{
    private UserService userService;
    private Users users;
    private Users getUsers(){
        return users;
    }
    public void setUsers(Users users){
        this.users=users;
    }
    public UserService getUserService(){
        return userService;
    }
    public void setUserService(UserService userService){
        this.userService=userService;
    }
    public String execute() throws Exception{
        System.out.println("用户名:"+users.getUsrname());//这里显示获得的usrname值是空的,但是usrpass值是对的,而且删了jsp里的usrpass输入框之后usrname就能正确传值
        Users yh =userService.find(users.getUsrname(),users.getUsrpass());
        if(yh!=null){
            Map session=(Map)ActionContext.getContext().getSession();
            session.put("users", yh);
            return SUCCESS;
        }
        else{
            System.out.print("wocao");
            return ERROR;
        }
            
    }

}


Users.java

public class Users implements java.io.Serializable {

// Fields

private String usrname;
private String usrpass;
private Userinfo userinfo;

// Constructors

/** default constructor */
public Users() {
}

/** full constructor */
public Users(String usrname, String usrpass) {
this.usrname = usrname;
this.usrpass = usrpass;
}

// Property accessors

public String getUsrname() {
return this.usrname;
}

public void setUsrname(String usrname) {
this.usrname = usrname;
}

public String getUsrpass() {
return this.usrpass;
}

public void setUsrpass(String usrpass) {
this.usrpass = usrpass;
}
public Userinfo getUserinfo(){
return this.userinfo;
}
public void setUserinfo(Userinfo userinfo){
this.userinfo=userinfo;
}

}

Users.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.model.Users" table="USERS" schema="dbo" catalog="KFGL">
        <id name="usrname" type="java.lang.String">
            <column name="usrname" length="10" />
            <generator class="foreign">
             <param name="property">userinfo</param>
            </generator>
        </id>
        <property name="usrpass" type="java.lang.String">
            <column name="usrpass" length="10" not-null="true" />
        </property>
        <one-to-one name="userinfo" class="org.model.Userinfo" constrained="true"></one-to-one>
        
    </class>
</hibernate-mapping>

------解决思路----------------------
引用:
Quote: 引用:

是不是在传递password时setUsers方法把name清空了?为什么不在action中分开接收name和pass呢。

在action中分开接收name和pass是什么意思呢?在Users yh =userService.find(users.getUsrname(),users.getUsrpass());
这一句之前,我用System.out.println("用户名:"+users.getUsrname());显示取得的name,但是这里也是显示null

看起来像参数没获取过来,我只是觉得两个参数同时存储会不会互相影响,分开操作会不会成功,像这样:
public class UserAction extends ActionSupport{
    private UserService userService;
    private String username;
    private String password;
   
    //username, password 的getter, setter

    public UserService getUserService(){
        return userService;
    }
    public void setUserService(UserService userService){
        this.userService=userService;
    }
    public String execute() throws Exception{
        System.out.println("用户名:"+username);
        Users yh =userService.find(username,password);
        if(yh!=null){
            Map session=(Map)ActionContext.getContext().getSession();
            session.put("users", yh);
            return SUCCESS;
        }
        else{
            System.out.print("wocao");
            return ERROR;
        }
             
    }
 
}
  相关解决方案