3

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Zachary Carter
  • 373
  • 2
  • 10
  • 22

3 Answers3

6

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());
renaud91
  • 61
  • 1
  • 1
5

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Thank you for the answer Dave. I'm not trying to exclude everything, I'm trying to understand how the feature works. I'm migrating an app to Spring-MVC and I wanted to run Struts2 / Spring-MVC side by side. I figured out my problem was actually related to the struts filter I was using. I'll post details later. – Zachary Carter Nov 21 '12 at 20:52
0

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>
mana
  • 6,347
  • 6
  • 50
  • 70