2013-05-12 2 views
2

스프링 컨트롤러로 AJAX 요청을 처리하도록 Tomcat 서버를 설정하려고하지만 AJAX 요청이 전송 될 때 404 오류가 발생합니다. Maven과 함께 IntelliJ를 사용하고 궁극적으로 로그인 서비스 및 기타 데이터 서비스를 처리 할 웹 사이트를 설정하려고합니다. mywebapp와/pom.xml 파일스프링 컨트롤러 (AJAX 포함) 404

<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 - 경로

의 pom.xml을 : 여기

내가 가진 무엇 /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> 

발송자-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/메인/자바/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'); 
      } 
     }); 
    }); 
}); 

와 크롬 개발 도구에서 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 

모든 입력은 감사하겠습니다! 고마워요 :)

업데이트 날짜 : 닫지 않은 : 나는 이것을 안타깝게도 알 수 없었다. 이 문제에 대해서는 다른 곳에서 도움을 청하십시오. 나는 노드를 서버로 대신 사용했다.

+0

주소가 실제로 응용 프로그램 시작시 올바르게 매핑되었는지 확인해야합니까? 봄 mvc에서 시작하면 모든 RequestMapping을 볼 수 있습니다. –

+0

답변 해 주셔서 감사합니다. 나는 [이 그래서 대답] (http://stackoverflow.com/a/10898139/712849)에 따라 그것을 시도했다. 그리고 네, 그것은'com.myWebapp.sdc.controllers.CredentialsController' 콘솔을 인쇄합니다. 그래서 그 전화가 나쁘다는 것을 의미합니다. – rsteier

답변

0

컨트롤러의 login 메서드에 @RequestMapping이라는 주석을 달지 않았습니다. 코드가

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

이면 /data/login이 매핑됩니다.

+0

설명해 주시겠습니까? 'login' 메소드가 나오기 전에'@RequestMapping (value = "log")'를 추가하면 충분할까요? 나는 그것을 시도했지만 (/ AJAX 호출을'/ data/login/log'로 업데이트했다.) 아무런 쓸모가 없다. – rsteier

+0

오, 나는'POST' 메소드를 사용하고있는 것을 보았다. '@RequestMapping (value = "log", method = RequestMethod.POST)'로 다시 시도 할 수 있습니까? – lunr

+0

예, 그냥 시도했지만 그 중 하나가 작동하지 않았다. 답장을 보내 주셔서 감사합니다. 다른 생각? 컨트롤러가 Spring에 의해 선택되는지 여부를 확인하는 방법이 있습니까? – rsteier