spring 未找到 WebApplicationContext:未注册 ContextLoaderListener?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8924761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No WebApplicationContext found: no ContextLoaderListener registered?
提问by Batman
I'm trying to create a simple Spring 3 application and have the following files. Please tell me the reason for this error
我正在尝试创建一个简单的 Spring 3 应用程序并具有以下文件。请告诉我这个错误的原因
Below is my web.xml
下面是我的 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>
Below is my index.jsp
下面是我的 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>
Below is my dispatcher-servlet.xml
下面是我的 dispatcher-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>
This is the LoginController.java
这是 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");
}
}
And finally my LoginBean
最后是我的 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;
}
}
回答by Aravind A
You'll have to have a ContextLoaderListener in your web.xml - It loads your configuration files.
您必须在 web.xml 中有一个 ContextLoaderListener - 它会加载您的配置文件。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
You need to understand the difference between Web application context and root application context .
您需要了解 Web 应用程序上下文和根应用程序上下文之间的区别。
In the web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans defined can be overridden in the servlet-specific scope, and new scope-specific beans can be defined local to a given servlet instance.
在 web MVC 框架中,每个 DispatcherServlet 都有自己的 WebApplicationContext,它继承了根 WebApplicationContext 中已经定义的所有 bean。定义的这些继承的 bean 可以在 servlet 特定的范围内被覆盖,并且新的特定范围的 bean 可以在给定的 servlet 实例的本地定义。
The dispatcher servlet's application context is a web application context which is only applicable for the Web classes . You cannot use these for your middle tier layers . These need a global app context using ContextLoaderListener .
调度程序 servlet 的应用程序上下文是一个仅适用于 Web 类的 Web 应用程序上下文。您不能将这些用于您的中间层。这些需要使用 ContextLoaderListener 的全局应用程序上下文。
Read the spring reference herefor spring mvc .
在此处阅读 spring 参考以了解 spring mvc 。
回答by ank
And if you would like to use an existing context, rather than a new context which would be loaded from xml configuration by org.springframework.web.context.ContextLoaderListener, then see -> https://stackoverflow.com/a/40694787/3004747
如果您想使用现有上下文,而不是由 org.springframework.web.context.ContextLoaderListener 从 xml 配置加载的新上下文,请参阅 -> https://stackoverflow.com/a/40694787/ 3004747

