在 Struts 1.2 中使用 mapping.findForward 进行转发

Forwarding using mapping.findForward in Struts 1.2

本文关键字:findForward mapping 转发 Struts      更新时间:2023-09-26
public class MyAction extends Action
{   
    public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {
        String status="success";
        HttpSession session = request.getSession(true);
        System.out.println("My Action---setting key value");
        request.getSession().setAttribute("key1","check");
        //response.sendRedirect("http://localhost:9080/FamiliarPortal/jsp/inicio.jsp");
        return mapping.findForward(status); 
    }
}

Struts-config.xml中,添加了以下内容:

<action path="/myAction" type="iusa.ubicacel.actions.MyAction" validate="false" >
         <forward name="success" path="/jsp/inicio.jsp"/>       
</action>

web.xml中,添加了以下内容:

<servlet>
        <servlet-name>GetFAP</servlet-name>
        <servlet-class>iusa.ubicacel.actions.map.GetFAP</servlet-class>
    </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
<servlet-mapping>
        <servlet-name>GetFAP</servlet-name>
        <url-pattern>/GetFAP</url-pattern>
</servlet-mapping>

inicio.jsp中,添加了以下内容:

<BODY onload="requestXML('<%=reqURL %>');">
<table border="0" cellspacing="0" cellpadding="0" align="center">
    <tr>
        <td align="center" valign="middle">
            <div id="mapdiv" style="width: 1000px; height:700px"></div> 
        </td>
    </tr>
</table>
</BODY>

函数requestXML如下:

function requestXML(reqURL) 
{   
    alert("calling requestXML"+reqURL);
    var url = "../GetFAP?requestURL=" + reqURL;
    alert("calling requestXML"+url);
    xmlhttpUbi = FAPXMLHttpRequest();
    xmlhttpUbi.open("POST", url, true); // async
    alert("after calling");
    xmlhttpUbi.onreadystatechange = obtainFAPLocation;
    xmlhttpUbi.send(null);
}

上面的代码在使用 mapping.findForward 时没有调用 GetFAP servlet。但是当我使用response.sendRedirect("entire jsp path")它调用servlet时。

谁能告诉我我在这里做错了什么?

您使用的是相对 URL 而不是绝对 URL。

当您直接呈现 JSP 时,../GetFAP映射有效,因为您必须/jsp目录"上移"一个级别。1

当您从操作渲染 JSP 时,您将从操作的路径向上移动一个级别,即 URL 中没有更多的/jsp目录可以向上移动。

这是使用相对路径可能是一个坏主意的众多原因之一。


1 JSP 文件应位于 WEB-INF 目录中,以避免直接访问客户机。