Question

In my application I am trying to register a user by adding his details to the database and after success fully insert these values. I need to display form input page but on result page I am not getting any output.

My ACTION CLASS IS

package action;


import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import dao.Empdao;
import model.Empmodel;
public class Empaction extends ActionSupport implements ModelDriven<Object>{
private static final long serialVersionUID = 1L;
Empmodel modelobj;

    public Empmodel getModelobj() {
    return modelobj;
}

public void setModelobj(Empmodel modelobj) {
    this.modelobj = modelobj;
}
    public String execute() throws Exception{
        Empdao empdao = new Empdao();
        int queryResult = empdao.registration(modelobj);


        if (queryResult==0)
        {   
          addActionError("it is a dublicate entry please enter anothe id");
         return ERROR;
        }   
        else{

         return SUCCESS;    
            }

}


    @Override
    public Object getModel() {
        modelobj = new Empmodel();
        return null;
    }

}

Success.jsp is

<body>


  first name : <s:property value="modelobj.firstname"/> <br>
last name :<s:property value="modelobj.lastname" />
<s:property value="modelobj.id" />
<s:property value="modelobj.gender" />
<s:property value="modelobj.dob" />
<s:property value="modelobj.maritalstatus" />
<s:property value="modelobj.email" />
<s:property value="modelobj.joiningdate" />
<s:property value="modelobj.designation" />
<s:property value="modelobj.country" />
<s:property value="modelobj.state" />
<s:property value="modelobj.city" />
<s:property value="modelobj.pincode" />
<s:property value="modelobj.mobileno" />
<s:property value="modelobj.groups" />

</body>
</html>

Empmodel CLASS IS

package model;

public class Empmodel {
private String firstname;
private String lastname;                        
private String id;
private String gender;
private String dob;
private String maritalstatus;
private String email;
private String joiningdate;
private String designation;
private String address;
private String country;
private String state;
private String city;
private int  pincode;
private long mobileno;
private String groups;
public String getFirstname() {
    return firstname;
}
public void setFirstname(String firstname) {
    this.firstname = firstname;
}
public String getLastname() {
    return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getDob() {
    return dob;
}
public void setDob(String dob) {
    this.dob = dob;
}
public String getMaritalstatus() {
    return maritalstatus;
}
public void setMaritalstatus(String maritalstatus) {
    this.maritalstatus = maritalstatus;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getJoiningdate() {
    return joiningdate;
}
public void setJoiningdate(String joiningdate) {
    this.joiningdate = joiningdate;
}
public String getDesignation() {
    return designation;
}
public void setDesignation(String designation) {
    this.designation = designation;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getState() {
    return state;
}
public void setState(String state) {
    this.state = state;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}


public long getMobileno() {
    return mobileno;
}
public void setMobileno(long mobileno) {
    this.mobileno = mobileno;
}
public String getGroups() {
    return groups;
}
public void setGroups(String groups) {
    this.groups = groups;
}
public int getPincode() {
    return pincode;
}
public void setPincode(int pincode) {
    this.pincode = pincode;
}

}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
   "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
           <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
           <constant name="struts.devmode" value="true"/>
          <package name="loginmodel" extends ="struts-default">


           <action name ="emplogin" class ="action.Empaction">

          <result name= "wait">/Registration/wait.jsp</result>
          <result name = "input">/Registration/empregistration.jsp</result>
           <result name ="success" type="redirect" >/Registration/success.jsp</result>
         <result name="error">/Registration/empregistration.jsp</result>                   
        </action>


          </package>

empregistration.jsp

<%@ page language ="java" contentType ="text/html; charset=ISO-8859-1" pageEncoding ="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="/struts-dojo-tags" prefix="sx" %> 
 <html>
    <head>
       <sx:head/>
    <script type="text/javascript"  src ="script.js"></script>

    </head>  

      <body>
        <div align="center"> <h1 style="color: red">  ENPLOYEE REGISTRATION FORM</h1>
        <s:form  action="emplogin"  method="post">

      <s:textfield name="modelobj.firstname" label="Employee Firstname"/>
      <s:textfield name ="modelobj.lastname" label ="Last name"/>  
      <s:textfield name ="modelobj.id"  label="Id"/>
      <s:radio name ="modelobj.gender"   list="{'male', 'female'}" label = "Gender"/>
    <sx:datetimepicker name="dob" displayFormat="dd-MMM-yyyy"  label="DOB"></sx:datetimepicker> 
      <s:radio  name ="modelobj.maritalstatus" list="{'singale','married'}" label="Marital Status" />

      <s:textfield name ="modelobj.email" label ="Email"/>
 <sx:datetimepicker name ="modelobj.joiningdate" displayFormat="dd-MMM-yyyy" label="Joining Date"></sx:datetimepicker>

      <s:textfield name= "modelobj.designation" label = "Designation"/>
      <s:textarea name ="modelobj.address" label ="Address" />
      <s:textfield name = "modelobj.country" label ="Country" />     
      <s:textfield name  ="modelobj.state" label = "State" />
      <s:textfield name  ="modelobj.city" label ="City"/> 
      <s:textfield name ="modelobj.pincode" label ="Pincode"/>
      <s:textfield name ="modelobj.mobileno" label="Mobile No"/>
      <s:select   name ="modelobj.groups" list="{'group 1', 'group 2', 'group 3'}"  label ="Group"  cssStyle="{width:184px"/>
     <tr><td>&nbsp;</td></tr>
        <tr>
    <td>&nbsp;</td>
      <s:submit align="center"></s:submit>
      </s:form>
       </div>
    </body>
 </html>
Was it helpful?

Solution

You are redirecting the page on success that is wrong because you loose values needed to display results. Use

<result name ="success">/Registration/success.jsp</result>

or simply

<result>/Registration/success.jsp</result>

BTW, you only have getters & setters for firstname and lastname, so the only these properties will display. If you want to display other properties then you should add correcponding methods to fields.

You may read the servlet API javadoc to make clue what may cause result type="redirect".

For further compare the difference between forward and redirect results consider this thread.

OTHER TIPS

If type=redirectAction then you have to mention the Action name without any extensions if its only redirect then you have to mention including the action name and extension, For your Question it was because of redirect the problem happened.

Even after redirecting to some page and when you come back to the result page , and if you want to retain data then its better you better put in session .

Do not redirect you page remove it and use

<result name ="success">/Registration/success.jsp</result> in you struts.xml file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top