为什么我无法获取 jsp 以为它的路径?

Why I can not get the jsp thought its path?

我在我的 Java EE 项目中使用 struts:

在我的 loading.jsp 中,如果我使用下面的 src,我将得到 404 错误:

<IFRAME src="${pageContext.request.contextPath}/WEB-INF/page/menu/alermDevice.jsp" name="dev" id="dev" frameBorder="0" width="500" scrolling="auto" height="400">
</IFRAME>

但是如果我使用下面的 src:

<IFRAME src="elecMenuAction_alermDevice.do" name="dev" id="dev" frameBorder="0" width="500" scrolling="auto" height="400">
</IFRAME>

我会得到正确的信息。

这是我的 struts.xml:

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

    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.ui.theme" value="simple"></constant>
    <constant name="struts.action.extension" value="do"></constant>

    <package name="system" namespace="/system" extends="struts-default">
        <action name="elecTextAction_*" class="elecTextAction" method="{1}">
            <result name="save">/system/textAdd.jsp</result>
        </action>
        <action name="elecMenuAction_*" class="elecMenuAction" method="{1}">
            <result name="menuHome">/WEB-INF/page/menu/home.jsp</result>
            <result name="title">/WEB-INF/page/menu/title.jsp</result>
            <result name="left">/WEB-INF/page/menu/left.jsp</result>
            <result name="change">/WEB-INF/page/menu/change.jsp</result>
            <result name="loading">/WEB-INF/page/menu/loading.jsp</result>
            <result name="logout" type="redirect">index.jsp</result>
            <result name="alermStation">/WEB-INF/page/menu/alermStation.jsp</result>
            <result name="alermDevice">/WEB-INF/page/menu/alermDevice.jsp</result>
        </action>
    </package>
</struts>

为什么我用的路径无法访问JSP?只用动作就能搞定了吗?

Web 服务器无法从 WEB-INF 文件夹及其下获取资源。调用操作时,它 return 作为结果执行的响应。它使用结果类型 dispatcher,默认情况下用于将请求转发到指定的 URL(请求的 JSP 页面)。

Dispatcher Result

Includes or forwards to a view (usually a jsp). Behind the scenes Struts will use a RequestDispatcher, where the target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, you can pass data between them using request.setAttribute() - the Struts action is available.

There are three possible ways the result can be executed:

  • If we are in the scope of a JSP (a PageContext is available), PageContext's PageContext#include(String) method is called.

  • If there is no PageContext and we're not in any sort of include (there is no "javax.servlet.include.servlet_path" in the request attributes), then a call to RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse) is made.

  • Otherwise, RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse) is called.


调用 servlet 调度程序时,它没有这样的限制,并且可以 return 资源与最初请求的响应相同。