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>

FireBug エラー メッセージ

404お探しのページが見つかりませんでした -http://localhost:8080/Questionnaire/getQnrList.do? {%22inputMessage%22:%22hello%22}"

どこが間違っているのか掴めない

4

3 に答える 3

1

問題は、web.xml に URL パターン .do がないことです。

これを追加

  <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をこれに設定してみてください:

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

もしかしたらURLが間違っているかもしれません..

于 2013-02-06T06:03:11.400 に答える