【问题标题】:Spring Controller w/ AJAX 404带有 AJAX 404 的弹簧控制器
【发布时间】:2013-05-06 09:00:30
【问题描述】:

我正在尝试设置我的 Tomcat 服务器以使用 Spring Controller 处理 AJAX 请求,但是在发送 AJAX 请求时出现 404 错误。我正在将 IntelliJ 与 Maven 一起使用,并最终尝试建立一个网站来处理登录服务和其他数据服务。

这是我所拥有的:

pom.xml - 路径:myWebapp/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.mygroup.app</groupId>
    <artifactId>myWebapp-webapp</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>myWebapp-webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>

web.xml - 路径:myWebapp/src/web/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd"
           version="2.5">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/data/*</url-pattern>
    </servlet-mapping>
</web-app>

dispather-servlet.xml - 路径:myWebapp/src/web/WEB-INF/dispatcher-servlet.xml

<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.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.myWebapp.sdc.controllers"  />
    <mvc:annotation-driven />
</beans>

CredentialsController.java - 路径:myWebapp/src/main/java/com/myWebapp/sdc/controllers/CredentialsController.java。 ServiceRequest 类工作正常,是一个标准模板。

package com.myWebapp.sdc.controllers;

import com.myWebapp.classes.LoginDataModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.myWebapp.classes.ServiceRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "login")
public class CredentialsController {
    public @ResponseBody ServiceRequest<LoginDataModel> login(
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ServiceRequest<LoginDataModel> loginDataModelServiceRequest = new
                ServiceRequest<LoginDataModel>(new 
        return loginDataModelServiceRequest;
    }
}

最后,AJAX 调用 - 在 html 内(路径:myWebapp/src/web/index.html)。默认启动页面是http://localhost:8080/web/index.html。并且我在调用之前测试了 JQuery 是否正确加载。

$(document).ready(function(){
    $("#submitLogin").on('click', function(){
        $.ajax({
            type:"POST",
            url: '/data/login',
            success:function(data){
                alert('success');
            },
            error:function(){
                alert('failed');
            }
        });
    });
});

以及 Chrome 开发工具中的 XHR 调用:

Request URL:http://localhost:8080/data/login
Request Method:POST
Status Code:404 Not Found
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:0
Cookie:JSESSIONID=6717B5682C531D61F05270196DC09DFD
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/web/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
X-Requested-With:XMLHttpRequest
Response Headersview source
Content-Length:971
Content-Type:text/html;charset=utf-8
Date:Sun, 12 May 2013 00:58:04 GMT
Server:Apache-Coyote/1.1

任何意见将不胜感激!非常感谢:)

更新和关闭:未解决 不幸的是,我无法弄清楚这一点。请在其他地方咨询有关此问题的帮助。我最终改用 Node 作为服务器。

【问题讨论】:

  • 您必须在应用程序启动时检查地址是否实际映射正确?你在spring mvc启动时可以看到所有RequestMapping。
  • 感谢您的回复!我根据this SO answer 进行了尝试。是的,它打印到控制台com.myWebapp.sdc.controllers.CredentialsController。所以那一定意味着电话不好。

标签: ajax spring spring-mvc


【解决方案1】:

您没有在控制器中使用@RequestMapping 注释您的login 方法。所以如果你的代码是

@RequestMapping
public @ResponseBody ServiceRequest<LoginDataModel> login(
....

然后将映射 url /data/login

【讨论】:

  • 你能澄清一下吗?在login 方法之前添加@RequestMapping(value = "log") 对您来说就足够了吗?我已经尝试过了(并将 AJAX 调用更新为 /data/login/log)但无济于事
  • 哦,我看到你正在使用POST 方法。你能用@RequestMapping(value = "log", method = RequestMethod.POST)再试一次吗?
  • 是的,刚刚尝试过,但也没有用。谢谢回复。还有其他想法吗?有没有办法检查控制器是否被 Spring 拾取?
  • 您发布到/data/login/log,应该是myWebapp-webapp/data/login/log,其中myWebapp-webapp 是您的应用程序名称。
  • 我认为你的情况是web/data/login/log
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-28
  • 1970-01-01
相关资源
最近更新 更多