java Struts2 排除模式 - 它究竟是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13496504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Struts2 exclude pattern - How exactly does it work?
提问by Zachary Carter
I'm trying to migrate a Struts2 app (version 2.2.1.1) to Spring MVC and I'm having a tough time getting the struts.xml mappings converted over to SpringMVC servlet mappings.
我正在尝试将 Struts2 应用程序(版本 2.2.1.1)迁移到 Spring MVC,但我很难将 struts.xml 映射转换为 SpringMVC servlet 映射。
My first question is how exactly the Struts2 exclude pattern works. Let's say in my web.xml I have a filter / mapping for struts2 set up as follows :
我的第一个问题是 Struts2 排除模式究竟是如何工作的。假设在我的 web.xml 中,我为 struts2 设置了一个过滤器/映射,如下所示:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
In my struts.xml I have a bunch of actions defined which are already working. Now from my understanding, based on the struts2 documentation -
在我的 struts.xml 中,我定义了一堆已经在工作的动作。现在根据我的理解,基于 struts2 文档 -
(Since Struts 2.1.7, you are able to provide a comma seperated list of patterns for which when matching against the request URL the Filter will just pass by. This is done via the configuration option struts.action.excludePattern, for example in your struts.xml) -
(从 Struts 2.1.7 开始,您可以提供逗号分隔的模式列表,当与请求 URL 匹配时,过滤器将通过该列表。这是通过配置选项 struts.action.excludePattern 完成的,例如在您的struts.xml) -
if I add a exclude pattern such as :
如果我添加一个排除模式,例如:
<constant name="struts.action.excludePattern" value="/*"/>
Then the filter should be bypassed and the actions mentioned above shouldn't resolve, correct?
那么过滤器应该被绕过并且上面提到的操作不应该解决,对吗?
For some reason this isn't happening and all my actions are still being routed appropriately.
出于某种原因,这并没有发生,我所有的动作仍然被适当地路由。
What gives?
是什么赋予了?
回答by renaud91
The struts value must respect the java.util.regex.Pattern class standard. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
struts 值必须遵守 java.util.regex.Pattern 类标准。 http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
So, if you want to avoid all /rest/* url for example:
所以,如果你想避免所有 /rest/* url,例如:
/rest/[a-zA-Z_0-9]*
If, you need to handle a rest web service with multiple parameters:
如果,您需要处理具有多个参数的休息 Web 服务:
/rest/[a-zA-Z_0-9]*/?[a-zA-Z_0-9]*/?[a-zA-Z_0-9]*/?[a-zA-Z_0-9]*/?[a-zA-Z_0-9]*
In order to test your pattern, you can use the following code:
为了测试您的模式,您可以使用以下代码:
Pattern pattern = Pattern.compile("/rest/[a-zA-Z_0-9]*/?[a-zA-Z_0-9]*/?[a-zA-Z_0-9]*/?[a-zA-Z_0-9]*/?[a-zA-Z_0-9]*");
String uri = "/rest/users/1/1";
System.out.println(pattern.matcher(uri).matches());
回答by Dave Newton
The exclude pattern is a regex.
排除模式是一个正则表达式。
If you want to exclude everything, you'd want .*
.
如果你想排除一切,你会想要.*
.
http://struts.apache.org/2.x/docs/webxml.html#web.xml-SimpleExample
http://struts.apache.org/2.x/docs/webxml.html#web.xml-SimpleExample
I don't know exactly whyyou'd want that, since you could just remove the S2 filter.
我不知道你为什么想要那个,因为你可以删除 S2 过滤器。
回答by mana
I got an addition to the previous answer. You can define excludes as stated here.
我得到了之前的答案的补充。您可以按此处所述定义排除项。
But there seems to be a mistake in the documentation. If you need to escape a dot '.' you have to use only a singe backslash:
但是文档中似乎有一个错误。如果你需要转义一个点 '.' 你只需要使用一个反斜杠:
<struts>
<constant name="struts.action.excludePattern" value=".*unfiltered.*,.*\.nofilter"/>
...
</struts>