简体   繁体   English

Struts2动作映射问题

[英]Struts2 action mapping issue

Hello everyone, 大家好,

it's not really an issue but I'd like to know how do I restrict the following behavior. 这不是真正的问题,但我想知道如何限制以下行为。

I have this set in my stuts.xml file. 我在stuts.xml文件中设置了此设置。

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />

    <package name="default" extends="struts-default" namespace="/">
        <result-types>
            <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
        </result-types>

        <action name="blue">
            <result name="success" type="tiles">/blue.tiles</result>
        </action>

        <action name="yellow">
            <result name="success" type="tiles">/yellow.tiles</result>
        </action>

        <action name="red">
            <result name="success" type="tiles">/red.tiles</result>
        </action>
     </package>
</struts>

Now what bothers me, is that the actions are acessible like this: 现在令我困扰的是,这样的行为是必须的:

http://localhost:port/blue
http://localhost:port/yellow
http://localhost:port/red

but you can also access them like this.. 但您也可以像这样访问它们。

http://localhost:port/yellow/blue/
http://localhost:port/red/blue/yellow

so ti triggers all the actions mentioned after "/". 因此,ti会触发“ /”后面提到的所有动作。

I want to prevent this from happening , so I'd like to know if there's any way to restrict it? 我想防止这种情况的发生,所以我想知道是否有任何限制方法?

thanks in advance, Alex 预先感谢亚历克斯

In the web.xml you might having an entry like this for the struts action mapping web.xml ,对于struts操作映射,您可能会有一个这样的条目

<servlet-mapping>
    <servlet-name>struts2</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

As per the servlet specification A string beginning with a '/' character and ending with a '/*' suffix is used for path mapping 根据Servlet规范A string beginning with a '/' character and ending with a '/*' suffix is used for path mapping

So if you want to give the absolute url mapping, then you have to specify them like below 因此,如果要提供绝对的URL映射,则必须像下面这样指定它们

<servlet-mapping>
    <servlet-name>struts2</servlet-name>
    <url-pattern>/blue</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>struts2</servlet-name>
    <url-pattern>/yellow</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>struts2</servlet-name>
    <url-pattern>/red</url-pattern>
</servlet-mapping>

If you are using struts 2.1.7 or more then you can add a exclude pattern like below 如果您使用的是Struts 2.1.7或更高版本,则可以添加如下所示的排除模式

<constant name="struts.action.excludePattern" value="/([a-zA-Z0-9]+)/.*"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM