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

标签 java ajax spring jsp struts

我试图让 Ajax 使用 Struts 和 Spring 与一个简单的 WebApp 一起工作,这样我就可以在另一个 .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:

type Status report

message /ajax.do

description The requested resource (/ajax.do) is not available.

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

知道问题出在哪里吗?我是否认为 Struts 的重定向方式错误?

提前致谢。

最佳答案

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

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

关于java - 无法使 Ajax 与 Struts/Spring 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9620536/

相关文章:

java - 如何选择maven依赖项:list?的输出

javascript - JQuery:将 JSON 结果存储在 JavaScript 数组中?

java - 如何从 MongoDB 获取 ObjectId 值?

java - 使用小程序单击按钮时打印图像?

java - WildcardQuery 没有返回正确的结果

javascript - 带有百分比的 Ajax 加载进度条

c# - 使用ajax在iframe中加载内容

java - 如何手动包含 maven 更新由于防火墙限制而无法获取的 jar 文件

java - Hibernate - 多对多实现

java - 配置问题: Bean name "dataSource" is already used in this <beans> element