• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Losing hidden variable value during Struts 2 validation

 
suresh midde
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I am using struts 2.0.14 . I have a hidden parameter that is being passed from a page profile.jsp , which hits an action and navigates to the other page "editUser.jsp", where I am trying to display a few buttons based on the hidden parameter value from the previous page.


profile.jsp
-----------------
<input type="hidden" name ="fromProfilePage" id="fromProfilePage" value="true"/>

UserAction.java
------------------
I have created a variable and its setter and getter methods in the action class.

private String fromProfilePage = "false";
public String getFromProfilePage() {
return fromProfilePage;
}

public void setFromProfilePage(String fromProfilePage) {
this.fromProfilePage = fromProfilePage;
}

And the following code inside a method that handles the previous page request,

String fromProfile = getServletRequest().getParameter("fromProfilePage");

if ((null != fromProfile) && (!("".equals(fromProfile)))) {
fromProfilePage = fromProfile;
}


editUser.jsp
--------------
<s:if test="%{fromProfilePage == 'true'}">
Hello from profile page...
<s:submit ondblclick="return false;" action="editProfileChanges" key="label.save" cssClass="btn"/>
<s:submit ondblclick="return false;" action="populateProfiles" key="label.cancel" cssClass="btn"/>
</s:if>
<s:else>
<s:submit ondblclick="return false;" action="addUser" key="label.save" cssClass="btn"/>
<s:submit ondblclick="return false;" action="populateUser" key="label.cancel" cssClass="btn"/>
</s:else>




For validation , I am using validations xml files
The above thing works fine, if all required fields are entered from the page. But if one of the mandatory fields are not passed, I get the error messages onto the same page, but I am losing fromProfilePage value, which shows the other block now.........


Is there any way to retain fromProfilePage even after the validation ?

Regards
Suresh Midde
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what is going wrong.

But instead of this in profile.jsp


try this
 
suresh midde
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitin,

Thanks for the reply. I tried replacing the html hidden with the struts hidden in the profile.jsp , but it didnt solve my problem. And I have come up with adding another hidden parameter in the editUser.jsp, which has the value of the earlier hidden parameter

profile.jsp
----------------

<input type="hidden" name ="fromProfilePage" id="fromProfilePage" value="true"/> or <s:hidden name="fromProfilePage" value="true"/>


and in


editUser.jsp
---------------------

<s:hidden name="fromProfilePage" >

This works even failing with the validation, which brings me the first block of components. But I am not finding any reason for this working.

Regards
Suresh Midde
 
Jigs Gohil
Greenhorn
Posts: 6
Eclipse IDE jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its actually quite simple concept of http request.
1st time the value gets set into the request and you get it.
But on the submit, a new request is generated.
Now, since you don't have that hidden variable in second jsp, the action object gets initialized with null value in "fromProfilePage" variable.
When you add the hidden variable, the value is retained in the jsp, so you get it in the action as well.
 
candid java
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found the exact answer from this link you can check it out http://candidjava.com/struts-1x-validation-with-example-program-in-eclipse
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic