2014-03-01 23 views
0

我創建了一個新的Spring MVC項目,然後在index.jsp中發送POST到名爲springController.java的Spring Controller。文件的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> 

我試圖抓住POST在springController.java,這裏是代碼:

/* 
* 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"); 
    } 

} 

但sendind POST不工作,在WebInspector控制檯我得到

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

在這一刻我只想抓取從index.jsp發送的POST。不幸的是,我是Spring MVC的新手,我不知道我做錯了什麼。 這裏是調度員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"/> 

這裏是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> 

我不知道什麼是錯的,我爲什麼不能,,抓「在springController.java中從index.jsp發佈POST 這是我在StackOverflow上的第一篇文章,如果有人決定幫助我,我會非常高興

來自波蘭的問候並感謝您的幫助!

這裏是文件applicationContext,我必須在這裏粘貼文件,因爲我是新的,沒有足夠的信譽。仍然在WebInspector控制檯我有同樣的消息早前:

<?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> 

回答

1

嘗試取消註釋

RequestMethod.POST 

方法定義

另外,還要確保您的控制器是由彈簧公認豆上方。你用@Controller註解了類。爲了使它工作,你可以添加配置到你的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 /> 

命名空間:

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" 
+0

不幸的是,仍然無法正常工作,也許我貼你在錯誤的地點的代碼,文件的applicationContext我有在第一篇文章中粘貼,因爲回覆消息太長,我沒有足夠的信譽來回答自己。 –

+0

抱歉,嘗試將的「base-package」屬性從「by」更改爲「newpackage」(您的類所在的包)。 – yname

相關問題