Struts2 URL无法访问

时间:2014-08-19 01:21:32

标签: java struts2 actionresult xml-configuration struts2-namespace

我真的在这里使用Struts2 - 我能够通过省略部分路径来访问JSP页面。请注意路径假设包含pages/welcome_user.jsp。关键是要查看路径中的单词pages

这是struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="default" namespace="/User" extends="struts-default">
        <action name="Login">
            <result>pages/login.jsp</result>
        </action>
        <action name="Welcome" class="com.mkyong.user.action.WelcomeUserAction">
            <result name="SUCCESS">pages/welcome_user.jsp</result>
        </action>
    </package>
</struts>

我可以通过login.jsp访问http://localhost/Struts2Example/User/Loginwelcome_user.jsp来自:http://localhost/Struts2Example/User/Welcome
请注意,在这两个网址中,我都可以删除pages,为什么?

源: http://www.mkyong.com/misc/how-to-use-mkyong-tutorial/

有人可以通过上面的教程告诉我出了什么问题吗?

1 个答案:

答案 0 :(得分:1)

首先,您使用了映射到struts.xml中的操作的网址。

执行action方法并返回结果代码SUCCESS。您可以在操作配置中找到此结果。然后执行结果,如果未设置结果类型,则默认值为dispatcher,并且请求将转发到结果配置中指定的位置。

如果location是相对的,则最终绝对位置将由用于此操作的包的名称空间确定。

您可以在示例Struts 2 Namespace configuration example and explanation中找到更详细的使用命名空间和解释示例。

如果使用转发到JSP的dispatcher结果,则无法删除页面。在这种情况下,URL已被重写,您无法看到最终的URL。