1

Here it goes: I am refreshing a page named index.jsp via this

<head>
        <meta http-equiv="Refresh" CONTENT="0;URL=populatefields.action">

</head>

And it is calling my action. here is the mapping in my struts.xml

 <struts>

        <package name="com.action" extends="struts-default">
            <action name="*fields" method="{1}" class="com.action.RegisterAction">
    <result name="populate">/register.jsp</result>
    <result name="fields">/success.jsp</result>
<result name="error">/index.jsp</result>
    </action>

What i did here is on loading of index.jsp, i refreshed it with this url 'populatefields.action'. This calls my action class and the populate method(wildcard method invocation).Populate method picks entry from databases and assign them to form fields properties and returns "populate";Which takes the control to register.jsp with some values filled already.[i.e dynamic filling of a part of form according to the current user]

On register.jsp if some validation check is there like if(getUssername.equals("")) {this.addActionerror("Empty field -Uname-"); return error;}

The problem is on return "error" the control goes to index.jsp and then to populate method and automatic fields get populated but populate method does not throw any errors so is empty...or errors are lost ... Any idea how i can access thses errors ? by setting dem global in my action class? The actionerrors comes in index.jsp but i want to refresh that page evry time it loads to pick values form Database...can i take the value of action error from one jsp page to another ?

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
nr5
  • 4,228
  • 8
  • 42
  • 82
  • You can only get so far with html if you want more complicated dynamic behaviour the easiest thing to do is use AJAX. There are numerous examples here of using the JSON plugin. What you ask for may be possible by pushing the errors into the session but it would be a brittle solution which would not apply to many kinds of web applications so it is better to stick with a method that works in most every case. – Quaternion May 06 '12 at 20:46
  • did it check my answer ! – nr5 May 07 '12 at 08:51

1 Answers1

1

Done: I never thought it would be that easy.. no need to transfer the values of <s:actionerror/>.But you can do transfer your whole page using <jsp:include page='index.jsp'> if it doesnot refreshes and contains only <s:actionerror/> tag. Like when you use an <a> link to call your action, not in this case that refreshes its way to action.. But in my case i want a refresh so be it:

Just before when you type return error; copy the contents of your populate method there in your execute method. Such as

execute{
if(validation fails){populate method content(leave the return statement); 
  return error;
 }
else
return success;
}

In struts.xml change this <result name="error">/register.jsp</result>rather than <result name="error">/index.jsp</result>

Also no need to write <s:actionerror> in index.jsp,since you are now redirecting it to register.jsp so just write it in register.jsp...

Now the refresh funda working that take you to register form.. Try submitting now if validation fails same page will get called with populated fields :D and action errors...

nr5
  • 4,228
  • 8
  • 42
  • 82
  • Normally this would be handled trivially by using the `"input"` result to be the form. S2 will forward to the input result automatically on validation failure. It seems like you're doing a bunch of manual work where the framework already has the functionality in place. – Dave Newton May 07 '12 at 13:59
  • Right and if you named you had an input form called myform-input.jsp and used the conventions plugin you would probably not had to do anything but follow a naming convention. – Quaternion May 08 '12 at 02:37