简体   繁体   中英

How prepare interceptor works in struts2?

Please tell me how the prepare intercetor works I am using it now & found strage behaviour

prepare()
validate()
execute()

these are methods

so when i hit the request it called 3 times as

prepare()
validate()
execute()
prepare()
validate()
execute()
prepare()
validate()
execute()

I dont know whats the problem with it According to my understanding it should only run prepare method & show prepopulated data form & when user clicks on submit then it should submit the data.

Please explain

<action name="updatebusinessinfo" class="com.controller.UpdateBusinessDetails">

            <interceptor-ref name="params"/>
            <!--
            <interceptor-ref name="prepare"/> 

            <interceptor-ref name="basicStack"/>
            -->

            <interceptor-ref name="fileUpload">
                    <param name="maximumSize">2097152</param>
                    <param name="allowedTypes">image/png,image/gif,image/jpeg,image/pjpeg</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success">businessinfo.jsp</result>
            <result name="input">businessinfo.jsp</result>
            <result name="error">businessinfo.jsp</result>
        </action>

Yes friends i have made mistake in struts.xml file. Now please tell me how should i receive url parameter in prepare() method? http://www.myweb.com/updatebusinessinfo/23

i tried following but not working

<action name="updatebusinessinfo/*" class="com.controller.UpdateBusinessDetails">
<param name="id">{1}</param>

            <interceptor-ref name="params"/>
            <!--
            <interceptor-ref name="prepare"/> 

            <interceptor-ref name="basicStack"/>
            -->

            <interceptor-ref name="fileUpload">
                    <param name="maximumSize">2097152</param>
                    <param name="allowedTypes">image/png,image/gif,image/jpeg,image/pjpeg</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success">businessinfo.jsp</result>
            <result name="input">businessinfo.jsp</result>
            <result name="error">businessinfo.jsp</result>
        </action>

Just some hints about this issue that may come in handy to people looking for information about the Prepare Interceptor:

  • The DefaultStack in struts2 already includes Prepare interceptor, so if you include them both you will have 2 calls to prepare(). Normally you don't want that.
  • In DefaultStack the Prepare interceptor is called BEFORE the Params interceptor, so you won't have the request params in the prepare() method. If you wanted to do something with params there (fetch something from DB with an ID, for example) you won't be able.

Look here to see what the basic/default stack has in struts2: http://struts.apache.org/release/2.0.x/docs/struts-defaultxml.html

There is a "paramsPrepareParamsStack" thats uses Params before and after Prepare, so you have your params in prepare().

Well without any further information its really hard to tell what and why this is happening.we still expect from you to provides more details about what you are trying to do? what URL you are hitting, information about the configuration and any other information about your application.

Prepare method will only be called if you have implemented Preparable interface.Its in short a kind of init method which allow us to do any initialization work before the actual heavy work will start.

Prepare method will be called before your execute method.I suggest you to go through the working of Prepare method and how actually it is being called by the stack.

On a similar way S2 will call validate method of your action class if you have implemented it and will validate the data as per the implementation provided inside the method.

This is just a overview of the flow and i still suggest to provide more information of your context to get any good inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM