在struts2中使用拦截器捕获root的问题

时间:2011-04-20 13:10:40

标签: struts2 interceptor login-control interceptorstack

我有一个intercetpor,用于检查用户是否在提供请求的操作之前已登录。我已经尝试将此设置为所有操作的默认值。除了一个地址之外的所有地址都像魅力一样。当我由于某种原因转到我的根URL "http://localhost:8080/map/"时,拦截器不会触发。我猜我的struts.xml有些缺失,但我无法弄清楚:

<struts>

    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources,DatabaseResources" />

    <package name="map" extends="struts-default">
        <interceptors>
            <interceptor name="loginintercept"
                class="se.contribe.intercept.LoginInterceptor" />
            <interceptor-stack name="defaultLoginStack">
                <interceptor-ref name="loginintercept" />
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="defaultLoginStack" />

        <default-action-ref name="index"></default-action-ref>

        <global-results>
            <result name="loginneeded">/login.jsp</result>
        </global-results>

        <action name="index" class="**.map.MapAction">
            <result>/index.jsp</result>
        </action>

        <action name="login">
            <result>/login.jsp</result>
        </action>

        <action name="loginInput" class="**.session.LoginAction">
            <result type="redirectAction">
                <param name="actionName">index</param>
            </result>
            <result name="input">/login.jsp</result>
            <result name="error">/login.jsp</result>
        </action>

        <action name="*" class="**.map.MapAction">
            <result>/index.jsp</result>
        </action>
    </package>

</struts>

我已经混淆了班级名称,以防万一我的雇主反对。

2 个答案:

答案 0 :(得分:0)

<击> 您的根网址可能是http://localhost:8080/map/,但包裹“地图”是指http://localhost:8080/map/map

你可能想要一个为“{”定义的包,它位于http://localhost:8080/map/的根,你可能想要一个为“”定义的包,它允许在任何内部执行任何操作。包。

编辑:在上面我混淆了命名空间的名称(似乎使用的约定插件太多了!)

我强烈怀疑,如果你检查你的web.xml文件,你会发现类似的东西:

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

如果您将其更改为

<welcome-file-list>
    <welcome-file>index.action</welcome-file>
</welcome-file-list>

你会得到你所期望的,因为我可以同时拥有index.jsp和index.action并更改该设置选择其中一个。

答案 1 :(得分:0)

我最终成功地把这个想象得出来了 我测试了在

的执行阶段向控制台写一个简单的输出
<action name="index" class="**.map.MapAction">  

当我打开网页时,控制台中没有打印输出。这让我思考。我的主页名为index.jsp,显然该名称绕过了Struts普通控件。将名称更改为index2.jsp解决了这个问题 可能有一些地方我可以改变这种行为,但只是更改名称更容易。