Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts2 exclude pattern - How exactly does it work?

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.

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 :

<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 -

(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) -

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?

like image 446
Zachary Carter Avatar asked May 08 '26 10:05

Zachary Carter


2 Answers

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

I don't know exactly why you'd want that, since you could just remove the S2 filter.

like image 191
Dave Newton Avatar answered May 10 '26 00:05

Dave Newton


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

So, if you want to avoid all /rest/* url for example:

/rest/[a-zA-Z_0-9]*

If, you need to handle a rest web service with multiple parameters:

/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());
like image 31
renaud91 Avatar answered May 09 '26 23:05

renaud91



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!