gpt4 book ai didi

java - 在 Prepare() 方法中找不到高级通配符映射参数

转载 作者:行者123 更新时间:2023-11-30 07:13:21 24 4
gpt4 key购买 nike

来自文档:Struts2's Advanced Wildcard Mappings :

Advanced Wildcards

From 2.1.9+ regular expressions can be defined defined in the action name. To use this form of wild card, the following constants must be set:

<constant name="struts.enable.SlashesInActionNames" value="true"/> 
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.patternMatcher" value="regex" />

The regular expressions can be in two forms, the simplest one is {FIELD_NAME}, in which case the field with the FIELD_NAME in the action will be populated with the matched text, for example:

<package name="books" extends="struts-default" namespace="/">
<action name="/{type}/content/{title}" class="example.BookAction">
<result>/books/content.jsp</result>
</action>
</package>

In this example, if the url /fiction/content/Frankenstein is requested, BookAction's field "type" will be set to "fiction", and the field "title" will be set to "Frankenstein".

这绝对很棒,如果您在常规 Action 方法(如 execute())中读取这些变量,则效果很好。

如果您尝试从 prepare() 方法读取它们,它们为空,因为 PrepareInterceptor 在负责设置参数的其他拦截器之前运行;解决这个问题的通常方法是使用适当的拦截器堆栈来获取在执行 prepare() 方法时已经填充的参数...

来自文档:ParamsPrepareParamStack

<!-- An example of the paramsPrepareParams trick. This stack
is exactly the same as the defaultStack, except that it
includes one extra interceptor before the prepare interceptor:
the params interceptor.

This is useful for when you wish to apply parameters directly
to an object that you wish to load externally (such as a DAO
or database or service layer), but can't load that object
until at least the ID parameter has been loaded. By loading
the parameters twice, you can retrieve the object in the
prepare() method, allowing the second params interceptor to
apply the values on the object. -->

这对来自页面的参数非常有效,但不适用于高级通配符设置的参数。它们仍然为空。

如何解决这个问题?

最佳答案

参数不是由 ParametersInterceptor 设置的(就像来自 JSP 的那些),但是 StaticParametersInterceptor
要在 prepare() 方法中填充它们,必须应用与 paramsPrepareParamsStack 相同的技巧。
由于没有开箱即用的拦截器堆栈,您必须定义它。
defaultStack开始,我们应该创建一个像这样堆叠:

<interceptor-stack name="allYourParamsAreBelongToUsStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="i18n"/>
<!-- THE TRICK: NOW PREPARE() WILL FIND EVERYTHING SET -->
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
</interceptor-ref>
<!-- END OF THE TRICK -->
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="debugging"/>
</interceptor-stack>

注意:ActionMappingParams 不是必需的,仅供将来使用。

如果您发现与此相关的任何问题,请发表评论。据我所知,它完美无缺。

关于java - 在 Prepare() 方法中找不到高级通配符映射参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19495332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com