javascript 在 Struts 1.2 中使用 mapping.findForward 转发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17676496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:10:00  来源:igfitidea点击:

Forwarding using mapping.findForward in Struts 1.2

javascriptstruts-1

提问by user2473430

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); 

    }
}

In Struts-config.xml, the following is added:

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

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

In web.xml, the following is added:

在 中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>

In inicio.jsp, the following is added:

在 中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>

The function requestXMLis as follows:

功能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);
}

The above code is not calling the GetFAP servlet when using mapping.findForward. But when I used response.sendRedirect("entire jsp path")it is calling the servlet.

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

Can anyone tell me what I am doing wrong here?

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

采纳答案by Dave Newton

You are using a relative URL instead of an absolute URL.

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

When you render the JSP directly, the ../GetFAPmapping works because you mustmove "up" a level up from the /jspdirectory.1

当您直接呈现 JSP 时,../GetFAP映射会起作用,因为您必须/jsp目录“上移”一个级别。1

When you render the JSP from an action, you're moving a level up from the action'spath, i.e., there's no more /jspdirectory in the URL to move up from.

当您从一个动作呈现 JSP 时,您从动作的路径上移了一个级别,即/jspURL 中没有更多的目录可以.

This is among the many reasons why using relative paths can be a bad idea.

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



1JSP files should live in the WEB-INFdirectory to avoid direct client access.

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