当前位置: 代码迷 >> Java Web开发 >> 菜鸟Struts2 无法获取页面的值 求答案
  详细解决方案

菜鸟Struts2 无法获取页面的值 求答案

热度:4862   发布时间:2013-02-25 21:10:00
初学者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:password name="PassWord" label="密码"></s:password> // name=us.PassWord
  相关解决方案