No Webapplicationcontext Found: No Contextloaderlistener Or Dispatcherservlet Registered?
No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered?
在使maven构建SpringMVC 国际化(i18n) 项目中中出现页面显示异常,尝试查找了所有答案。一度怀疑是tomcat出了问题,但是经过测试得出tomcat是正常的,以下是项目的配置和运行异常。
jsp页面报错信息
Eclipse 的控制台报出的错误:
十二月 xiv, 2017 ane:52:xl 下午 org.springframework .spider web .servlet .tags .MessageTag doStartTag 严重: No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered? coffee.lang.IllegalStateException: No WebApplicationContext institute: not in a DispatcherServlet request and no ContextLoaderListener registered? at org.springframework .web .servlet .support .RequestContext .initContext(RequestContext.java:235) at org.springframework .web .servlet .support .JspAwareRequestContext .initContext(JspAwareRequestContext.java:75) at org.springframework .web .servlet .support .JspAwareRequestContext.<init>(JspAwareRequestContext.java:49) at org.springframework .web .servlet .tags .RequestContextAwareTag .doStartTag(RequestContextAwareTag.java:77) at org.apache .jsp .welcome_jsp._jspx_meth_spring_005fmessage_005f0(welcome_jsp.java:167) at org.apache .jsp .welcome_jsp._jspService(welcome_jsp.java:129) at org.apache .jasper .runtime .HttpJspBase .service(HttpJspBase.java:70) at javax.servlet .http .HttpServlet .service(HttpServlet.java:729) at org.apache .jasper .servlet .JspServletWrapper .service(JspServletWrapper.java:438) at org.apache .jasper .servlet .JspServlet .serviceJspFile(JspServlet.coffee:396) at org.apache .jasper .servlet .JspServlet .service(JspServlet.java:340) at javax.servlet .http .HttpServlet .service(HttpServlet.java:729) at org.apache .catalina .cadre .ApplicationFilterChain .internalDoFilter(ApplicationFilterChain.java:292) at org.apache .catalina .core .ApplicationFilterChain .doFilter(ApplicationFilterChain.java:207) at org.apache .tomcat .websocket .server .WsFilter .doFilter(WsFilter.coffee:52) at org.apache .catalina .core .ApplicationFilterChain .internalDoFilter(ApplicationFilterChain.java:240) at org.apache .catalina .core .ApplicationFilterChain .doFilter(ApplicationFilterChain.java:207) at org.apache .catalina .core .StandardWrapperValve .invoke(StandardWrapperValve.java:212) at org.apache .catalina .core .StandardContextValve .invoke(StandardContextValve.coffee:106) at org.apache .catalina .authenticator .AuthenticatorBase .invoke(AuthenticatorBase.java:502) at org.apache .catalina .core .StandardHostValve .invoke(StandardHostValve.java:141) at org.apache .catalina .valves .ErrorReportValve .invoke(ErrorReportValve.java:79) at org.apache .catalina .valves .AbstractAccessLogValve .invoke(AbstractAccessLogValve.java:616) at org.apache .catalina .core .StandardEngineValve .invoke(StandardEngineValve.java:88) at org.apache .catalina .connector .CoyoteAdapter .service(CoyoteAdapter.java:528) at org.apache .coyote .httpeleven.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100) at org.apache .coyote .AbstractProtocol$AbstractConnectionHandler.procedure(AbstractProtocol.java:687) at org.apache .tomcat .util .internet .NioEndpoint$SocketProcessor.doRun(NioEndpoint.coffee:1520) at org.apache .tomcat .util .net .NioEndpoint$SocketProcessor.run(NioEndpoint.coffee:1476) at java.util .concurrent .ThreadPoolExecutor .runWorker(Unknown Source) at coffee.util .concurrent .ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache .tomcat .util .threads .TaskThread$WrappingRunnable.run(TaskThread.java:61) at coffee.lang .Thread .run(Unknown Source) 十二月 xiv, 2017 1:52:40 下午 org.apache .catalina .core .StandardWrapperValve invoke 严重: Servlet.service() for servlet [jsp] in context with path [/Springi18n] threw exception [java.lang .IllegalStateException: No WebApplicationContext found: non in a DispatcherServlet request and no ContextLoaderListener registered?] with root cause java.lang.IllegalStateException: No WebApplicationContext constitute: not in a DispatcherServlet request and no ContextLoaderListener registered? at org.springframework .web .servlet .back up .RequestContext .initContext(RequestContext.java:235) at org.springframework .spider web .servlet .support .JspAwareRequestContext .initContext(JspAwareRequestContext.coffee:75) at org.springframework .spider web .servlet .back up .JspAwareRequestContext.<init>(JspAwareRequestContext.coffee:49) at org.springframework .spider web .servlet .tags .RequestContextAwareTag .doStartTag(RequestContextAwareTag.java:77) at org.apache .jsp .welcome_jsp._jspx_meth_spring_005fmessage_005f0(welcome_jsp.coffee:167) at org.apache .jsp .welcome_jsp._jspService(welcome_jsp.java:129) at org.apache .jasper .runtime .HttpJspBase .service(HttpJspBase.java:70) at javax.servlet .http .HttpServlet .service(HttpServlet.java:729) at org.apache .jasper .servlet .JspServletWrapper .service(JspServletWrapper.java:438) at org.apache .jasper .servlet .JspServlet .serviceJspFile(JspServlet.java:396) at org.apache .jasper .servlet .JspServlet .service(JspServlet.java:340) at javax.servlet .http .HttpServlet .service(HttpServlet.java:729) at org.apache .catalina .core .ApplicationFilterChain .internalDoFilter(ApplicationFilterChain.coffee:292) at org.apache .catalina .core .ApplicationFilterChain .doFilter(ApplicationFilterChain.java:207) at org.apache .tomcat .websocket .server .WsFilter .doFilter(WsFilter.java:52) at org.apache .catalina .core .ApplicationFilterChain .internalDoFilter(ApplicationFilterChain.java:240) at org.apache .catalina .cadre .ApplicationFilterChain .doFilter(ApplicationFilterChain.java:207) at org.apache .catalina .cadre .StandardWrapperValve .invoke(StandardWrapperValve.java:212) at org.apache .catalina .core .StandardContextValve .invoke(StandardContextValve.coffee:106) at org.apache .catalina .authenticator .AuthenticatorBase .invoke(AuthenticatorBase.coffee:502) at org.apache .catalina .core .StandardHostValve .invoke(StandardHostValve.coffee:141) at org.apache .catalina .valves .ErrorReportValve .invoke(ErrorReportValve.java:79) at org.apache .catalina .valves .AbstractAccessLogValve .invoke(AbstractAccessLogValve.java:616) at org.apache .catalina .core .StandardEngineValve .invoke(StandardEngineValve.java:88) at org.apache .catalina .connector .CoyoteAdapter .service(CoyoteAdapter.java:528) at org.apache .coyote .http11.AbstractHttp11Processor.procedure(AbstractHttp11Processor.coffee:1100) at org.apache .coyote .AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687) at org.apache .tomcat .util .net .NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520) at org.apache .tomcat .util .net .NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476) at java.util .concurrent .ThreadPoolExecutor .runWorker(Unknown Source) at coffee.util .concurrent .ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache .tomcat .util .threads .TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang .Thread .run(Unknown Source)
项目构建样式
ane.web.xml的相关配置
spider web.xml
<?xml version="one.0" encoding="UTF-eight"?> <spider web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-example" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-proper name>Springi18n</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> <!-- web.xml加载的时候初始化到welcom.jsp页面 --> </welcome-file-list> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- springMVC 映射服务名称 --> <url-pattern>/springmvc/*</url-pattern> <!-- 映射路径,协议+主机+端口+项目名+其他(具体使用的方法),http://localhost:8080/Springi18n/ --> </servlet-mapping> <servlet> <servlet-name>springmvc</servlet-name> <!-- springMVC 服务名称 ,注意这里的 <servlet-mapping/>和<servlet/>的<servlet-proper noun/> 的值需要一样 --> <servlet-class>org.springframework.spider web.servlet.DispatcherServlet</servlet-class> <!-- 使用使用spring框架的 DispatcherServlet调度服务类 --> <init-param> <param-name>contextConfigLocation</param-name> <!-- context配置位置 --> <param-value>classpath:applicationContext.xml</param-value> <!-- 加载类路径下的 XML文件 --> </init-param> <load-on-startup>1</load-on-startup> <!-- web服务加载的优先级 ,正整数数值小的优先级高 --> </servlet> </spider web-app>
这里是web.xml配置,当web.xml加载的时候初始化页面是
2.pox.xml文件的相关配置
pox.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>iv.0.0</modelVersion> <groupId>online.shixun.springi18n</groupId> <artifactId>Springi18n</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Springi18n Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <leap-framework.version>4.3.10.RELEASE</spring-framework.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.eight.one</version> <scope>test</telescopic> </dependency> <!-- spring-cadre --> <dependency> <groupId>org.springframework</groupId> <artifactId>jump-core</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${bound-framework.version}</version> </dependency> <!-- spring-mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/antiquity/javax.servlet/servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.one.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>i.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-cadre --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> <scope>test</telescopic> </dependency> </dependencies> <build> <finalName>Springi18n</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>imitation</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
three.springMVC 的applicationContext.xml文件的配置
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://world wide web.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://world wide web.springframework.org/schema/beans/bound-beans-4.0.xsd http://www.springframework.org/schema/mvc http://world wide web.springframework.org/schema/mvc/jump-mvc-4.0.xsd http://world wide web.springframework.org/schema/context http://www.springframework.org/schema/context/jump-context-iv.0.xsd "> <!-- 扫描包 --> <context:component-scan base-package="online.shixun.springi18n.controllers"> </context:component-scan> <!-- 启动注解标签,控制 --> <mvc:annotation-driven /> <!-- 试图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="suffix" value=".jsp" /> <holding name="prefix" value="/" /> </edible bean> <!-- messageSource bean 配置系统message的资源 --> <bean id="messageSource" course="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <!-- 国际化信息所在的文件名,加载messages 目录下,所有以message开头的priperties 资源文件 --> <property proper name="basename" value="classpath:messages" /> <!-- 默认编码字符集 --> <property name="defaultEncoding" value="UTF-eight" /> <!-- 如果国际化资源文件中找不到对应代码的信息,则使用默认资源文件,即:message.backdrop --> <property proper noun="useCodeAsDefaultMessage" value="true" /> </bean> <!-- 用于提取请求包含的区域语言的拦截器 --> <mvc:interceptors> <bean course="org.springframework.spider web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="lang"> </property> </bean> </mvc:interceptors> </beans>
4.两个国际化资资源文件的配置("zh"是 中文,"en" 是 英文)
messages_en_US.properties
app=shixun.online username= user name password=password login=login welcome=welcome
messages_zh_CN.properties
app=\u8F7B\u5B9E\u8BAD username=\u7528\u6237\u540D password=\u5BC6\u7801 login=\u767B\u5F55 welcome=\u6B22\u8FCE
注意:"乱码"是经过Unicode编码的,例如"\u6B22\u8FCE"的中文是"欢迎"
v.springMVC控制跳转配置
LoginController.coffee
package online.shixun .springi18n.controllers ; import java.util .Locale ; import org.springframework .stereotype .Controller ; import org.springframework .ui .Model ; import org.springframework .spider web .bind .notation .RequestMapping ; import org.springframework .web .bind .note .RequestMethod ; import org.springframework .spider web .bind .notation .RequestParam ; import org.springframework .spider web .servlet .ModelAndView ; @Controller public grade LoginController { /** * 判断springMVC是否连接成功 * @render */ @RequestMapping(value = "login", method = RequestMethod.Go) public ModelAndView login() { Organisation.out .println("222355"); return new ModelAndView("welcome"); } }
half-dozen. login.jsp和welcome.jsp页面的主要body配置
welcome.jsp
<%@ page linguistic communication="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML iv.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Blazon" content="text/html; charset=UTF-eight"> <title>欢迎</championship> </head> <trunk> <jump:bulletin lawmaking="welcome"/>:${username } </torso> </html>
login.jsp
<%@ folio linguistic communication="java" contentType="text/html; charset=UTF-viii" pageEncoding="UTF-8"%> <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML iv.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登陆</title> </head> <body> <div style="text-align: center;"> <h2> <leap:message code="app" /> </h2> <form activeness="${pageContext.request.contextPath }/springmvc/welcome" method="postal service"> <input name="username" style="width: 200px;" type="text" placeholder='<spring:message code="username"/>'> <br> <br> <input style="width: 200px;" type="text" placeholder='<spring:message code="password"/>'> <br> <br> <input fashion="width: 80px;" type="submit" value='<spring:message code="login"/>'> </form> <br> <a href="${pageContext.request.contextPath }/springmvc/loginPage?lang=zh_CN">中文</a>/<a href="${pageContext.request.contextPath }/springmvc/loginPage?lang=en_US">Englist</a> </div> </trunk> </html>
错误分析:
-
翻译:没有发现WebApplicationContext:不是在DispatcherServlet请求中,也不存在已注册的ContextLoaderListener ? (是有道翻译)
-
web容器在加载web.xml的时候,初始化页面是web.xml配置中的login.jsp页面,login需要引用国际化(i18n)的文件并没有被WebApplicationContext加载。
-
解决思路:通过SpringmMVC 的 Controller视图跳转,来加载WebApplicationContext。然而该Context存在了,会自动加载国际化文件。
-
总结:初始化页面是,引用了未创建WebApplicationContext中的国际化文件,是不合法的,必须先创建好了Contxt容器了,才可以引用容器的资源。
- 笔者是编程小白,初次写博客还存在许多不足之处,希望能够得到批评和指正。
调试成功后的运行结果:
- 中文国际化
- 英文国际化
Source: https://blog.csdn.net/qq_30906613/article/details/78809921
Posted by: brownlizeatied.blogspot.com
0 Response to "No Webapplicationcontext Found: No Contextloaderlistener Or Dispatcherservlet Registered?"
Post a Comment