1

我经历了类似的问题,但无法弄清楚为什么我的代码不起作用。请指出我的代码中有什么错误。问题是 jquery ajax 调用没有到达 spring mvc 控制器

我正在使用 Spring 3.2 和 jackson 1.9

QuestionnaireController.java

@Controller
public class QuestionnaireController {

    public QuestionnaireController()
    {
        super();
    }   

        @RequestMapping(value="/getQnrList.do")
        public @ResponseBody Message getMessage(@RequestParam String inputMessage) 
                {
          Message message = new Message();
          message.setMessage("I was sent the message : " + inputMessage + " : so I returned.");
          return message;
        }
}

问卷列表.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

<script type="text/javascript" src="scripts/jquery-1.6.2.js"></script>
<script type="text/javascript" src="scripts/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="scripts/json.min.js"></script>

<script type="text/javascript">

function queClick()
{
        $.ajax({
    url:"getQnrList.do",
    method: "GET",
    dataType: "json",
    contentType: 'application/json',
    data: {inputMessage : 'hello'},
    success: function()
        {
            alert("hola");
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert("error:" + textStatus + " - exception:" + errorThrown);
            }
    }); 
}

</script>
</head>
<body>
<p>Questionnaire test</p>
<input type="button" value="Get Questionnaire!" onClick="queClick()">
</body>
</html>

类路径上的 jar 文件

<classpathentry kind="lib" path="D:/Questionnaire1.0 libs/commons-fileupload-1.2.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/commons-io-1.4.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/commons-lang3-3.0.1.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/commons-logging-1.1.1.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/jackson-core-asl-1.9.11.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/jackson-mapper-asl-1.9.11.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/jstl-1.2.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/log4j-1.2.15.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/mongo-2.10.1.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/servlet-api.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/slf4j-api-1.7.1.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/slf4j-simple-1.7.1.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-aop-3.2.1.RELEASE.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-beans-3.2.1.RELEASE.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-context-3.2.1.RELEASE.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-context-support-3.2.1.RELEASE.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-core-3.2.1.RELEASE.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-data-commons-core-1.4.0.RELEASE.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-data-mongodb-1.1.0.RC1.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-data-mongodb-cross-store-1.1.0.RC1.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-data-mongodb-log4j-1.1.0.RC1.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-expression-3.2.1.RELEASE.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-tx-3.2.1.RELEASE.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-web-3.2.1.RELEASE.jar"/>
    <classpathentry kind="lib" path="D:/Questionnaire1.0 libs/spring-webmvc-3.2.1.RELEASE.jar"/>
    <classpathentry kind="output" path="build/classes"/>

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-  app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>QuestionnaireTEST</display-name>
    <welcome-file-list>
    <welcome-file>questionnaireList.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
    <servlet-name>qnr</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>qnr</servlet-name>
    <url-pattern>/Questionnaire/*</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            /WEB-INF/rf_applicationContext.xml
        </param-value>
      </context-param>
    </web-app>

* rf_applicationContext.xml*

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans">

    <context:annotation-config></context:annotation-config>

    <context:component-scan base-package="com.ref"></context:component-scan>

    <mvc:annotation-driven></mvc:annotation-driven>

    <context:property-placeholder location="classpath:config.properties"></context:property-    placeholder>
    <import resource="rf_spring-data.xml" />
</beans>

萤火虫错误讯息

404 未找到 -http://localhost:8080/Questionnaire/getQnrList.do? {%22inputMessage%22:%22hello%22}"

我无法找出错误在哪里

4

3 回答 3

1

问题是您在 web.xml 中没有 .do 的 url 模式

添加这个

  <servlet-mapping>
<servlet-name>qnr</servlet-name>
<url-pattern>*.do</url-pattern>
  </servlet-mapping>

同样,您的成功函数将不会被调用,因为该函数没有将参数更改success: function()success: function(data)

于 2013-02-06T06:16:50.263 回答
0

您能否尝试将网址设置为此并查看:

url: "http://localhost:8080/Questionnaire/getQnrList.do",

可能是网址不对。。

于 2013-02-06T06:03:11.400 回答
0

要解决此问题,您可以做的第一件事是验证您的调度程序 servlet 文件,以确保 url 映射等参数。

除此之外,您可以尝试在 @Controller 之后提供 @RequestMapping 并在控制器中编写一个带有一些日志条目的小型 GET 方法,并查看 ajax 调用是否到达控制器(如果不是控制器中的预期方法)。一旦您确认您将更好地了解问题的根本原因是什么。

于 2014-08-31T15:36:15.093 回答