Struts2 regex configuration

夙愿已清 提交于 2019-12-10 13:54:30

问题


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


来源:https://stackoverflow.com/questions/31262287/struts2-regex-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!