简体   繁体   English

准备拦截器如何在struts2中工作?

[英]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 所以当我点击请求时,它被调用了3次

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. 我不知道这是什么问题,根据我的理解,它应该只运行prepare方法并显示预填充的数据表格,并且当用户单击Submit时,它应该提交数据。

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. 是的朋友,我在struts.xml文件中犯了错误。 Now please tell me how should i receive url parameter in prepare() method? 现在,请告诉我我应该如何在prepare()方法中接收url参数? http://www.myweb.com/updatebusinessinfo/23 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(). struts2中的DefaultStack已经包含了Prepare拦截器,因此如果同时包含它们,则将有两个调用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. 在DefaultStack中,Prepare拦截器在Params拦截器之前被称为,因此prepare()方法中不会包含请求参数。 If you wanted to do something with params there (fetch something from DB with an ID, for example) you won't be able. 如果您想在其中使用参数进行操作(例如,从具有ID的DB中获取某些内容),则将无法执行。

Look here to see what the basic/default stack has in struts2: http://struts.apache.org/release/2.0.x/docs/struts-defaultxml.html 查看此处以了解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(). 有一个“ paramsPrepareParamsStack”,它在Prepare之前和之后使用Params,因此您可以在prepare()中使用params。

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. 您要访问的URL,有关配置的信息以及有关您应用程序的其他信息。

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. 仅当您实现了Preparable接口时才调用prepare方法。简而言之,它是一种init方法,它使我们可以在开始繁重的工作之前进行任何初始化工作。

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. 以类似的方式,S2将调用您的操作类的validate方法(如果已实现),并将根据方法内部提供的实现来验证数据。

This is just a overview of the flow and i still suggest to provide more information of your context to get any good inputs. 这只是流程的概述,我仍然建议您提供有关您的上下文的更多信息,以获得任何好的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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