0

回答:


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

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

我需要将“上下文根”(又名 webapp 名称)放在 URL 中:

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

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

我开始让我的 JQuery 调用为:

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

下面引用了我的代码

任何线索将不胜感激。在此先感谢史蒂夫

你好.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

极致-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>

网页.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>
4

3 回答 3

0

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

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

jquery json 到字符串?

继续编码:)

于 2012-06-11T19:05:22.937 回答
0

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

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

我需要将“上下文根”(又名 webapp 名称)放在 URL 中:

$.post("/acme/test_ajax",  {state:"Alaska"});
于 2012-06-12T13:33:38.920 回答
0

我刚刚遇到了同样的问题。

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

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

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

于 2017-08-11T12:31:53.537 回答