Reputation: 600
I am new to Struts2, trying to use addFieldError
in my form with <s:select>
tag.
I have a form in which i have to select a value from a drop-down list. First time when this form is shown, the response comes from an Action class, in which i have created a List. In my form i am printing that list in a drop-down using <s:select>
tag like this:
<s:form action="clientselect">
<h2>Select Client to add the case :</h2>
<table>
<s:select list="list" headerKey="-1" headerValue="Select Client"
label="Select Client" tooltip="Select the desired client" name="client">
</s:select>
<tr><td><s:submit value="Register Case" theme="simple" align="right"/></td>
<td><s:reset value="Reset" theme="simple" align="right"/></td></tr>
</table>
</s:form>
"pageinclude=ancar" prints this form.
struts.xml
<action name="clientselect" class="casediary.JudicialCaseRegisterValidation" method="execute">
<result name="addcase">user.jsp?pageinclude=ncr</result>
<result name="error">user.jsp?pageinclude=errancar</result>
<result name="input">user.jsp?pageinclude=ancar</result>
<result name="loggedout">index.jsp?pageinclude=relogin</result>
</action>
in JudicialCaseRegisterValidation.java
public void validate()
{
if(client==null || client.equals("-1"))
addFieldError("client", "This field can not be blank.");
}
All is working fine. error condition is satisfied and in result i am getting "input". Error message is also printing BUT THE VALUES IN THE DROP-DOWN LIST ARE GONE. THE LIST IS PRINTING EMPTY. Because this time response is not coming from the Action class in which the List is set.
then i changed my struts.xml to send the request to the Action class from the <result>
like this:
<result name="input" type="redirect">link.action</result>
"link.action" is what that is sending the request in Action class to create the List and printing that form.
But this time the form is just printed again & no error message is printed.
I want that List be printed again and also the error message besides it. Please Tell How.?
Upvotes: 2
Views: 2387
Reputation: 50203
Pretty much everyone starting with Struts2 encounter this problem, soon or later.
When requesting an Action in Struts2, your request will pass through an Interceptor Stack (a list of Interceptors); every Interceptor does is particular business, then redirect the request (in case of errors) or proceed the execution to the next Interceptor, or to the Action if it is the last.
The validation is performed by the Validation Interceptor. If the validation fails, it will hijack the request and will redirect it to the input result defined in struts.xml. No matter if the validation is by XML, by Annotation or in a validate() method inside the Action: the Action is not reached ! Then, everything inside the execute()
method, or the method you are calling (if you are using a custom one) is not executed;
If the loading of your List elements is demanded to the execute()
method, it won't be performed in case of an input result.
The main ways to avoid this problem are:
Implement Preparable Interface, and put all the data loading stuff into the prepare() method.
That method is run by the Prepare Interceptor, that is placed BEFORE the Validation Interceptor;
it will always run, and it will run before the validation, no matter which method of your action (execute()
or something else) you are calling, nor if you are encountering validation errors;
Use <s:action/>
tag from the JSP, to call dummy Actions returning JSP snippets;
Use a redirectAction
as the input result type, to the execute()
(or something else) method, as you are doing.
While redirecting, it will lose all the request parameters (including action errors, field errors, action messages etc) , then you will need to manually copy them by declaring them in the struts.xml as parameters of the redirectAction
result.
The first way is the preferred, imho.
EDIT:
just found it in the official documentation too: how do we repopulate controls when validation fails ?
Upvotes: 2
Reputation: 285
I think type = "redirectAction" to redirect to a particular action
Something like this
<result name="input" type="redirectAction">link</result>
hope it helped
you set it in your execute() method if i am correct or where ever you are returning the result of that action like this
list=setList(newList);
return result;
Upvotes: 0