没有在Action中获取表单数据

时间:2013-09-09 10:40:07

标签: jsp struts2 web.xml struts-config

我是struts 2的新手,我正在做的是在struts 2中创建一个表单并在其他页面上显示输入的值。但是当我在step1.jsp页面上提交值时,它没有显示在profileuplaoded上的.jsp

以下是我输入值step1.jsp

的jsp页面
<%@ taglib uri="/struts-tags" prefix="s" %>
<h2>User Profile upload form</h2>

<s:form action="upload" method="post">
<s:textfield name="fname" label="First Name"></s:textfield>
<s:textfield name="lname" label="Last Name"></s:textfield>
<!-- <s:radio name="mstatus" label="Martial Status" list="{'Single','Married'}">
</s:radio><s:radio name="gender" label="Gender" list="{'male','female'}"></s:radio>-->
<s:submit value="upload profile" align="center"></s:submit>
</s:form>

这是我的另一个jsp,我想在其中显示输入的值profileuploaded.jsp

<%@ taglib uri="/struts-tags" prefix="p" %>
<h2>Profile uploaded sucessfully</h2>
First Name:<p:property value="fname"/><br/>
Last Name:<p:property value="lname"/><br/>
Martial status:<p:property value="mstatus"/><br/>
Gender:<p:property value="gender"/><br/>

这是我的java文件uploadprofile.java

package com.javapoint;

import com.opensymphony.xwork2.ActionSupport;



public class UploadProfile extends ActionSupport {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String fname;
private String lname;
//private String mstatus;
//private String gender;

public String getfname() {
    System.out.println("inside getfname");
    return fname;
}
public void setfname(String fname) {
    System.out.println("inside getfname");
    System.out.println("the firstname set is "+fname);
    this.fname = fname;
}
public String getlname() {

    return lname;
}
public void setlname(String lname) {
    this.lname = "lname";
    System.out.println("the lastname set is "+ lname);
}
/*public String getMstatus() {
    return mstatus;
}
public void setMstatus(String mstatus) {
    this.mstatus = mstatus;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}*/
public String execute()throws Exception{
    System.out.println("inside execute");
    if(fname!=null){
    return "profileuploaded";
    }else{
        return "error";

    }
}
}

这是我的struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
 Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 <struts>
   <package name="default" extends="struts-default">

<action name="product" class="com.javapoint.Product">
<result name="success">welcome.jsp</result>
</action>
<action name="upload" class="com.javapoint.UploadProfile">
<result name="profileuploaded">ProfileUploaded.jsp</result>
<result name="error">index.jsp</result>
</action>
</package>
</struts>

这是我的web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <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>
  <welcome-file-list>
   <welcome-file>step1.jsp</welcome-file>
  </welcome-file-list>
   </web-app>

请帮助我,这样我就可以开始学习struts2了。

1 个答案:

答案 0 :(得分:3)

您的Getters和Setters错误命名。

如果你有一个名为

的属性,它们必须是 camelCased
private String fname;

您必须拥有以下访问者/变更者:

public String getFname() {
    return fname;
}
public void setFname(String fname) {
    this.fname = fname;
}

使用大写的F

要防止出现此类错误,请使用IDE生成错误;在Eclipse中,按 ALT SHIFT S 然后生成Getter和Setter