How to set refreshModelBeforeResult in ModelDriven interceptor?

回眸只為那壹抹淺笑 提交于 2019-12-13 12:18:12

问题


I am planning to use refreshModelBeforeResult as suggested in Struts2 Documentation, however I am confused whether this property can be set in Action class or struts.xml. Is there is anything apart from what I have tried below

<action name="myAction" class="com.stuff.MyActionClass" method="myMethod">
        <result name="myHome" type="tiles">MyHome</result>

        <interceptor-ref name="basicStack" />
        <interceptor-ref name="params"/>  
        <interceptor-ref name="modelDriven"> 
          <param name="refreshModelBeforeResult">true</param> 
        </interceptor-ref>
</action>

回答1:


You have asked

I am confused whether this property can be set in Action class or struts.xml

This property is called a parameter, and it could be used to parametrize the interceptor configuration or when overriding the interceptors in action configuration. Like you did or like the example in the doc link that you referenced.

<action name="someAction" class="com.examples.SomeAction">
    <interceptor-ref name="modelDriven"> 
      <param name="refreshModelBeforeResult">true</param> 
    </interceptor-ref>
    <interceptor-ref name="basicStack"/>
    <result name="success">good_result.ftl</result>
</action>

In this example the interceptors configuration is overridden that means only those interceptors that you have used with interceptor-ref tag will be configured.

You can't set this property to the action because it's an interceptor parameter, and not the action parameter. Interceptors are singletons and use only static parameters by the configuration which is built when a dispatcher is initialized (on start up).

Is there is anything apart from what I have tried

Yes, you have use the interceptors in the different order. The order is important when invoking an interceptor's chain during action invocation. Because interceptors might be dependent on execution of each other. If you set modelDriven interceptor after the basicStack some interceptors like params which is included in this stack might not work. Also you are included params interceptor twice. And it will be executed twice, worse it does it before the model is pushed on the value stack. So, the http parameters might not be set to the model because model driven action requires the model being pushed to the value stack before params interceptor is invoked.



来源:https://stackoverflow.com/questions/27960534/how-to-set-refreshmodelbeforeresult-in-modeldriven-interceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!