You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Boon Leng <kb...@hotmail.com> on 2007/08/20 10:00:27 UTC

Reload combo option if validation fail.

In my form, I have a select box which get the option value populated from
database.
But when validation fail and return to input page (jsp page), I get the
error where the option value cannot be found. 

I know this is because the method (e.g search) is not called to retrieve
data from database. I tried to use "chain" result type to forward back to
the main action (e.g list) but I get "Infinite recursion detected" error.
May I know what is the best way to return to input page and at the sametime
retrieve data for the select box?

I don't want to store the option value in http session coz this way I need
to find a way to clean up unuse option value and might get problem is user
spore multiple tab on browser.

Appreciate is someone can give some guidelines. Thanks.


The following is my config and code.
struts.xml
-----------------
        <action name="*_search" class="{1}Action" method="list">
            <result name="success">/pages/{1}_search.jsp</result>
            <result name="input">/pages/{1}_search.jsp</result>
            <result name="cancel" type="redirect">welcome.html</result>
        </action>

Sample_search.jsp
-----------------
<s:form>
	<s:select name="user.country" 
	          label="%{getText('user.country')}" 
	          list="countryOptions"
	          headerKey="-1"
	          headerValue="%{getText('messages.select')}"/>

	<s:submit key="buttons.search" method="search" theme="simple"/>
	<s:submit key="buttons.cancel" method="cancel" theme="simple"/>
</s:form>

SampleAction
-----------------
public class SampleAction extends ActionSupport {
    ....

    @SkipValidation
    public String list() throws Exception {
        this.countryOptions = lookupManager.getCountries();
        return SUCCESS;
    }

    public String search() throws Exception {
        List<?> results = userManager.findByValueBean(user);
        this.listHolder = new PagedListHolder(results);

        return SUCCESS;
    }

    ....
}
-- 
View this message in context: http://www.nabble.com/Reload-combo-option-if-validation-fail.-tf4297263.html#a12231644
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Reload combo option if validation fail.

Posted by Boon Leng <kb...@hotmail.com>.
Hi,

I have tried using Preparable and putting all the collections loading in
prepare(), but because my action contains multiple methods and each load
different kind of collections, end up every method call will retrieve  all
the collections even though it's not using it. Is there anyway I able to
know which method will be execute in prepare() method? 


Zarar Siddiqi wrote:
> 
> You could implement Preparable and load countryOptions in the prepare()
> method.
> 
> http://struts.apache.org/2.x/struts2-core/apidocs/com/opensymphony/xwork2/Preparable.html
> 
> Zarar
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Reload-combo-option-if-validation-fail.-tf4297263.html#a12247181
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Reload combo option if validation fail.

Posted by Boon Leng <kb...@hotmail.com>.
Hi Dilip Ladhani-2,

Thanks for you reply, I know your solution works on struts 1, but I'm using
struts 2.
How can I call validate method in my Action class explictly?
Thanks.

Regards,
Boon Leng


Dilip Ladhani-2 wrote:
> 
> Well, here's what I do.
> 1) Have a private method (prepare??) in your action class which is
> responsible for populating all your Select boxes. This method is
> called from your main Action method.
> 
> 2) Call the validate method from your Action class explictly, instead
> of setting "validate=true" in the struts-config file
> 
> 3) After calling the validate, check the errors.size, if you have
> errors, just call the private method (prepare or whatever you call it
> and forward to your input page.
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Reload-combo-option-if-validation-fail.-tf4297263.html#a12247213
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Reload combo option if validation fail.

Posted by Dilip Ladhani <di...@gmail.com>.
Well, here's what I do.
1) Have a private method (prepare??) in your action class which is
responsible for populating all your Select boxes. This method is
called from your main Action method.

2) Call the validate method from your Action class explictly, instead
of setting "validate=true" in the struts-config file

3) After calling the validate, check the errors.size, if you have
errors, just call the private method (prepare or whatever you call it
and forward to your input page.


On 8/20/07, Zarar Siddiqi <za...@gmail.com> wrote:
> You could implement Preparable and load countryOptions in the prepare() method.
>
> http://struts.apache.org/2.x/struts2-core/apidocs/com/opensymphony/xwork2/Preparable.html
>
> Zarar
>
> On 8/20/07, Boon Leng <kb...@hotmail.com> wrote:
> >
> > In my form, I have a select box which get the option value populated from
> > database.
> > But when validation fail and return to input page (jsp page), I get the
> > error where the option value cannot be found.
> >
> > I know this is because the method (e.g search) is not called to retrieve
> > data from database. I tried to use "chain" result type to forward back to
> > the main action (e.g list) but I get "Infinite recursion detected" error.
> > May I know what is the best way to return to input page and at the sametime
> > retrieve data for the select box?
> >
> > I don't want to store the option value in http session coz this way I need
> > to find a way to clean up unuse option value and might get problem is user
> > spore multiple tab on browser.
> >
> > Appreciate is someone can give some guidelines. Thanks.
> >
> >
> > The following is my config and code.
> > struts.xml
> > -----------------
> >         <action name="*_search" class="{1}Action" method="list">
> >             <result name="success">/pages/{1}_search.jsp</result>
> >             <result name="input">/pages/{1}_search.jsp</result>
> >             <result name="cancel" type="redirect">welcome.html</result>
> >         </action>
> >
> > Sample_search.jsp
> > -----------------
> > <s:form>
> >         <s:select name="user.country"
> >                   label="%{getText('user.country')}"
> >                   list="countryOptions"
> >                   headerKey="-1"
> >                   headerValue="%{getText('messages.select')}"/>
> >
> >         <s:submit key="buttons.search" method="search" theme="simple"/>
> >         <s:submit key="buttons.cancel" method="cancel" theme="simple"/>
> > </s:form>
> >
> > SampleAction
> > -----------------
> > public class SampleAction extends ActionSupport {
> >     ....
> >
> >     @SkipValidation
> >     public String list() throws Exception {
> >         this.countryOptions = lookupManager.getCountries();
> >         return SUCCESS;
> >     }
> >
> >     public String search() throws Exception {
> >         List<?> results = userManager.findByValueBean(user);
> >         this.listHolder = new PagedListHolder(results);
> >
> >         return SUCCESS;
> >     }
> >
> >     ....
> > }
> > --
> > View this message in context: http://www.nabble.com/Reload-combo-option-if-validation-fail.-tf4297263.html#a12231644
> > Sent from the Struts - User mailing list archive at Nabble.com.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Reload combo option if validation fail.

Posted by Zarar Siddiqi <za...@gmail.com>.
That would do it too.  But you could've used the info in ActionProxy
in the prepare() method to differentiate between actions.

Later,
Zarar

On 8/20/07, Boon Leng <kb...@hotmail.com> wrote:
>
> Hi Zarar,
>
> I managed to solve the problem by using PreResultListener and find the
> action info in ActionProxy.
> Now I can load the collections base on method and result type :)
> Thanks for your help.
>
> Regards,
> Boon Leng
>
>
> Zarar Siddiqi wrote:
> >
> > You could implement Preparable and load countryOptions in the prepare()
> > method.
> >
> > http://struts.apache.org/2.x/struts2-core/apidocs/com/opensymphony/xwork2/Preparable.html
> >
> > Zarar
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Reload-combo-option-if-validation-fail.-tf4297263.html#a12247320
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Reload combo option if validation fail.

Posted by Boon Leng <kb...@hotmail.com>.
Hi Zarar,

I managed to solve the problem by using PreResultListener and find the
action info in ActionProxy.
Now I can load the collections base on method and result type :)
Thanks for your help.

Regards,
Boon Leng


Zarar Siddiqi wrote:
> 
> You could implement Preparable and load countryOptions in the prepare()
> method.
> 
> http://struts.apache.org/2.x/struts2-core/apidocs/com/opensymphony/xwork2/Preparable.html
> 
> Zarar
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Reload-combo-option-if-validation-fail.-tf4297263.html#a12247320
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Reload combo option if validation fail.

Posted by Zarar Siddiqi <za...@gmail.com>.
You could implement Preparable and load countryOptions in the prepare() method.

http://struts.apache.org/2.x/struts2-core/apidocs/com/opensymphony/xwork2/Preparable.html

Zarar

On 8/20/07, Boon Leng <kb...@hotmail.com> wrote:
>
> In my form, I have a select box which get the option value populated from
> database.
> But when validation fail and return to input page (jsp page), I get the
> error where the option value cannot be found.
>
> I know this is because the method (e.g search) is not called to retrieve
> data from database. I tried to use "chain" result type to forward back to
> the main action (e.g list) but I get "Infinite recursion detected" error.
> May I know what is the best way to return to input page and at the sametime
> retrieve data for the select box?
>
> I don't want to store the option value in http session coz this way I need
> to find a way to clean up unuse option value and might get problem is user
> spore multiple tab on browser.
>
> Appreciate is someone can give some guidelines. Thanks.
>
>
> The following is my config and code.
> struts.xml
> -----------------
>         <action name="*_search" class="{1}Action" method="list">
>             <result name="success">/pages/{1}_search.jsp</result>
>             <result name="input">/pages/{1}_search.jsp</result>
>             <result name="cancel" type="redirect">welcome.html</result>
>         </action>
>
> Sample_search.jsp
> -----------------
> <s:form>
>         <s:select name="user.country"
>                   label="%{getText('user.country')}"
>                   list="countryOptions"
>                   headerKey="-1"
>                   headerValue="%{getText('messages.select')}"/>
>
>         <s:submit key="buttons.search" method="search" theme="simple"/>
>         <s:submit key="buttons.cancel" method="cancel" theme="simple"/>
> </s:form>
>
> SampleAction
> -----------------
> public class SampleAction extends ActionSupport {
>     ....
>
>     @SkipValidation
>     public String list() throws Exception {
>         this.countryOptions = lookupManager.getCountries();
>         return SUCCESS;
>     }
>
>     public String search() throws Exception {
>         List<?> results = userManager.findByValueBean(user);
>         this.listHolder = new PagedListHolder(results);
>
>         return SUCCESS;
>     }
>
>     ....
> }
> --
> View this message in context: http://www.nabble.com/Reload-combo-option-if-validation-fail.-tf4297263.html#a12231644
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org