prepare action for input after validation errors

2008-09-07 Thread Jean-Baptiste Nizet

Hello.

I'm reading the documentation for Struts 2 (after lots of work on Struts 
1 and Beehive), and I wonder if an interceptor exists in order to 
prepare the action after the validation fails, and before going back to 
the input page. Imagine the following action class :


public class MyAction extends ActionSupport {
  public String displayForm() {
 prepareForInput();
 return INPUT;
  }

  private void prepareForInput() {
 // go to the database and get some data in order to display it in 
the input form page

  }

  public String submitForm() {
 // this action is only called if validation succeeds
 // submits the form and returns a redirect result 
(redirect-after-post)

 return SUCCESS;
  }

  public void validateSubmitForm() {
 // validation logic for the submitForm action
 // after this call, if there is an error, the workflow interceptor 
will return the INPUT result, but the necessary data won't be in the action

 // because the prepareForInput method hasn't been called
  }
}

Is there an interceptor that would call my prepareForInput method?
I could call it in the validateSubmitForm method, but the validation 
logic would have a side effect, and I wouldn't be able to validate 
declaratively anymore.
I could call the prepareForInput method from the page, but the global 
exception handling wouldn't work (AFAIK) in the case an exception is 
thrown from this method.
I could use a Prepare interceptor, but the it would prepare regardless 
of the result of the validation.
The best way I see would be to use a subclass of the workflow 
interceptor (or add another interceptor after validation) that would 
automatically call a method named, for example, postValidationError or 
postValidationErrorSubmitForm before returning the INPUT result. Is 
there such a beast? How do you usually solve this problem?


Thanks.

JB.

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: prepare action for input after validation errors

2008-09-08 Thread Lukasz Lenart
Hi,

Take a look at Prepare Interceptor [1] and Preparable interface [2]

[1] http://struts.apache.org/2.x/docs/prepare-interceptor.html
[2] 
http://struts.apache.org/2.0.11.2/struts2-core/apidocs/com/opensymphony/xwork2/Preparable.html


Regards
-- 
Lukasz
http://www.lenart.org.pl/

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: prepare action for input after validation errors

2008-09-08 Thread jnizet
Hi.

Thanks for your answer. But as I said in my original post, the prepare 
interceptor would prepare the action regardless of the validation result. If 
the validation succeeds, I don't want the data for the input page to be 
prepared, as the flow will go to the submit method which will redirect to 
somewhere else.

JB.

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: prepare action for input after validation errors

2008-09-08 Thread Lukasz Lenart
You can use perpareXXX method per action method as mentioned in docs:

Applies only when action implements Preparable

   1. if the action class have prepare{MethodName}(), it will be invoked
   2. else if the action class have prepareDo(MethodName()}(), it will
be invoked
   3. no matter if 1] or 2] is performed, if alwaysinvokePrepare
property of the interceptor is "true" (which is by default "true"),
prepare() will be invoked.


In other way, you can also develop your own interceptor which will
suite your needs.


Regards
-- 
Lukasz
http://www.lenart.org.pl/

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: prepare action for input after validation errors

2008-09-08 Thread jnizet
I know that. But this does not solve the problem I described. prepareXxx will 
be invoked if I the action method is xxx. I validation fails, it'll work: the 
input page will have the prepared data ready in the action instance. If 
validation succeeds, however, the prepareXxx method will have prepared useless 
data, because the xxx method will forward or redirect to somewhere else.

JB.

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: prepare action for input after validation errors

2008-09-08 Thread Lukasz Lenart
How did you configure your input method? What is the name of this
method? I still don't understand why you can use Prepare interceptor,
just implement prepareDoInput() method and that method will be called
just before input() call.

If input result redirect to some other action, configure and implement
prepareDo method in that action


Regards
-- 
Lukasz
http://www.lenart.org.pl/

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: prepare action for input after validation errors

2008-09-08 Thread jnizet
I don't have any input method. I have a displayForm method, and a submitForm 
method. 

The displayForm method returns an input *result*, which is a default result 
pointing to a JSP. This JSP has to display some data coming from the database. 
I'd like this same JSP to be displayed when the input *result* is returned by 
the workflow interceptor after validation fails. My problem is that I don't 
know how to make Struts get the data necessary to render the input JSP. This 
data is prepared by the displayForm action method when it's displayed 
initially, but when the form is submitted and a validation error occurs, Struts 
goes directly to the input JSP without calling any action or other method. 
There is no (AFAIK) equivalent of the prepare interceptor for *results*.

The way that seems to be suggested by the documentation is to prepare the data 
by calling an action from the JSP itself, but I don't like this solution very 
much because, if the data preparation method throws an exception, it won't be 
handled correctly (since the HTML has already been partly sent to the browser).

I could also chain the actions by making the input result chain to another 
action method, but this is discouraged by the doc (see 
http://struts.apache.org/2.x/docs/action-chaining.html).

JB.

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: prepare action for input after validation errors

2008-09-08 Thread Dave Newton
--- On Mon, 9/8/08, [EMAIL PROTECTED] wrote:
> I don't have any input method. I have a displayForm
> method, and a submitForm method. 
> 
> The displayForm method returns an input *result*, which is
> a default result pointing to a JSP. This JSP has to display
> some data coming from the database. I'd like this same
> JSP to be displayed when the input *result* is returned by
> the workflow interceptor after validation fails. My problem
> is that I don't know how to make Struts get the data
> necessary to render the input JSP. This data is prepared by
> the displayForm action method when it's displayed
> initially, but when the form is submitted and a validation
> error occurs, Struts goes directly to the input JSP without
> calling any action or other method. There is no (AFAIK)
> equivalent of the prepare interceptor for *results*.
> 
> The way that seems to be suggested by the documentation is
> to prepare the data by calling an action from the JSP
> itself, but I don't like this solution very much
> because, if the data preparation method throws an exception,
> it won't be handled correctly (since the HTML has
> already been partly sent to the browser).
> 
> I could also chain the actions by making the input result
> chain to another action method, but this is discouraged by
> the doc (see http://struts.apache.org/2.x/docs/action-chaining.html).

A couple of ideas have already been provided; the way your application is 
currently structured leaves you with little alternative than to implement one 
of the suggestions or re-structure things a bit.

An additional potential solution I don't recall seeing is to override 
validate(), call super.validate(), and if there are errors, call the 
initialization code.

Dave


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: prepare action for input after validation errors

2008-09-08 Thread Martin Gainty

you can return the result you desire from the Action which implements 
parameterNameAware interceptor



good_result.ftl

http://struts.apache.org/2.x/docs/parameters-interceptor.html

Bon Chance,
Martin

__ 
Disclaimer and confidentiality note 
Everything in this e-mail and any attachments relates to the official business 
of Sender. This transmission is of a confidential nature and Sender does not 
endorse distribution to any party other than intended recipient. Sender does 
not necessarily endorse content contained within this transmission. 


> Date: Mon, 8 Sep 2008 23:02:49 +0200
> From: [EMAIL PROTECTED]
> To: user@struts.apache.org
> Subject: Re: prepare action for input after validation errors
> 
> I don't have any input method. I have a displayForm method, and a submitForm 
> method. 
> 
> The displayForm method returns an input *result*, which is a default result 
> pointing to a JSP. This JSP has to display some data coming from the 
> database. I'd like this same JSP to be displayed when the input *result* is 
> returned by the workflow interceptor after validation fails. My problem is 
> that I don't know how to make Struts get the data necessary to render the 
> input JSP. This data is prepared by the displayForm action method when it's 
> displayed initially, but when the form is submitted and a validation error 
> occurs, Struts goes directly to the input JSP without calling any action or 
> other method. There is no (AFAIK) equivalent of the prepare interceptor for 
> *results*.
> 
> The way that seems to be suggested by the documentation is to prepare the 
> data by calling an action from the JSP itself, but I don't like this solution 
> very much because, if the data preparation method throws an exception, it 
> won't be handled correctly (since the HTML has already been partly sent to 
> the browser).
> 
> I could also chain the actions by making the input result chain to another 
> action method, but this is discouraged by the doc (see 
> http://struts.apache.org/2.x/docs/action-chaining.html).
> 
> JB.
> 
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

_
See how Windows Mobile brings your life together—at home, work, or on the go.
http://clk.atdmt.com/MRT/go/msnnkwxp1020093182mrt/direct/01/

RE: prepare action for input after validation errors

2008-09-08 Thread Dave Newton
--- On Mon, 9/8/08, Martin Gainty wrote:
> you can return the result you desire from the Action which
> implements parameterNameAware interceptor
> 
> 
>   
>   good_result.ftl
> 
>
> http://struts.apache.org/2.x/docs/parameters-interceptor.html

I guess I'm not clear on how that deals with his issue with Preparable and the 
prepareXxx() methods; can you explain further?

Thanks,
Dave


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]