You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Struts2 Fan <st...@gmail.com> on 2007/05/17 16:00:30 UTC

When validation fails, how to add beans back to the value stack

Hi all,

I have this problem since I started using struts 2 and I still cannot find
the solution. Please help me. :)

Note : I didn't write all the codes.

I have a form.

<s:form>
         <%-- order is a property in orderItem bean--%>
        <s:set name="order" value="orderItem.order"/>
        <s:label value="%{order.date}" label="order.date"/>

        <s:hidden name="orderItem.order.id"/>
        <s:textfield name="orderItem.quantity" key="orderItem.quantity"/>
        <s:submit/>
</s:form>

Here orderItem.quantity is a required field. So when I left if blank and
press the submit button, the validation fails and displays the error message
in the screen. But this time label with id="orderDate" doesn't display the
order.date field on the screen because orderItem is no longer in the value
stack I guess. So it cannot get the order value of orderItem. It writes
nothing on the screen. 

Here is the struts.xml

<action name="....." class="....." method="insert">
    <result
name="input">/WEB-INF/pages/order/orderItem/orderItemModify.jsp</result>
</action>

When validation fails It returns to jsp. Because of not reaching an action I
cannot put order to the value stack back. 

Any idea how can I get it worked? I hope I explained it well. If not please
post a message. 
-- 
View this message in context: http://www.nabble.com/When-validation-fails%2C-how-to-add-beans-back-to-the-value-stack-tf3772094.html#a10664756
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: When validation fails, how to add beans back to the value stack

Posted by Struts2 Fan <st...@gmail.com>.
I forgot to mention something. I implemented Preparable interface. And in
prepare method I can get the order.id from request. But I don't want to get
the value from getRequest().getParameter("order.id") to create the bean and
put it again on the value stack. I expect it to populate order bean again as
in the action methods. Do I expect too much? :) 
-- 
View this message in context: http://www.nabble.com/When-validation-fails%2C-how-to-add-beans-back-to-the-value-stack-tf3772094.html#a10665112
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: When validation fails, how to add beans back to the value stack

Posted by Guillaume Carré <gu...@gmail.com>.
2007/5/18, Laurie Harper <la...@holoweb.net>:
> You could point your input result to the same action you use to populate
> the form in the first place. That way you can do the same data
> pre-fetching on validation failure as you do on first displaying the form.

how do you point to the previous action in the input result? with a
<result type="chain">?

-- 
Guillaume Carré

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


Re: When validation fails, how to add beans back to the value stack

Posted by Laurie Harper <la...@holoweb.net>.
You could point your input result to the same action you use to populate 
the form in the first place. That way you can do the same data 
pre-fetching on validation failure as you do on first displaying the form.

The other alternative would be to make sure all data you want to have 
available is included in the form, so it's carried through as request 
parameters. For example, to solve the specific case of your order date 
example, you could include that as a hidden field, like so:

   <s:hidden name='orderItem.order.date'/>

Depending on what objects you place in what scopes, how you have your 
initial action and form processing action set up, how you have 
conversion and validation configured, etc, there may be other factors 
you need to take into account.

L.

Struts2 Fan wrote:
> Hi all,
> 
> I have this problem since I started using struts 2 and I still cannot find
> the solution. Please help me. :)
> 
> Note : I didn't write all the codes.
> 
> I have a form.
> 
> <s:form>
>          <%-- order is a property in orderItem bean--%>
>         <s:set name="order" value="orderItem.order"/>
>         <s:label value="%{order.date}" label="order.date"/>
> 
>         <s:hidden name="orderItem.order.id"/>
>         <s:textfield name="orderItem.quantity" key="orderItem.quantity"/>
>         <s:submit/>
> </s:form>
> 
> Here orderItem.quantity is a required field. So when I left if blank and
> press the submit button, the validation fails and displays the error message
> in the screen. But this time label with id="orderDate" doesn't display the
> order.date field on the screen because orderItem is no longer in the value
> stack I guess. So it cannot get the order value of orderItem. It writes
> nothing on the screen. 
> 
> Here is the struts.xml
> 
> <action name="....." class="....." method="insert">
>     <result
> name="input">/WEB-INF/pages/order/orderItem/orderItemModify.jsp</result>
> </action>
> 
> When validation fails It returns to jsp. Because of not reaching an action I
> cannot put order to the value stack back. 
> 
> Any idea how can I get it worked? I hope I explained it well. If not please
> post a message. 


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