春天3,阿贾克斯& JQuery:为什么我的请求不能到达我的控制器?

时间:2012-06-11 18:49:38

标签: jquery ajax spring-mvc


答案:


My Spring 3 Java webapp是“极致”。我有这个用于我的JQuery调用以联系Spring 3 Java服务器端组件:

$.post("/test_ajax",  {state:"Alaska"});

我需要将“context-root”(也就是webapp名称)放在URL中:

$.post("/acme/test_ajax",  {state:"Alaska"});

我正在学习Spring 3,Ajax和JQuery。我为自己制作了一个小型的webapp,“极致”,以学习如何连接3种技术。从我的日志语句中看,在JQuery POST请求之后,控制器 HelloController 中的处理程序方法 getAddressList()没有被调用。

我开始将我的JQuery调用作为:

$.post("/test_ajax",  {state:"Alaska"});

我的代码引用在下面

任何线索都会非常感激。 提前致谢 史蒂夫

的hello.jsp

<html>
<head>
    <title>ACME JSP</title>
    <script language = "JavaScript" src = "../acme/js/jquery-1.7.js"></script>
    <script language = "JavaScript">
    function test(){
        $("#div1").replaceWith("Text Replaced With Javascript");
        $.post("/test_ajax",  {state:"Alaska"});
    }  
    </script>
</head>
<body>
    <h1>Hello.jsp: ${message}</h1>  

    <div id = "div1">
        Original Text
    </div>

    <input type="submit" value="Test" onclick="test()" /> 

</body>
</html>

HelloController.java:

package com.acme.controller;

import javax.annotation.Resource;
import javax.servlet.http.*;

import org.springframework.stereotype.Controller;
import org.springframework.validation.*;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import org.apache.log4j.Logger;

@Controller
public class HelloController {
    private static final Logger logger  = Logger.getLogger(HelloController.class);

   @RequestMapping({"/","/hello"})
    public String home(ModelMap model) {

        logger.debug("Started...");
        model.put("message","Hello From The Spring 3.1 Controller!");
        return "hello";
    }

    @RequestMapping(value = "/test_ajax", method = RequestMethod.POST)
    public @ResponseBody String getAddressList( @RequestParam(required=true) String state,
                                                ModelMap model  ) {

        logger.debug("started...");
        logger.debug("State: " + state);
        String a_string  = "Test String From Spring And Ajax!";
        return a_string;
    }

}// end class HelloController

ACME-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


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

  <mvc:resources mapping = "/**" location = "/"/>
  <mvc:annotation-driven/>

  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name = "prefix" value = "/jsp/"/>
    <property name = "suffix" value = ".jsp"/>
  </bean>


</beans>

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app  id="WebApp_ID" version="3.0"
   xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">


  <display-name>ACME Web Application</display-name>

  <servlet>
    <servlet-name>acme</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>acme</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- Help Find The Spring Config Files -->
  <listener>
    <listener-class>
                  org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/acme-servlet.xml</param-value>
  </context-param>

</web-app>

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.acme</groupId>
    <artifactId>acme</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>ACME webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2-rev-1</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet.jsp</groupId>
                    <artifactId>jsp-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jstl-impl</artifactId>
            <version>1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet.jsp</groupId>
                    <artifactId>jsp-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


        <!-- Spring 3.1.1 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>

        <!-- Jackson JSON Mapper -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.6.4</version>
        </dependency>

    </dependencies>



    <build>
        <finalName>acme</finalName>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

        </plugins>

    </build>
</project>

3 个答案:

答案 0 :(得分:0)

在我看来,您尝试将服务传递给JS对象而不是JSON字符串。有一个jQuery插件可以将JS对象解析为JSON字符串,如前面在另一个stackoverflow问题中的回答。

您可以在以下问题中获取插件和更多信息:

jquery json to string?

继续编码:)

答案 1 :(得分:0)

My Spring 3 Java webapp是“极致”。我有这个用于我的JQuery调用以联系Spring 3 Java服务器端组件:

$.post("/test_ajax",  {state:"Alaska"});

我需要将“context-root”(也就是webapp名称)放在URL中:

$.post("/acme/test_ajax",  {state:"Alaska"});

答案 2 :(得分:0)

我刚遇到同样的问题。

我的所有AJAX请求都被发送到根上下文。

一个简单的解决方法是省略第一个斜杠,因此url会附加到应用程序上下文中。

在您的情况下:$.post("test_ajax", {state:"Alaska"});