Spring 未找到WebApplicationContext:未注册ContextLoaderListener?
我正在尝试创建一个简单的Spring3应用程序,并包含以下文件。请告诉我这个错误的原因 下面是我的web.xmlSpring 未找到WebApplicationContext:未注册ContextLoaderListener?,spring,spring-mvc,web.xml,dispatcher,Spring,Spring Mvc,Web.xml,Dispatcher,我正在尝试创建一个简单的Spring3应用程序,并包含以下文件。请告诉我这个错误的原因 下面是我的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.s
<?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>
最后是我的登录名
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;
}
}
您必须在web.xml中有一个ContextLoaderListener,它加载您的配置文件
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
org.springframework.web.context.ContextLoaderListener
您需要了解Web应用程序上下文和根应用程序上下文之间的区别
在WebMVC框架中,每个DispatcherServlet都有自己的WebApplicationContext,它
继承根WebApplicationContext中已定义的所有bean。这些都是遗传的
定义的bean可以在特定于servlet的范围内重写,也可以在特定于新范围内重写
bean可以在给定servlet实例的本地定义
DispatcherServlet的应用程序上下文是仅适用于web类的web应用程序上下文。您不能将这些用于中间层。这些需要使用ContextLoaderListener的全局应用程序上下文
阅读spring mvc的spring参考。如果您想使用现有上下文,而不是org.springframework.web.context.ContextLoaderListener从xml配置加载的新上下文,
然后请参见->这将需要一个applicationContext.xml,然后它将提供一个绑定error@Batman另外,您如何自动检测控制器。您需要一个上下文:组件扫描或一个“@Batman检查我输入的Spring引用。我是MVC编程的新手。我刚刚开始学习这个模型,我学习的第一个框架是Spring。我认为我的配置文件中有一些错误,因为它永远无法到达控制器,这意味着bean没有作为HTTPRequest的一部分初始化。请让我知道如何正确配置它们,我想我遗漏了一些小东西。另外,请注意,我没有扩展任何控制器,只是在我的应用程序中使用注释。控制器仅映射在dispatcher-servlet.xml中。除了web.xml之外,还可以在没有web.xml的情况下使用
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");
}
}
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;
}
}
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>