日期:2014-05-17  浏览次数:20796 次

初学者Struts2 无法获取页面的值 求答案
文件如下
LoginActionForm.java
Java code

import com.opensymphony.xwork2.ActionSupport;
import com.lee.user.user.*;

public class LoginActionForm extends ActionSupport{
    User us = new User();
    public String execute(){
        System.out.println("username:"+us.getUserName());
        System.out.println("password:"+us.getPassWord());
        return SUCCESS;
    }

}


User.java
Java code

public class User {
    private String UserName;
    private String PassWord;
    
    
    /**
     * @return the userName
     */
    public String getUserName() {
        return UserName;
    }
    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        UserName = userName;
    }
    /**
     * @return the passWord
     */
    public String getPassWord() {
        return PassWord;
    }
    /**
     * @param passWord the passWord to set
     */
    public void setPassWord(String passWord) {
        PassWord = passWord;
    }

}


login.jsp
Java code

<s:form action="login" method="post">
   <s:textfield name="UserName" label="用户名"></s:textfield>
   <s:password name="PassWord" label="密码"></s:password>
   <s:submit label="登陆" value="登陆"></s:submit>
  </s:form>


struts.xml
Java code

<package name="default" namespace="/" extends="struts-default">
     <action name="loginindex">
         <result>login.jsp</result>
     </action>
     <action name="login" class="com.lee.user.form.LoginActionForm">
         <result name="success">wellcom.jsp</result>
         <result name="error">error.jsp</result>
     </action>
     
     <action name="goback">
         <result name="success">login.jsp</result>
     </action>
     
     </package>


web.xml
Java code

 <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>




为什么在action中定义页面属性就能获取到值,但是像这样分开后就拿不到值了呢?求解.

------解决方案--------------------
public class LoginActionForm extends ActionSupport{
User us = new User();//不需要new User 为us生成getter和setter方法
public String execute(){
System.out.println("username:"+us.getUserName());
System.out.println("password:"+us.getPassWord());
return SUCCESS;
}

}

<s:form action="login" method="post">
<s:textfield name="UserName" label="用户名"></s:textfield>// name=us.UserName
<s:p