2013-05-12 57 views
2

我试图建立我的Tomcat服务器来处理与一个Spring控制器AJAX请求,但是我得到的404错误发送AJAX请求的时候。我在Maven中使用IntelliJ,并最终尝试建立一个网站来处理登录服务和其他数据服务。春控制器瓦特/ AJAX 404

这是我有:

的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-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-INF /调度程序-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 /主/ JAVA/COM/myWebapp/SDC /控制器/ 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目录/网络/ 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 

任何投入,将不胜感激!非常感谢:)

UPDATE AND CLOSING:UNRESOLVED 不幸的是,我无法弄清楚这一点。请在这个问题的其他地方寻求帮助。我最终将Node用作服务器。

+0

你得检查该地址实际上是在应用程序启动正确映射?你在春季开始mvc可以看到所有的RequestMapping。 – 2013-05-12 05:43:16

+0

感谢您的回应!我试图基于[这个SO回答](http://stackoverflow.com/a/10898139/712849)。是的,它打印到控制台'com.myWebapp.sdc.controllers.CredentialsController'。所以这意味着电话会很糟糕。 – rsteier 2013-05-12 14:09:22

回答

0

你并没对@RequestMapping注释在控制器的login方法。因此,如果您的代码是

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

然后将映射网址/data/login

+0

你能澄清吗?在'login'方法之前添加'@RequestMapping(value =“log”)'是否足以满足您的需求?我曾试过(和更新AJAX调用'/数据/登录/ log'),但无济于事 – rsteier 2013-05-12 02:32:43

+0

哦,我看到你在使用'POST'方法。你可以用'@RequestMapping(值= “日志”,方法= RequestMethod.POST)'再试一次? – lunr 2013-05-12 02:44:06

+0

是的,只是试过,那也没有工作。谢谢回复。任何其他想法?有没有办法来检查控制器是否被Spring拿起,也许? – rsteier 2013-05-12 02:53:01