Java 如何在Spring控制器中捕获JQuery$post?
我创建了一个新的SpringMVC项目,然后在index.jsp中向名为springController.java的Spring控制器发送帖子。文件index.jsp:Java 如何在Spring控制器中捕获JQuery$post?,java,jquery,spring-mvc,post,controller,Java,Jquery,Spring Mvc,Post,Controller,我创建了一个新的SpringMVC项目,然后在index.jsp中向名为springController.java的Spring控制器发送帖子。文件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.
<%@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>
但是sendindpost不起作用,在WebInspector的控制台中我得到
POST http://localhost:8084/test/another 404 (Not Found)
现在我只想从index.jsp发送catch POST。不幸的是,我是春季MVC的新手,我不知道我做错了什么。
以下是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"/>
索引控制器
下面是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>
上下文配置位置
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
调度员
org.springframework.web.servlet.DispatcherServlet
2.
调度员
*.htm
30
redirect.jsp
我不知道出了什么问题,为什么我不能从springController.java中的index.jsp捕获“POST”。
这是我在StackOverflow上的第一篇文章,如果有人决定帮助我,我会非常高兴
来自波兰的问候,感谢您的帮助
这是文件应用程序上下文,我必须将文件粘贴到这里,因为我是新来的,没有足够的声誉。仍然在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>
尝试取消注释
RequestMethod.POST
在方法定义之上
还要确保您的控制器被spring识别为bean。您用@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 />
不幸的是,它仍然不起作用,可能我将您的代码粘贴到了错误的位置,我必须在第一篇文章中粘贴文件applicationContext,因为回复消息太长,而且我没有足够的声誉来回答我自己。抱歉,请尝试将的“base package”属性从“by”更改为“newpackage”(您的类所在的包)。
<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"