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

发布于 2024-11-02 15:07:26 字数 1948 浏览 3 评论 0原文

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

我对类名做了一些混淆,以防我的雇主反对。

I have an intercetpor that checks if the user is logged in before serving the requested action. I have tried setting this as default for all action. For all addresses except one this works like a charm. When I go to my root URL "http://localhost:8080/map/" for some reason the interceptor doesn't fire. I'm guessing there is something missing i struts.xml but I can't figure out what:

<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>

I have obfuscated the class names a bit just in case my employer would object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2024-11-09 15:07:26

我最终自己解决了这个问题。
的执行阶段向控制台写入一个简单的输出

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

我测试了在“当我打开网页时,控制台中没有打印任何输出” 。这让我思考。我的主页被命名为index.jsp,显然该名称绕过了Struts 正常控件。将名称更改为index2.jsp 解决了问题。
可能在其他地方我可以更改此行为,但简单地更改名称会更容易。

I managed to figure this one out my self eventually.
I tested writing a simple output to console in the execute stage of

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

When I opened the web page no output was printed in the console. That got me thinking. My main page was named index.jsp and apparently that name bypasses Struts normal controls. Changing the name to index2.jsp solved the problem.
There is probably some place else where I could change this behaviour but it was easier to simply change the name.

坐在坟头思考人生 2024-11-09 15:07:26

<罢工>
您的根 URL 可能是 http://localhost:8080/map/ 但包“map”指的是 < a href="http://localhost:8080/map/map" rel="nofollow">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,并更改该设置选择其中之一。


Your root url may be http://localhost:8080/map/ but a package "map" refers to http://localhost:8080/map/map

You probably want a package defined for "/" which the root at http://localhost:8080/map/ and you might want a package defined for "" which allows the actions within to be executed from within any package.

Edit: In the above I confused name for namespace (been using conventions plugin too much it seems!)

I would strongly suspect that if you check your web.xml file you'd find something like:

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

which if you changed that to

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

you would get what you expected, as I can have both a index.jsp and an index.action and changing that setting choose one or the other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文