2

我是 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 学习。

4

1 回答 1

3

您的 Getter 和 Setter 名称错误。

例如,如果您有一个名为

private String fname;

您必须具有以下访问器/突变器:

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

大写的F.

为防止此类错误,请使用 IDE 生成它们;在 Eclipse 中,然后ALT SHIFT SGenerate Getters and Setters

于 2013-09-09T11:40:52.033 回答