Using * in regular expression in the action name of struts2

497 Views Asked by At

My url may or may not have a certain parameter and I set the action class for such a url as follows :

<action name="{paramOne}/{paramTwo:myparam*}/details" 
     class="myaction"  
     method="execute"> 
     <result name="success">/mypage.jsp</result>
  </action>

So the url something/myparam/details is working but when i try to invoke something/details (which according to struts.xml should work). It shows that there is not action class mapped.

2

There are 2 best solutions below

2
Fei Xia On

your code '*' only can match between "{paramOne}/" and "/details" so you must have something between "{paramOne}/" and "/details"

4
fustaki On

You want to route different URI patterns to the same action, this is exactly the case action mapping is made for. I think the best solution is to write two separate statements.

If you don't want to repeat the "code" inside the <action> tag you can chain it (it's a kind of internal redirect, a kind of aliasing)

<action name="secondpattern" class="com.opensymphony.xwork2.ActionSupport">
    <result type="chain">firstpattern</result>
</action>

https://struts.apache.org/docs/action-chaining.html