0

Answer:


My Spring 3 Java webapp is "acme". I had this for my JQuery call to contact the Spring 3 Java server side components:

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

I needed to put the "context-root" ( aka the webapp name ) in the URL:

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

I'm learning Spring 3, Ajax and JQuery. I made myself a small webapp, "acme", to learn how to connect the 3 technologies. From my logging statements it looks the handler method getAddressList() in the Controller HelloController is not getting called after the JQuery POST request.

I started off making my JQuery call as:

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

My code is quoted below

Any clues would be much appreciated. Thanks in advance Steve

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>
Steve
  • 3,127
  • 14
  • 56
  • 96

3 Answers3

0

Seems to me that you're trying to pass the service a JS Object rather than a JSON String. There is a jQuery plugin that parses a JS Object to a JSON String, as previously answered in another stackoverflow question.

You can get the plugin and more information in the following question:

jquery json to string?

keep coding :)

Community
  • 1
  • 1
Paulo Victor
  • 906
  • 2
  • 10
  • 18
  • I'm still learning all 3 of these things, and just starting with JQuery and Ajax. I didn't understand the other post too much. Can you break your answer down more? Are you saying that I need a library/*.js file from JQuery beyond just what I have or are you saying I need to change my code? I'm using this post to help me learn and it is what I modeled my test program after. It didn't mention anything about plugins. http://maxheapsize.com/2010/07/20/spring-3-mvc-ajax-and-jquery-magic-or-better-simplicity/ – Steve Jun 11 '12 at 19:21
  • Sure:) Basically, a jQuery plugin is *.js file that you "import" in your HTML. Magically you'll get the function you need using the $ global. But my mistake, it wasn't a jQuery plugin, it was a external library, which is not much different from what I said earlier. In the comments, someone mentioned the use of browser-native JSON functions, such as JSON.stringify(). It's worth a try. – Paulo Victor Jun 11 '12 at 19:33
  • So if I understand you correctly, I am sending Spring a javascript string/object, so Spring is ignoring it, not calling the handler. So, I need to send the POST argument "state" as a JSON string? – Steve Jun 11 '12 at 20:26
  • I updated my original post based on your comments. I'm still not reaching the Spring handler – Steve Jun 11 '12 at 21:19
  • Hmm, seeing your code, you could try to pass just "Arizona", as a pure string. The problem is that you're trying to pass an actual object to the server, not its string representation. – Paulo Victor Jun 12 '12 at 00:09
0

My Spring 3 Java webapp is "acme". I had this for my JQuery call to contact the Spring 3 Java server side components:

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

I needed to put the "context-root" ( aka the webapp name ) in the URL:

$.post("/acme/test_ajax",  {state:"Alaska"});
Steve
  • 3,127
  • 14
  • 56
  • 96
0

I just experienced the same problem.

All my AJAX requests were sent to the root context.

An easy fix, is to leave out the first slash, so the url is appended to the application context.

In your case: $.post("test_ajax", {state:"Alaska"});

Lotus91
  • 1,127
  • 4
  • 18
  • 31
seris
  • 1