gpt4 book ai didi

java - 如何在Spring Controller 中捕获JQuery $post?

转载 作者:行者123 更新时间:2023-12-01 13:20:01 24 4
gpt4 key购买 nike

我创建一个新的 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>

我尝试在 springController.java 中捕获 POST,代码如下:

/*
* 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 的新手,我不知道我做错了什么。这是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>

我不知道出了什么问题,为什么我不能从 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>

最佳答案

尝试取消注释

RequestMethod.POST

方法定义上方

还要确保您的 Controller 被 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 />

命名空间:

    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"

关于java - 如何在Spring Controller 中捕获JQuery $post?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22116451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com