无法使Ajax与Struts / Spring一起工作

时间:2012-03-08 15:49:49

标签: java ajax spring jsp struts

我正在尝试使用Struts和Spring使用简单的WebApp使Ajax工作,所以我可以在另一个.jsp上的div标签内加载.jsp,但我不断收到“找不到资源”错误。我看了整个网络,但仍然无法弄明白。

这是我的代码:

的struts-config.xml

<global-exceptions>
</global-exceptions>
<global-forwards></global-forwards>

<action-mappings>
    <action path="/ajax" input="/index.jsp" type="org.springframework.web.struts.DelegatingActionProxy">
        <forward name="success" path="/contenido.jsp" />
    </action>

</action-mappings>

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
         <set-property property="contextConfigLocation"
            value="/WEB-INF/classes/spring/ApplicationContext.xml"/>
</plug-in>

的applicationContext.xml

<!-- Actions Classes -->
<bean name="/ajax" class="com.ajax.prueba.action.AjaxAction"> </bean>

AjaxAction.java

public class AjaxAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward("success");
    }
}

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Ajax Test</title>
        <script type="text/javascript" src="<c:url value="/js/general.js"/>"></script>
    </head>
    <body>

        <h1>This is an Ajax Test</h1>
        <html:link  href="#" onclick="loadContent()" >Show Message</html:link>

        <div id="content"></div>

    </body>
</html>

contenido.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Ajax is working!

general.js

var xmlhttp;

function loadContent()
{

 xmlhttp=GetXmlHttpObject();

  if (xmlhttp==null)
  {
   alert ("Your browser does not support Ajax HTTP");
   return;
  }

    var url="/ajax.do";

    xmlhttp.onreadystatechange=getOutput;
    xmlhttp.open("GET",url,false);
    xmlhttp.send(null);
}

function getOutput()
{
  if (xmlhttp.readyState==4)
  {
  document.getElementById("content").innerHTML=xmlhttp.responseText;
  }
}

function GetXmlHttpObject()
{
    if (window.XMLHttpRequest)
    {
       return new XMLHttpRequest();
    }
    if (window.ActiveXObject)
    {
      return new ActiveXObject("Microsoft.XMLHTTP");
    }
 return null;
}

index.jsp加载得很好,但当我点击“显示消息”链接时,我从Apache服务器获取404:

  

输入状态报告

     

消息/ajax.do

     

description请求的资源(/ajax.do)不可用。

对不起,如果太长了。这只是一个测试Ajax的小应用程序,然后在我正在处理的另一个大应用程序上使用它(我也遇到了同样的问题)。

知道问题可能是什么?我是否认为Struts的重定向方式错误?

提前致谢。

1 个答案:

答案 0 :(得分:1)

您需要指定操作的完整路径:

    url='http:\\localhost:8080\'+appName+'\ajax.do'