2012-01-19 2 views
19

간단한 스프링 3 애플리케이션을 만들고 다음 파일을 만들려고합니다. 나에게이 오류 아래WebApplicationContext가 없습니다. ContextLoaderListener가 등록되지 않았습니까?

에 대한 이유를 알려주십시오 것은 내 web.xml에 다음

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
     id="WebApp_ID" version="3.0"> 
    <display-name>Spring2</display-name> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>0</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

입니다 것은 내 index.jsp를 아래

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 

<!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=ISO-8859-1"> 
     <title>Insert title here</title> 
    </head> 
    <body> 
     Index Page<br/> 
     <form:form commandName="loginBean" method="POST" action="login"> 
      <form:input path="userName" id="userName"/><br/> 
      <form:input path="password" id="password"/><br/> 
      <input type="submit" value="submit"/> 
     </form:form> 
     <a href="register.jsp">Go to Registration Page</a> 
    </body> 
</html> 

것은 내 디스패처-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" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
    <bean name="/login" class="com.infy.controller.LoginController"/> 
</beans> 

이것은 LoginController.java입니다.

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 


@Controller 
public class LoginController { 
    @RequestMapping(value="/login", method=RequestMethod.POST) 
    public ModelAndView loginAction(@ModelAttribute("loginBean")LoginBean bean){ 
     return new ModelAndView("success", "success", "Successful Login"); 
    } 
} 

그리고 마지막으로 내 LoginBean

public class LoginBean { 
    private String userName; 
    private String password; 
    public String getUserName() { 
     return userName; 
    } 
    public void setUserName(String userName) { 
     this.userName = userName; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 
} 

답변

64

당신은 당신의 web.xml에서의 ContextLoaderListener을해야합니다 - 그것은 당신의 구성 파일을로드합니다.

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

웹 응용 프로그램 컨텍스트와 루트 응용 프로그램 컨텍스트 간의 차이점을 이해해야합니다. 웹 MVC 프레임 워크에서

은, 각각의 DispatcherServlet은 이미 루트 WebApplicationContext에 정의 된 모든 bean을 상속하는 자체적 인 WebApplicationContext를 가지고있다. 이러한 상속 된 빈은 서블릿 특정 범위에서 재정의 될 수 있으며 새로운 범위 별 빈은 지정된 서블릿 인스턴스에 대해 로컬로 정의 될 수 있습니다.

Dispatcher 서블릿의 응용 프로그램 컨텍스트는 웹 클래스에만 적용되는 웹 응용 프로그램 컨텍스트입니다. 중간 계층에 사용할 수 없습니다. ContextLoaderListener를 사용하여 전역 앱 컨텍스트가 필요합니다.

스프링 mvc에 대한 스프링 참조 here을 읽습니다.

+2

을 applicationContext.xml이 필요하고 이후에는 바인딩 오류가 발생합니다 – Batman

+1

@ 배트맨 또한 컨트롤러를 어떻게 자동 감지하고 있습니까? 컨텍스트가 필요합니다 : 구성 요소 스캔 또는 '' –

+0

@Batman 내가 넣은 Spring 참조를 확인하십시오. 그것은 당신을 도울 것입니다. –

0

그리고 당신은, 오히려 org.springframework.web.context.ContextLoaderListener하여 XML 구성에서로드하는 새로운 상황에 맞는보다 를 기존의 컨텍스트를 사용하고자하는 경우 다음을 참조 - 즉거야>https://stackoverflow.com/a/40694787/3004747

+0

이것은 web.xml뿐만 아니라 web.xml이없는 경우에도 사용할 수 있습니다. – ank