aspose file tools*
The moose likes Struts and the fly likes struts2 validation result input cannot be an action? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts2 validation result input cannot be an action?" Watch "struts2 validation result input cannot be an action?" New topic
Author

struts2 validation result input cannot be an action?

heng zhang
Greenhorn

Joined: Jul 12, 2010
Posts: 16
Hi all,
The following problem has bothered me for days.

I have an action Registration.java which is used for users to create an account.
this action class has two methord: doList and execute.
doList gets data from the database and renders the initial jsp page with some s:select tags.
execute do the actual business logics.

in the struts.xml:

<action name="InitList" method="list" class="......Registration" >
<result name="success">/..../...../Registration.jsp</result>
</action>

<action name="Registration" class="......Registration">
<result name="input" >InitList.action</result>
<result name="next" type="redirect">InitListReg.action</result>
</action>



I also have a validation config file: RegistrationAction-Registration-validation.xml

when i created some validation errors and the intial page was not displayed with the error: InitList.action is not available. It seems strut2s did not recognized the action InitList. When i change the result input like this:

<action name="Registration" class="......Registration">
<result name="input" type="redirect">InitList.action</result>
<result name="next" type="redirect">InitListReg.action</result>
</action>

the initial page was displayed successfully, but the validation error messages were lost and not displayed because of "redirect".

So i wonder if input can be an action or only support jsps. Or how can i fix my problem?

appreciate any suggestions

Thanks
Sita Marivada
Greenhorn

Joined: Apr 01, 2010
Posts: 14
In your case you just need to write <result name="input" >yourfile.jsp</result> .
Whiel rendering the input page (if validation fails) ,the Action Support class which your action class will extend checks for hasActionErrors() and displays the errors if any.

If you have any dropdown lists(s:select) in your page which needs to be prepopulated everytime the page is rendered ,write some prepare method in your action class to load the data from database.You don't need to call an action class in the result in this case.


Sita Putta
Sita Marivada
Greenhorn

Joined: Apr 01, 2010
Posts: 14
Its not an error to call action again to load data from database,but you will lose your error messages
heng zhang
Greenhorn

Joined: Jul 12, 2010
Posts: 16
Sita Marivada wrote:
If you have any dropdown lists(s:select) in your page which needs to be prepopulated everytime the page is rendered ,write some prepare method in your action class to load the data from database.


<result name="input" >yourfile.jsp</result> is a better way to do it. Do you mean put the prepare methods in the execute of the action class?
in that case, will those methods be invoked again when i submit the form?

Thanks
Sita Marivada
Greenhorn

Joined: Apr 01, 2010
Posts: 14
Instead of populating controls in an Action method, implement the Preparable interface, and use a prepare method instead. The prepare method is called before validation, so if validation fails, we still have a chance to populate controls (or whatever).

By default ActionSupport implements Preparable interface,so just need to override prepare method and add code to load data.

For reference,look at http://struts.apache.org/2.x/docs/how-do-we-repopulate-controls-when-validation-fails.html
heng zhang
Greenhorn

Joined: Jul 12, 2010
Posts: 16
YES,
put the initialize methods in the Preparable interface.
but this still cannot prevent invoking those initialize methods when validation is passed and action executes successfully because prepare interceptor cannot be bypassed. but your approach is still good because it avoid action redirect which caused everything in request lost!!!

by the way, to pass error messages between actions, <interceptor-ref name="store"> can be used.


Thanks
Heng
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts2 validation result input cannot be an action?