2012-03-08 111 views
1

我想使Ajax與使用Struts和Spring的簡單WebApp一起工作,因此我可以在另一個.jsp上的div標籤中加載一個.jsp,但我一直在一個「資源未找到」的錯誤。我瀏覽過所有網頁,但仍然無法弄清楚。不能使Ajax與Struts/Spring工作

這裏是我的代碼:

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

你需要指定動作的完整路徑:

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

該死的,非常感謝你,不敢相信這是這麼簡單。 – 2012-03-08 16:14:47