You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by meeboo <de...@gmail.com> on 2008/03/22 13:11:38 UTC

Running a prepare method before the assigned action method

Hi all

I have a class for several actions and each action is mapped to a method,
like this:

<action name="update/modelObject" 
           class="net.myapp.web.modelObjectActions"
           method="update">
<result>/index.jsp</result>
</action>

The above action will fire when a form is submitted and the modelObject
properties are populated via the form fields. Now that I want is to invoke a
prepare method before the update method so that I first can read the model
object from the database and then populate its fields. I did implement
preparable in modelObjectActions but then the prepare method will be invoked
before all actions in that class, I want it to be invoked just for this
specific action. 

Any ideas?

Thanks
-- 
View this message in context: http://www.nabble.com/Running-a-prepare-method-before-the-assigned-action-method-tp16219260p16219260.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: Running a prepare method before the assigned action method

Posted by meeboo <de...@gmail.com>.
It seems as if the preparable method can't parse parameters if they are in
the following format

update/modelObject/10.action (where 10.action is the primary key)

where as it works fine when using action urls like this
update/modelObject?10.action

Is this expected behavior? 


meeboo wrote:
> 
> Hi all
> 
> I have a class for several actions and each action is mapped to a method,
> like this:
> 
> <action name="update/modelObject" 
>            class="net.myapp.web.modelObjectActions"
>            method="update">
> <result>/index.jsp</result>
> </action>
> 
> The above action will fire when a form is submitted and the modelObject
> properties are populated via the form fields. Now that I want is to invoke
> a prepare method before the update method so that I first can read the
> model object from the database and then populate its fields. I did
> implement preparable in modelObjectActions but then the prepare method
> will be invoked before all actions in that class, I want it to be invoked
> just for this specific action. 
> 
> Any ideas?
> 
> Thanks
> 

-- 
View this message in context: http://www.nabble.com/Running-a-prepare-method-before-the-assigned-action-method-tp16219260p16222867.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: Running a prepare method before the assigned action method

Posted by meeboo <de...@gmail.com>.
Ahh nevermind the first mail, I managed to work around it. 

I still have a problem of not being able to access parameters via my prepare
method though. Here's my action class:

public class UserActions extends StrutsSupport implements Preparable {

   public void prepare()
   {
	if(id != null) //property is located in superclass StrutsSupport
		user= appManager.readUser(Long.valueOf(id));
   }

}

and the struts mapping:
<action name="update/user/*" 
        		class="net.myapp.web.UserActions">
     <interceptor-ref name="paramsPrepareParamsStack"/>	
     {1}
     <result>/models/user/update.jsp</result>
</action>

The whole flow is that when you access the above action the user object is
read and its values populate the form in update.jsp. But the id property is
never set so the form is never populated. I even tried setting
paramsPrepareParamsStack as the default stack but with no luck. 


meeboo wrote:
> 
> Hi all
> 
> I have a class for several actions and each action is mapped to a method,
> like this:
> 
> <action name="update/modelObject" 
>            class="net.myapp.web.modelObjectActions"
>            method="update">
> <result>/index.jsp</result>
> </action>
> 
> The above action will fire when a form is submitted and the modelObject
> properties are populated via the form fields. Now that I want is to invoke
> a prepare method before the update method so that I first can read the
> model object from the database and then populate its fields. I did
> implement preparable in modelObjectActions but then the prepare method
> will be invoked before all actions in that class, I want it to be invoked
> just for this specific action. 
> 
> Any ideas?
> 
> Thanks
> 

-- 
View this message in context: http://www.nabble.com/Running-a-prepare-method-before-the-assigned-action-method-tp16219260p16220742.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: [struts] Running a prepare method before the assigned action method

Posted by Dale Newfield <Da...@Newfield.org>.
meeboo wrote:
> Still no solution to this problem. How come parameters in actions with
> wildcards (for example myapp/modelObject/*) aren't parsed in the prepare
> method? They work fine when I parse them in my assigned action method :(

If you're using 2.1, there are now a number of ways in which parameters 
can be set on your action, each with separate interceptors:
params
actionMappingParams
staticParams

You might need to update your interceptor stack.

-Dale

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


Re: Running a prepare method before the assigned action method

Posted by Kedar Choudhary <ke...@helixtechsolutions.com>.
meeboo wrote:
> Still no solution to this problem. How come parameters in actions with
> wildcards (for example myapp/modelObject/*) aren't parsed in the prepare
> method? They work fine when I parse them in my assigned action method :(
>
>
> meeboo wrote:
>   
>> Hey again
>>
>> I'm posting via nabble, and it seems to strip out my wildcards, basically
>> my action looks like this now
>>
>> <action name="update/modelObject/*"
>> class="net.schemer.web.ObjectModelActions">
>>      <interceptor-ref name="paramsPrepareParamsStack"/>		
>>          < param name= " id " > { 1 } 
>>     <result>/models/objectModel/update.jsp</result>
>> </action>
>>
>> Hopefully this is viewable. 
>>
>>
>>     

It is not really an issue with wildcard mapping. 
Issue is with the way parameters are being set on your action class.
Your parameters are being set not by params interceptor but by staticParams interceptor.
StaticParams interceptor handles setting the parameters declared in your action mapping xml.
paramsPrepareParamsStack has staticParams interceptor after prepare, hence the issue. 

To get it working prepare an interceptor stack similar to paramsPrepareParams, replace initial params with staticParams.

Regards
Kedar 



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


Re: Running a prepare method before the assigned action method

Posted by meeboo <de...@gmail.com>.
Still no solution to this problem. How come parameters in actions with
wildcards (for example myapp/modelObject/*) aren't parsed in the prepare
method? They work fine when I parse them in my assigned action method :(


meeboo wrote:
> 
> Hey again
> 
> I'm posting via nabble, and it seems to strip out my wildcards, basically
> my action looks like this now
> 
> <action name="update/modelObject/*"
> class="net.schemer.web.ObjectModelActions">
>      <interceptor-ref name="paramsPrepareParamsStack"/>		
>          < param name= " id " > { 1 } 
>     <result>/models/objectModel/update.jsp</result>
> </action>
> 
> Hopefully this is viewable. 
> 
> 
> newton.dave wrote:
>> 
>> --- meeboo <de...@gmail.com> wrote:
>>> won't work with a wildcard parameter mapping Dave
>>> 
>>> <action name="update/modelObject/*"
>>>         class="net.myapp.web.ModelObjectActions"
>>>         method="update">
>> 
>> Your first post didn't have a wildcard mapping.
>> 
>>> The parameter simply isn't parsed in the prepare method unless it's in
>>> the
>>> form of update/modelObject.action?id=22
>> 
>> Do you mean parsed by the parameter interceptor?
>> 
>>> this is what I'm currently doing
>>> update/modelObject/22.action
>>> 
>>> The parameter is parsed in the update method, but not in the prepare
>>> method
>> 
>> Are you using the standard action mapper? Any plugins?
>> 
>> The mapping you posted in your follow-up message did not come through
>> properly; please make sure you're sending plain-text messages. This is
>> what I
>> saw:
>> 
>>> <action name="update/user/*" 
>>>         		class="net.myapp.web.UserActions">
>>>      <interceptor-ref name="paramsPrepareParamsStack"/>	
>>>      {1}
>>>      <result>/models/user/update.jsp</result>
>>> </action>
>> 
>> I don't really know what you're trying to do or how you're going about
>> trying
>> to do it.
>> 
>> Dave
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> 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/Running-a-prepare-method-before-the-assigned-action-method-tp16219260p16274676.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: Running a prepare method before the assigned action method

Posted by meeboo <de...@gmail.com>.
Hey again

I'm posting via nabble, and it seems to strip out my wildcards, basically my
action looks like this now

<action name="update/modelObject/*"
class="net.schemer.web.ObjectModelActions">
     <interceptor-ref name="paramsPrepareParamsStack"/>		
         < param name= " id " > { 1 } 
    <result>/models/objectModel/update.jsp</result>
</action>

Hopefully this is viewable. 


newton.dave wrote:
> 
> --- meeboo <de...@gmail.com> wrote:
>> won't work with a wildcard parameter mapping Dave
>> 
>> <action name="update/modelObject/*"
>>         class="net.myapp.web.ModelObjectActions"
>>         method="update">
> 
> Your first post didn't have a wildcard mapping.
> 
>> The parameter simply isn't parsed in the prepare method unless it's in
>> the
>> form of update/modelObject.action?id=22
> 
> Do you mean parsed by the parameter interceptor?
> 
>> this is what I'm currently doing
>> update/modelObject/22.action
>> 
>> The parameter is parsed in the update method, but not in the prepare
>> method
> 
> Are you using the standard action mapper? Any plugins?
> 
> The mapping you posted in your follow-up message did not come through
> properly; please make sure you're sending plain-text messages. This is
> what I
> saw:
> 
>> <action name="update/user/*" 
>>         		class="net.myapp.web.UserActions">
>>      <interceptor-ref name="paramsPrepareParamsStack"/>	
>>      {1}
>>      <result>/models/user/update.jsp</result>
>> </action>
> 
> I don't really know what you're trying to do or how you're going about
> trying
> to do it.
> 
> Dave
> 
> 
> 
> ---------------------------------------------------------------------
> 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/Running-a-prepare-method-before-the-assigned-action-method-tp16219260p16227199.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: Running a prepare method before the assigned action method

Posted by Dave Newton <ne...@yahoo.com>.
--- meeboo <de...@gmail.com> wrote:
> won't work with a wildcard parameter mapping Dave
> 
> <action name="update/modelObject/*"
>         class="net.myapp.web.ModelObjectActions"
>         method="update">

Your first post didn't have a wildcard mapping.

> The parameter simply isn't parsed in the prepare method unless it's in the
> form of update/modelObject.action?id=22

Do you mean parsed by the parameter interceptor?

> this is what I'm currently doing
> update/modelObject/22.action
> 
> The parameter is parsed in the update method, but not in the prepare method

Are you using the standard action mapper? Any plugins?

The mapping you posted in your follow-up message did not come through
properly; please make sure you're sending plain-text messages. This is what I
saw:

> <action name="update/user/*" 
>         		class="net.myapp.web.UserActions">
>      <interceptor-ref name="paramsPrepareParamsStack"/>	
>      {1}
>      <result>/models/user/update.jsp</result>
> </action>

I don't really know what you're trying to do or how you're going about trying
to do it.

Dave



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


Re: Running a prepare method before the assigned action method

Posted by meeboo <de...@gmail.com>.
won't work with a wildcard parameter mapping Dave

<action name="update/modelObject/*"
        class="net.myapp.web.ModelObjectActions"
        method="update">

The parameter simply isn't parsed in the prepare method unless it's in the
form of update/modelObject.action?id=22

this is what I'm currently doing
update/modelObject/22.action

The parameter is parsed in the update method, but not in the prepare method
(i've tried using the 


newton.dave wrote:
> 
> --- meeboo <de...@gmail.com> wrote:
>> I have a class for several actions and each action is mapped to a method,
>> like this:
>> 
>> <action name="update/modelObject" 
>>            class="net.myapp.web.modelObjectActions"
>>            method="update">
>> <result>/index.jsp</result>
>> </action>
>> 
>> The above action will fire when a form is submitted and the modelObject
>> properties are populated via the form fields. Now that I want is to
>> invoke
>> a
>> prepare method before the update method so that I first can read the
>> model
>> object from the database and then populate its fields. I did implement
>> preparable in modelObjectActions but then the prepare method will be
>> invoked
>> before all actions in that class, I want it to be invoked just for this
>> specific action. 
>> 
>> Any ideas?
> 
> prepareUpdate.
> 
> http://struts.apache.org/2.x/docs/prepare-interceptor.html
> 
> Dave
> 
> 
> ---------------------------------------------------------------------
> 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/Running-a-prepare-method-before-the-assigned-action-method-tp16219260p16225398.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: Running a prepare method before the assigned action method

Posted by Dave Newton <ne...@yahoo.com>.
--- meeboo <de...@gmail.com> wrote:
> I have a class for several actions and each action is mapped to a method,
> like this:
> 
> <action name="update/modelObject" 
>            class="net.myapp.web.modelObjectActions"
>            method="update">
> <result>/index.jsp</result>
> </action>
> 
> The above action will fire when a form is submitted and the modelObject
> properties are populated via the form fields. Now that I want is to invoke
> a
> prepare method before the update method so that I first can read the model
> object from the database and then populate its fields. I did implement
> preparable in modelObjectActions but then the prepare method will be
> invoked
> before all actions in that class, I want it to be invoked just for this
> specific action. 
> 
> Any ideas?

prepareUpdate.

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

Dave


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