Question

I am new to struts 2 ,what i am doing is to create a form in struts 2 and show the entered value on other page.but when i submit the values on the step1.jsp page it is not showing on the profileuplaoded.jsp

Here are my jsp page on which i am entering the values step1.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>

here is my another jsp on which i want to show the values entered 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/>

here is my java file 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";

    }
}
}

here is my 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>

and here is my 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>

please help me in this such that i can start my struts2 learning.

Was it helpful?

Solution

Your Getters and Setters are wrongly named.

They must be camelCased for example if you have a property named

private String fname;

you must have the following accessors / mutators:

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

with the uppercased F.

To prevent this kind of errors, use the IDE to generate them; in Eclipse, press ALT SHIFT S then Generate Getters and Setters

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top