You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Andreas Mähler <an...@stud.uni-karlsruhe.de> on 2008/07/10 14:12:39 UTC

[S2] Recipe for Action and View

Hello everyone,

this is my first S2 project - i am a newbie (although i have a tiny
little experience with S1...). :-)

I am storing my view (JSP) files in /WEB-INF/jsp/... so they cannot be
accessed directly to prevent links from external pages that contain fake
parameters. Therefore all requests are handled by actions.

AFAIR, the current coding practice in S1 is to use an action that caters
a form (e.g. prepopulate) and presents the view to the user. A second
action then takes care of the form processing (including validation and
everything).
                                                              ^
                                                  <<success>> |
                                                              |
        /-----------------\    +--------------+     /-----------------\
   --->( presentFormAction )-->|   form.jsp   |<-->( processFormAction )
        \-----------------/    +--------------+     \-----------------/

My questionis the following: Is it possible to merge these two actions
into a single one? Or are there any drawbacks?

               ^
               | <<success>>
               |
        /--------------\     +--------------+
   --->(   formAction   )<-->|   form.jsp   |
        \--------------/     +--------------+

The problem seems to be the validation. Of course, I don't want the
initial request (with all params set to null) validated (and the error
messages generated).

A solution could be to assign a param/value pair to the "Submit"-Button
and let an interceptor check for it. If not found, forward to INPUT..
The interceptor must fire before the error-generating interceptors
(conversionError, validation and workflow AFAIK).

Is there a good way to do this? Maybe I am not the first one who wants
to do it and there is already something in the default interceptor
stack. I wouldn't touch the S2-defaults until I really must.

Or is there a reason why my approach wouldn't work? Proposals are
welcome :-)

Thanks very much in advance,
~Andreas


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


Re: [S2] Recipe for Action and View

Posted by Gabriel Belingueres <be...@gmail.com>.
Calling an action's input() method is a "shortcut" to render the page
pointed by the "input" result.

If you need to do some processing before displaying the page, just
implement a regular action.

2008/7/14 ravi_eze <ra...@ivycomptech.com>:
>
> hi,
>
> i didnt get you. I have a page for which i need to do some preprocessing to
> display it. After the page is done by user it has to be processed. Can you
> please elaborate on the soln given?
>
> ravi
>
>
> Gabriel Belingueres-2 wrote:
>>
>> Sorry it seems I entered the conversation later and didn't saw that email.
>>
>> The problem you want to solve is very common (skipping form validation
>> the first time you call an action) and, as Dave wrote, it is commonly
>> solved by invoking the action with the input() method [1]. This method
>> will do nothing and will return INPUT as the result, thus forwarding
>> the response to the page with the form.
>>
>> [1]
>> http://struts.apache.org/2.1.2/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html#input()
>>
>> 2008/7/12 Andreas Mähler <an...@stud.uni-karlsruhe.de>:
>>> Hello again,
>>>
>>> Gabriel Belingueres schrieb:
>>>>
>>>> Without knowing what are you actually trying to accomplish with this
>>>> interceptor, I would warn that when an interceptor messes up with the
>>>> parameters, you need to carefully think where in the interceptor chain
>>>> this interceptor will execute.
>>>
>>> I explain what I am trying to do in my first post[1].
>>>
>>> What do you mean by messing up with the parameters? The Interceptor only
>>> checks for the presence of at least one of the specified parameters. It
>>> does
>>> not change anything. Are there any interceptors that actually change the
>>> parameter map? As I said, I am a S2 newbie and haven't heard of many
>>> things,
>>> but I would consider it as bad style.
>>>
>>> Greetings,
>>> ~Andreas
>>>
>>>
>>> [1] http://article.gmane.org/gmane.comp.jakarta.struts.user/158395
>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/-S2--Recipe-for-Action-and-View-tp18381617p18438070.html
> 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: [S2] Recipe for Action and View

Posted by ravi_eze <ra...@ivycomptech.com>.
hi,

i didnt get you. I have a page for which i need to do some preprocessing to
display it. After the page is done by user it has to be processed. Can you
please elaborate on the soln given?

ravi


Gabriel Belingueres-2 wrote:
> 
> Sorry it seems I entered the conversation later and didn't saw that email.
> 
> The problem you want to solve is very common (skipping form validation
> the first time you call an action) and, as Dave wrote, it is commonly
> solved by invoking the action with the input() method [1]. This method
> will do nothing and will return INPUT as the result, thus forwarding
> the response to the page with the form.
> 
> [1]
> http://struts.apache.org/2.1.2/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html#input()
> 
> 2008/7/12 Andreas Mähler <an...@stud.uni-karlsruhe.de>:
>> Hello again,
>>
>> Gabriel Belingueres schrieb:
>>>
>>> Without knowing what are you actually trying to accomplish with this
>>> interceptor, I would warn that when an interceptor messes up with the
>>> parameters, you need to carefully think where in the interceptor chain
>>> this interceptor will execute.
>>
>> I explain what I am trying to do in my first post[1].
>>
>> What do you mean by messing up with the parameters? The Interceptor only
>> checks for the presence of at least one of the specified parameters. It
>> does
>> not change anything. Are there any interceptors that actually change the
>> parameter map? As I said, I am a S2 newbie and haven't heard of many
>> things,
>> but I would consider it as bad style.
>>
>> Greetings,
>> ~Andreas
>>
>>
>> [1] http://article.gmane.org/gmane.comp.jakarta.struts.user/158395
>>
>>
>> ---------------------------------------------------------------------
>> 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
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/-S2--Recipe-for-Action-and-View-tp18381617p18438070.html
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: [S2] Recipe for Action and View

Posted by Andreas Mähler <an...@stud.uni-karlsruhe.de>.
Hi !

Gabriel Belingueres schrieb:
> The problem you want to solve is very common (skipping form validation
> the first time you call an action)

Aah - I thought so - that's why I was asking for a recipe.

> and, as Dave wrote, it is commonly
> solved by invoking the action with the input() method [1]. This method
> will do nothing and will return INPUT as the result, thus forwarding
> the response to the page with the form.

You guys are right - that is a better solution.
Sorry Dave - I did not get it at first, but that's the thing I was 
looking for :-)

BTW: I can't rely on distinguishing between GET and POST, as I use GET 
also in forms when it doesn't change the state on the server (e.g. a 
simple search form). I'm sometimes annoyed by webapps that use POST for 
all forms, because then my browser comes up with this "Are you sure?" 
message whenever I want to refresh the result page.


Thanx alot!
~Andreas


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


Re: [S2] Recipe for Action and View

Posted by Gabriel Belingueres <be...@gmail.com>.
Sorry it seems I entered the conversation later and didn't saw that email.

The problem you want to solve is very common (skipping form validation
the first time you call an action) and, as Dave wrote, it is commonly
solved by invoking the action with the input() method [1]. This method
will do nothing and will return INPUT as the result, thus forwarding
the response to the page with the form.

[1] http://struts.apache.org/2.1.2/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html#input()

2008/7/12 Andreas Mähler <an...@stud.uni-karlsruhe.de>:
> Hello again,
>
> Gabriel Belingueres schrieb:
>>
>> Without knowing what are you actually trying to accomplish with this
>> interceptor, I would warn that when an interceptor messes up with the
>> parameters, you need to carefully think where in the interceptor chain
>> this interceptor will execute.
>
> I explain what I am trying to do in my first post[1].
>
> What do you mean by messing up with the parameters? The Interceptor only
> checks for the presence of at least one of the specified parameters. It does
> not change anything. Are there any interceptors that actually change the
> parameter map? As I said, I am a S2 newbie and haven't heard of many things,
> but I would consider it as bad style.
>
> Greetings,
> ~Andreas
>
>
> [1] http://article.gmane.org/gmane.comp.jakarta.struts.user/158395
>
>
> ---------------------------------------------------------------------
> 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: [S2] Recipe for Action and View

Posted by Dave Newton <ne...@yahoo.com>.
--- On Sat, 7/12/08, Andreas Mähler wrote:
> Are there any interceptors that actually change the parameter map? 
> As I said, I am a S2 newbie and haven't heard of many things, but I 
> would consider it as bad style.

As an example, the checkbox interceptor removes the hidden checkbox parameters from the map to avoid an attempt to set them on the action or its results, instead munging the hidden field name to get the checkbox property name.

I don't think this is particularly typical, but it is kind of cool, in a treacherous kind of way. Mucking with the parameter map can do some interesting things under controlled circumstances.

Dave


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


Re: [S2] Recipe for Action and View

Posted by Andreas Mähler <an...@stud.uni-karlsruhe.de>.
Hello again,

Gabriel Belingueres schrieb:
> Without knowing what are you actually trying to accomplish with this
> interceptor, I would warn that when an interceptor messes up with the
> parameters, you need to carefully think where in the interceptor chain
> this interceptor will execute.

I explain what I am trying to do in my first post[1].

What do you mean by messing up with the parameters? The Interceptor only 
checks for the presence of at least one of the specified parameters. It 
does not change anything. Are there any interceptors that actually 
change the parameter map? As I said, I am a S2 newbie and haven't heard 
of many things, but I would consider it as bad style.

Greetings,
~Andreas


[1] http://article.gmane.org/gmane.comp.jakarta.struts.user/158395


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


Re: [S2] Recipe for Action and View

Posted by Gabriel Belingueres <be...@gmail.com>.
Without knowing what are you actually trying to accomplish with this
interceptor, I would warn that when an interceptor messes up with the
parameters, you need to carefully think where in the interceptor chain
this interceptor will execute.

2008/7/11 Andreas Mähler <an...@stud.uni-karlsruhe.de>:
> Hello Gabriel,
>
> yes, I indeed took that into consideration, but if i am not wrong, there are
> two drawbacks:
>
> 1: When I create my own base class, and would like to use the prepare-method
> for something else, too, I need to overwrite the old method and mustn't
> forget to call super.prepare() in my implementation. IMO, this is a likely
> source of bugs.
>
> 2: I would have had to mess with the defaultStack anyway, since prepare is
> located before some other important interceptors (e.g. i18n) so I would have
> lost this functionality by taking an "early shortcut".
>
> The Interceptor was done in 10mins and as far as i could test it, it seems
> to work fine.
>
> public class SubmitInterceptor implements Interceptor
> {
>        public static final String INPUT = "input";
>
>        public void destroy() {}
>        public void init() {}
>
>
>        private String paramNames;
>
>        public void setParamNames( String paramNames )
>        {
>                this.paramNames = paramNames;
>        }
>
>
>        public String intercept( ActionInvocation ai ) throws Exception
>        {
>                Map params = ai.getInvocationContext().getParameters();
>
>                String[] paramNames = this.paramNames.split( "," );
>
>                for( String param : paramNames )
>                {
>                        if( params.get( param.trim() ) != null )
>                                return ai.invoke();
>                }
>
>                return INPUT;
>        }
> }
>
> I created a new interceptor stack for my webapp that is like defaultStack,
> but with the SubmitInterceptor inserted right before conversionError:
>
> <interceptor-ref name="submit">
>        <param name="paramNames">submit</param>
> </interceptor-ref>
>
> As for the view, i am currently setting the "submit" parameter (as
> configured to be looked for above) for the Button:
>
> <s:submit name="submit" label="OK" value="true" type="button" />
>
> Comments and suggestions are welcome :-)
>
> ~Andreas
>
>
>
> Gabriel Belingueres schrieb:
>>
>> You can also opt for sharing the prepare() code through inheritance,
>> meaning that you create a superclass action holding the prepare()
>> code, instead of coding a custom interceptor.
>>
>> 2008/7/11 Andreas Mähler <an...@stud.uni-karlsruhe.de>:
>>>
>>> Thanks for your Answer, Lukasz.
>>>
>>> I already know the PrepareInterceptor and it can be used for what I would
>>> like to do. But I consider a special interceptor more convinient, since I
>>> intend to use it for more than one pair of view and action. (Using
>>> PrepareInterceptor would imply the need to add the code to every single
>>> action.)
>
>
> ---------------------------------------------------------------------
> 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: [S2] Recipe for Action and View

Posted by Dave Newton <ne...@yahoo.com>.
Personally I'd either map an action to the input method or differentiate based on GET/POST requests, but that's just personal preference.

Dave

--- On Fri, 7/11/08, Andreas Mähler <an...@stud.uni-karlsruhe.de> wrote:

> From: Andreas Mähler <an...@stud.uni-karlsruhe.de>
> Subject: Re: [S2] Recipe for Action and View
> To: user@struts.apache.org
> Date: Friday, July 11, 2008, 10:10 PM
> Hello Gabriel,
> 
> yes, I indeed took that into consideration, but if i am not
> wrong, there 
> are two drawbacks:
> 
> 1: When I create my own base class, and would like to use
> the 
> prepare-method for something else, too, I need to overwrite
> the old 
> method and mustn't forget to call super.prepare() in my
> implementation. 
> IMO, this is a likely source of bugs.
> 
> 2: I would have had to mess with the defaultStack anyway,
> since prepare 
> is located before some other important interceptors (e.g.
> i18n) so I 
> would have lost this functionality by taking an "early
> shortcut".
> 
> The Interceptor was done in 10mins and as far as i could
> test it, it 
> seems to work fine.
> 
> public class SubmitInterceptor implements Interceptor
> {
> 	public static final String INPUT = "input";
> 
> 	public void destroy() {}
> 	public void init() {}
> 
> 
> 	private String paramNames;
> 	
> 	public void setParamNames( String paramNames )
> 	{
> 		this.paramNames = paramNames;
> 	}	
> 	
> 
> 	public String intercept( ActionInvocation ai ) throws
> Exception
> 	{
> 		Map params = ai.getInvocationContext().getParameters();
> 
> 		String[] paramNames = this.paramNames.split(
> "," );
> 		
> 		for( String param : paramNames )
> 		{
> 			if( params.get( param.trim() ) != null )
> 				return ai.invoke();
> 		}
> 		
> 		return INPUT;
> 	}
> }
> 
> I created a new interceptor stack for my webapp that is
> like 
> defaultStack, but with the SubmitInterceptor inserted right
> before 
> conversionError:
> 
> <interceptor-ref name="submit">
> 	<param
> name="paramNames">submit</param>
> </interceptor-ref>
> 
> As for the view, i am currently setting the
> "submit" parameter (as 
> configured to be looked for above) for the Button:
> 
> <s:submit name="submit" label="OK"
> value="true" type="button" />
> 
> Comments and suggestions are welcome :-)
> 
> ~Andreas
> 
> 
> 
> Gabriel Belingueres schrieb:
> > You can also opt for sharing the prepare() code
> through inheritance,
> > meaning that you create a superclass action holding
> the prepare()
> > code, instead of coding a custom interceptor.
> > 
> > 2008/7/11 Andreas Mähler
> <an...@stud.uni-karlsruhe.de>:
> >> Thanks for your Answer, Lukasz.
> >>
> >> I already know the PrepareInterceptor and it can
> be used for what I would
> >> like to do. But I consider a special interceptor
> more convinient, since I
> >> intend to use it for more than one pair of view
> and action. (Using
> >> PrepareInterceptor would imply the need to add the
> code to every single
> >> action.)
> 
> 
> ---------------------------------------------------------------------
> 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: [S2] Recipe for Action and View

Posted by Andreas Mähler <an...@stud.uni-karlsruhe.de>.
Hello Gabriel,

yes, I indeed took that into consideration, but if i am not wrong, there 
are two drawbacks:

1: When I create my own base class, and would like to use the 
prepare-method for something else, too, I need to overwrite the old 
method and mustn't forget to call super.prepare() in my implementation. 
IMO, this is a likely source of bugs.

2: I would have had to mess with the defaultStack anyway, since prepare 
is located before some other important interceptors (e.g. i18n) so I 
would have lost this functionality by taking an "early shortcut".

The Interceptor was done in 10mins and as far as i could test it, it 
seems to work fine.

public class SubmitInterceptor implements Interceptor
{
	public static final String INPUT = "input";

	public void destroy() {}
	public void init() {}


	private String paramNames;
	
	public void setParamNames( String paramNames )
	{
		this.paramNames = paramNames;
	}	
	

	public String intercept( ActionInvocation ai ) throws Exception
	{
		Map params = ai.getInvocationContext().getParameters();

		String[] paramNames = this.paramNames.split( "," );
		
		for( String param : paramNames )
		{
			if( params.get( param.trim() ) != null )
				return ai.invoke();
		}
		
		return INPUT;
	}
}

I created a new interceptor stack for my webapp that is like 
defaultStack, but with the SubmitInterceptor inserted right before 
conversionError:

<interceptor-ref name="submit">
	<param name="paramNames">submit</param>
</interceptor-ref>

As for the view, i am currently setting the "submit" parameter (as 
configured to be looked for above) for the Button:

<s:submit name="submit" label="OK" value="true" type="button" />

Comments and suggestions are welcome :-)

~Andreas



Gabriel Belingueres schrieb:
> You can also opt for sharing the prepare() code through inheritance,
> meaning that you create a superclass action holding the prepare()
> code, instead of coding a custom interceptor.
> 
> 2008/7/11 Andreas Mähler <an...@stud.uni-karlsruhe.de>:
>> Thanks for your Answer, Lukasz.
>>
>> I already know the PrepareInterceptor and it can be used for what I would
>> like to do. But I consider a special interceptor more convinient, since I
>> intend to use it for more than one pair of view and action. (Using
>> PrepareInterceptor would imply the need to add the code to every single
>> action.)


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


Re: [S2] Recipe for Action and View

Posted by Gabriel Belingueres <be...@gmail.com>.
You can also opt for sharing the prepare() code through inheritance,
meaning that you create a superclass action holding the prepare()
code, instead of coding a custom interceptor.

2008/7/11 Andreas Mähler <an...@stud.uni-karlsruhe.de>:
> Thanks for your Answer, Lukasz.
>
> I already know the PrepareInterceptor and it can be used for what I would
> like to do. But I consider a special interceptor more convinient, since I
> intend to use it for more than one pair of view and action. (Using
> PrepareInterceptor would imply the need to add the code to every single
> action.)
>
> In my original post, i was wondering if there already is some kind of
> "recipe"/"best practice"/"standard way" since I don't think that my wish to
> put the actions together is very exotic.
>
> But as nobody intervened I take this as a sign that my design is not flawed
> :-))
>
> See You!
> ~Andreas
>
>
> Lukasz Lenart schrieb:
>>
>> Check PrepareInterceptor [1] with prepare() method
>>
>> [1] http://struts.apache.org/2.1.2/docs/prepare-interceptor.html
>
>
> ---------------------------------------------------------------------
> 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: [S2] Recipe for Action and View

Posted by Andreas Mähler <an...@stud.uni-karlsruhe.de>.
Thanks for your Answer, Lukasz.

I already know the PrepareInterceptor and it can be used for what I 
would like to do. But I consider a special interceptor more convinient, 
since I intend to use it for more than one pair of view and action. 
(Using PrepareInterceptor would imply the need to add the code to every 
single action.)

In my original post, i was wondering if there already is some kind of 
"recipe"/"best practice"/"standard way" since I don't think that my wish 
to put the actions together is very exotic.

But as nobody intervened I take this as a sign that my design is not 
flawed :-))

See You!
~Andreas


Lukasz Lenart schrieb:
> Check PrepareInterceptor [1] with prepare() method
> 
> [1] http://struts.apache.org/2.1.2/docs/prepare-interceptor.html


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


Re: [S2] Recipe for Action and View

Posted by Lukasz Lenart <lu...@googlemail.com>.
Hi,

Check PrepareInterceptor [1] with prepare() method

[1] http://struts.apache.org/2.1.2/docs/prepare-interceptor.html


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

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