【问题标题】:Problems catching the root with interceptor in struts2在 struts2 中使用拦截器捕获根的问题
【发布时间】:2011-04-20 13:10:40
【问题描述】:

我有一个拦截器,用于在执行请求的操作之前检查用户是否已登录。我尝试将其设置为所有操作的默认值。对于除一个之外的所有地址,这就像一个魅力。当我出于某种原因转到我的根 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>

我稍微混淆了类名,以防我的雇主反对。

【问题讨论】:

  • localhost:8080/map 的请求是否在调用您的索引操作?如果是这样,该请求会调用哪些拦截器?

标签: struts2 interceptor login-control interceptorstack


【解决方案1】:

我最终设法解决了这个问题。
我测试了在

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

当我打开网页时,控制台中没有打印输出。这让我思考。我的主页被命名为 index.jsp,显然这个名字绕过了 Struts 的常规控件。将名称更改为 index2.jsp 即可解决问题。
可能还有其他地方可以更改此行为,但更改名称更容易。

【讨论】:

    【解决方案2】:

    您的根 url 可能是 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 并更改该设置选择一个或另一个。

    【讨论】:

    • @Qyaternion 我认为您在谈论包的名称空间。如果没有提供 namespace="" 则使用。包的 name 属性就是一个名称,除非提供命名空间,否则没有与 url 的连接。至少这是我从 Apache 参考手册中得出的结论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多