aspose file tools*
The moose likes Struts and the fly likes JSP Form data lost while back to same Action (struts 2) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "JSP Form data lost while back to same Action (struts 2)" Watch "JSP Form data lost while back to same Action (struts 2)" New topic
Author

JSP Form data lost while back to same Action (struts 2)

Abhi Roy
Greenhorn

Joined: Jun 23, 2009
Posts: 22
Hi,
I have to action classes, say Test1 and Test 2 and two jsp pages,say jsp1 and jsp2, while jsp1 is related to Test1 and jsp2 is related to Test2. And the page flow is like , first the user will enter some data in jsp1 and click on next to go to the next page(jsp2) and also we have to display some of the fields entered injsp1. So I have extended Test2 to Test1, so that Test2 will contains all the commons fields and respective setters and getters.

As expected, on click on the next button, the data the jsp1 data is getting dispalyed in jsp2.Now while I'm trying to post the data from jsp2 and as a result users are reditected to the same page , jsp2, all the fields(inherited from Test1) are vanising.

Can some one help me out?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Struts 2 actions are instantiated per-request.
Abhi Roy
Greenhorn

Joined: Jun 23, 2009
Posts: 22
Thanks David, so what do you suggest to set the form fields in request as DTO to get the affect? Or is there any mechanism?

Thanks again for you input.

/Abhi
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Keep the list in session or application context, regenerate the list, assume your persistence solution has reasonable caching, etc.
Abhi Roy
Greenhorn

Joined: Jun 23, 2009
Posts: 22
Dear David,

Thanks for the solution, but in that case I beleive session will be overloaded. I want to keep this one as the last defence.I have just tried with a small PoC,
here is the code

My JSP -
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<s:form id="Login" name="Login" action="myAction">
<s:textfield value="%{userid}" name="%{userid}">
</s:textfield>
<s:submit>Login</s:submit>
</s:form>
</body>
</html>

My Action Class

public class MyTestAction extends ActionSupport implements ServletRequestAware {

private static final long serialVersionUID = -3594301868048109081L;
private String userid;
private HttpServletRequest request;


public String execute() throws Exception {
System.out.println("Step 3");
setUserid(userid);
if(this.userid!="sa")
return SUCCESS;
else
return INPUT;
}

public String getUserid() {
System.out.println("Step 1 " + userid);
return userid;
}

public void setUserid(String userid) {
System.out.println("Step 2 " + userid);
this.userid = userid;
}

public void setServletRequest(HttpServletRequest request) {
System.out.println("Step 4");
this.request = request;
}
}


and my Sturts.xml

<struts>
<include file="struts-default.xml" />
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="ajaxdemo" extends="struts-default">
<action name="myAction" class="ajaxdemo.action.MyTestAction">
<result name="input">/mydetail.jsp</result>
<result>/mydetail.jsp</result>
</action>
</package>
</struts>

A very simple form with a textbox and user can enter his name. But every time, both the setter and getter of the userid are printing null.

Sorry to disturb you again.
Thanks
Abhi Roy
Greenhorn

Joined: Jun 23, 2009
Posts: 22
Sorry ,
I'm calling that page from another index page...which is as follow


and one more correction, in my previous jsp, mydetail.jsp, that should look like


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Was there an additional question?

You're not providing enough information to help. If you don't submit a userId or have some other means of getting it (hidden form field, etc.) then yes, it will print null--it's not initialized.
Abhi Roy
Greenhorn

Joined: Jun 23, 2009
Posts: 22
ok...let me try in that way....thanks for your response....
Haravallabhan Jn
Greenhorn

Joined: Aug 10, 2009
Posts: 1
Abhi Roy wrote:ok...let me try in that way....thanks for your response....

use getUserid instead of this,userid and remove setUserid. You are setting the userid to null here. On submit you should get the value by using getUserid
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP Form data lost while back to same Action (struts 2)