【发布时间】:2013-10-29 23:50:01
【问题描述】:
来自文档:Struts2's Advanced Wildcard Mappings:
高级通配符
从 2.1.9+ 开始可以在动作中定义正则表达式 姓名。要使用这种形式的通配符,以下常量必须是 设置:
<constant name="struts.enable.SlashesInActionNames" value="true"/> <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/> <constant name="struts.patternMatcher" value="regex" />正则表达式可以有两种形式,最简单的一种是
{FIELD_NAME},在这种情况下,带有FIELD_NAME的字段在 操作将填充匹配的文本,例如:<package name="books" extends="struts-default" namespace="/"> <action name="/{type}/content/{title}" class="example.BookAction"> <result>/books/content.jsp</result> </action> </package>在本例中,如果 url
/fiction/content/Frankenstein是 请求时,BookAction 的字段“type”将设置为“fiction”,并且 字段“title”将设置为“Frankenstein”。
这绝对很棒,如果您在常规 Action 方法中读取这些变量(例如 execute()),则可以正常工作。
如果您尝试从prepare() 方法中读取它们,则它们为空,因为PrepareInterceptor 在其他负责设置参数的拦截器之前运行;解决此问题的常用方法是使用适当的拦截器堆栈来获取执行prepare() 方法时已填充的参数...
<!-- 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. -->
这适用于来自页面的参数,但它不适用于高级通配符设置的参数。它们仍然为空。
如何解决这个问题?
【问题讨论】:
标签: java xml regex struts2 interceptor