2

我正在尝试设置我的 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 作为服务器。

4

1 回答 1

0

您没有login在控制器中使用@RequestMapping. 所以如果你的代码是

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

/data/login然后将映射url 。

于 2013-05-12T02:01:03.933 回答