Struts2 URL 无法访问

我真的在用 Struts2 绞尽脑汁——我可以通过省略部分路径来访问 JSP 页面.注意路径假设包含 pages/welcome_user.jsp.关键是看路径中的pages这个词.

I'm really racking my head here with Struts2 - I'm able to access the JSP pages by omitting part of the path. Note the path suppose to include pages/welcome_user.jsp. The key is to look at the word pages in the path.

这是 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/Login
welcome_user.jsp 通过:http://localhost/Struts2Example/User/Welcome
请注意,在两个 URL 中,我都可以删除 pages,为什么?

I'm able to access login.jsp via: http://localhost/Struts2Example/User/Login
and welcome_user.jsp via: http://localhost/Struts2Example/User/Welcome
Note that in both URL, I'm able to drop pages, why?

来源:http://www.mkyong.com/misc/how-to-使用-mkyong-tutorial/

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

Can someone go through the above tutorial and tell me what's wrong?

推荐答案

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

First, you have used URLs that are mapped to the actions in the struts.xml.

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

The action method is executed and returns a result code SUCCESS. This result you can find in the action config. Then result is executed, if the type of result isn't set the default is dispatcher, and request is forwarded to the location specified in the result config.

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

If location is relative the final absolute location will be determined by the namespace of the package used for this action.

更详细的命名空间使用示例和说明可以在示例 Struts 2 Namespace 配置示例及说明.

More detailed example of usage namespaces and explanation you can find in the example Struts 2 Namespace configuration example and explanation.

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

You can't drop pages if you are using dispatcher result that forwards to JSP. In this case the URL has been rewritten and you can't see the final URL.

相关文章