Radek Anuszewski
Radek Anuszewski

Reputation: 1910

How to catch JQuery $post in Spring Controller?

I create a new Spring MVC project, then in index.jsp I send POST to the Spring Controller named springController.java. File index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.js"></script>
        <script type="text/javascript" src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
        <title>Welcome to Spring Web MVC project</title>

        <script type="text/javascript">
             $(document).ready(function(){
                 $("#testForm").submit(onFormSubmit);
             });
             function onFormSubmit(e){

                 data = $("#testForm").serialize();
                // console.log(data);
                 $.post("/test/another");
                 e.preventDefault();
             }

        </script>
    </head>

    <body>
        <form id="testForm">
            <label>Imie: </label>
            <input type="text" name="login">
            <label>Nazwisko: </label>
            <input type="text" name="password">
            <input id="testPostButton" type="submit">
        </form>

    </body>
</html>

I try to catch POST in springController.java, here is the code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newpackage;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Abc
 */
@Controller
@RequestMapping(/*method=RequestMethod.POST,*/ "/test")
public class springController {
    /**
     *
     * @return
     */
    @RequestMapping(/*method=RequestMethod.POST,*/ "/another")
    @ResponseBody
    public void test(){
        System.out.println("Doszlo");
    }

}

But sendind POST is not working, in WebInspector's console I get

 POST http://localhost:8084/test/another 404 (Not Found)

In this moment I only want catch POST send from index.jsp. Unfortunately, I am newbie in Spring MVC and I don't know what I am doing wrong. Here is dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />

<bean name="/test" class="newpackage.springController"/> 

And here is web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

I have no idea what is wrong, why I can't ,,catch" POST from index.jsp in springController.java. It is my first post on StackOverflow, I would be very happy if somebody decide to help me.

Greetings from Poland and thank you for help!

Here is file applicationContext, I have to paste file here because I am new and don't have enough reputation. Still in WebInspector's console I have the same message as earlier:

<?xml version="1.0" encoding="UTF-8"?>

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




<context:component-scan base-package="by" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

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

Upvotes: 0

Views: 239

Answers (1)

yname
yname

Reputation: 2255

Try to uncomment

RequestMethod.POST

above the method definition

Also make sure that your controller is recognized as bean by spring. You annotated class with @Controller. To make it work you can add configuration to your applicationContext.xml:

    <context:component-scan base-package="newpackage" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>

    <mvc:annotation-driven />

namespaces:

    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"          
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd                  
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd                 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"

Upvotes: 1

Related Questions