Struts2 regex configuration

2019-06-23 16:03发布

问题:

I am creating a Struts2 web app.

I want an URL like www.xyz.com/portal/orgCode/signin, this orgCode is dynamic, so I am using regex for that.

I configure struts.xml like:

<constant name="struts.devMode" value="false" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.action.extension" value=",html,action" />
<constant name="struts.action.excludePattern" value=".*unfiltered.*,.*\\.nofilter" />
<constant name="struts.multipart.maxSize" value="2097152000"/>
<constant name="struts.enable.SlashesInActionNames" value="true"/> 
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.patternMatcher" value="regex" />

<package name="portal" namespace="/portal/{orgCode}" extends="default">
    <interceptors>
        <interceptor name="portalUrlInterceptor"
                    class="com.wtmit.service.portal.interceptor.PortalCommonInterceptor">
        </interceptor>
        <interceptor-stack name="commonPortalUrlInterceptor">
            <interceptor-ref name="portalUrlInterceptor" />
            <interceptor-ref name="defaultStack" />
        </interceptor-stack>
    </interceptors>

    <action name="signin" method="signin"
           class="com.wtmit.service.portal.action.PortalSignInAction">          
        <interceptor-ref name="commonPortalUrlInterceptor"></interceptor-ref>
        <result name="portalSignIn" type="tiles">portalSignIn</result>
        <result name="successLogin" type="redirect">home</result>

    </action>
</package>

this is working fine, but URLs like www.xyz.com/mainLogin is not working for the following configuration:

<package name="main" namespace="/" extends="default">
    <action name="*Login" method="{0}" class="com.service.user.action.LoginAction">
        <result name="login" type="tiles">login</result>
        <result name="successLogin" type="redirect">home</result>
    </action>
</package>

The error is

HTTP Status 404 - There is no Action mapped for namespace / and action name mainLogin.

回答1:

You are mixing Wildcards with Advanced Wildcards; instead of this:

<action name="*Login" method="{0}" class="com.service.user.action.LoginAction">

try using this:

<action name="/{method}/Login" method="{1}" class="com.service.user.action.LoginAction">

Related reads:

  • Creating dynamic URL with 2 action parameters in Struts 2 using OGNL
  • Advanced Wildcard Mappings Parameters not found in Prepare() method