Arvind
Arvind

Reputation: 1568

ModelDriven Interface to get the values on success.jsp page

I wrote the code in my struts 2 application all is working fine but i am not getting attribute at succecc.jsp page. Please tell me where i am making mistake...

My Action class is

package action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

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

import dao.Empdao;
import model.Empmodel;
public class Empaction extends ActionSupport implements ModelDriven<Object>,SessionAware{

private static final long serialVersionUID = 1L;
    Empmodel modelobj;
    Map<String, Object> map;
    public String execute() throws Exception{
        map.put("a", modelobj.getFirstname());
        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;
    }

    @Override
    public void setSession(Map<String, Object> map) {
        // TODO Auto-generated method stub
        this.map =map;
    }

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="input">/Registration/empregistration.jsp</result>
        <result name="success">/Registration/success.jsp</result>
        <result name="error">/Registration/empregistration.jsp</result>
    </action>
</package>
</struts>

success.jsp is

<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    Registration successful
    <s:property value="#session.a" />
</body>
</html>

Empmodel class

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;
}

}

Upvotes: 0

Views: 1158

Answers (2)

Boris the Spider
Boris the Spider

Reputation: 61148

First you need to correct your action, the idea of using ModelDriven is that you push your model onto the stack, you are pushing null.
You can also get rid of the SessionAware if that's all you are using it for.

public class Empaction extends ActionSupport implements ModelDriven<Empmodel> {

    private static final long serialVersionUID = 1L;
    Empmodel modelobj = new Empmodel();

    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 Empmodel getModel() {
        return modelobj;
    }
}

Now your Empmodel is at the top of the ValueStack so this will work:

<s:property value="%{firstName}"/>

Make sure that the ModelDrivenInterceptor is on your interceptor stack.

Upvotes: 2

arvin_codeHunk
arvin_codeHunk

Reputation: 2390

why using session,if you can achieve this using valuestack, you have many ways to achieve this

using ognl

<s:property value="modelobj.firstname "/>

using scriptlet

<%
String fName=(String)session.getAtribute("a");

%>

Upvotes: 0

Related Questions