Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts2 validation result input cannot be an action?

 
heng zhang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Marivada
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not an error to call action again to load data from database,but you will lose your error messages
 
heng zhang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic