问题
我有一个使用spring-mvc 5.1.8.RELEASE和thymeleaf 3.0.11.RELEASE-junit和log4j的简单动态Web应用程序。
我的项目是通过xml文件配置的
当我启动我的应用程序时,我得到了错误消息:
Request processing failed; nested exception is java.lang.IllegalStateException: ApplicationObjectSupport instance [org.thymeleaf.spring5.view.ThymeleafView@54e2c72d] does not run in an ApplicationContext
我搜索了类似的内容,但是我没有找到-也许我使用了错误的搜索词?
设置
我使用spring-mvc和thymeleaf设置了一个“简单”的maven-java-web-project。当前它由两个控制器(StartController
和ErrorController
),一个拦截器,两个jsp(start.jsp
和error.jsp
-用于测试目的),两个html文件({ {1}}和start.html
),error.html
,web.xml
和pom。
设置-web.xml:
spring-servlet.xml
设置-spring-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-name>WebAppThymeleaf</display-name>
<distributable />
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error</location>
</error-page>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
设置-StartController(百里香)
<beans:beans
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd"
default-autowire="byType"
>
<context:component-scan
base-package="de.cmr.**"
annotation-config="true" />
<mybatis:scan base-package="de.cmr.**" />
<mvc:annotation-driven>
<mvc:message-converters>
<beans:bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter"
>
<beans:property
name="supportedMediaTypes"
>
<beans:list>
<beans:value>video/mp4</beans:value>
</beans:list>
</beans:property>
</beans:bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:resources
location="/resources/"
mapping="/resources/**" />
<!-- SpringResourceTemplateResolver automatically integrates with Spring's
own -->
<!-- resource resolution infrastructure, which is highly recommended. -->
<beans:bean
id="templateResolver"
class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"
>
<beans:property
name="prefix"
value="/WEB-INF/templates/" />
<beans:property
name="suffix"
value=".html" />
<!-- HTML is the default value, added here for the sake of clarity. -->
<beans:property
name="templateMode"
value="HTML" />
<!-- Template cache is true by default. Set to false if you want -->
<!-- templates to be automatically updated when modified. -->
<beans:property
name="cacheable"
value="true" />
</beans:bean>
<!-- SpringTemplateEngine automatically applies SpringStandardDialect and -->
<!-- enables Spring's own MessageSource message resolution mechanisms. -->
<beans:bean
id="templateEngine"
class="org.thymeleaf.spring5.SpringTemplateEngine"
>
<beans:property
name="templateResolver"
ref="templateResolver" />
<!-- Enabling the SpringEL compiler with Spring 4.2.4 or newer can speed up -->
<!-- execution in most scenarios, but might be incompatible with specific -->
<!-- cases when expressions in one template are reused across different data -->
<!-- ypes, so this flag is "false" by default for safer backwards compatibility. -->
<beans:property
name="enableSpringELCompiler"
value="true" />
</beans:bean>
<beans:bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<beans:property
name="templateEngine"
ref="templateEngine" />
<beans:property
name="order"
value="1" />
<beans:property
name="viewNames"
value="*.html,*.xhtml" />
</beans:bean>
<beans:bean
id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<beans:property
name="order"
value="2" />
<beans:property
name="prefix"
value="/WEB-INF/jsp/" />
<beans:property
name="suffix"
value=".jsp" />
</beans:bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping
path="/resources/**" />
<beans:bean
class="de.cmr.app.interceptors.AppMvcInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<mvc:default-servlet-handler />
</beans:beans>
设置-ErrorController(spring-mvc jsp)
package de.cmr.app.controller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.View;
import org.thymeleaf.spring5.view.ThymeleafView;
@Controller("StartController")
public class StartController {
private static final Logger LOG = LogManager.getLogger(StartController.class);
@GetMapping("/")
public View start() {
final ThymeleafView view = new ThymeleafView("start");
return view;
}
}
设置-拦截器
package de.cmr.app.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller("ErrorController")
public class ErrorController {
@GetMapping("/error")
public ModelAndView error() {
final ModelAndView modelAndView = new ModelAndView("error");
modelAndView.addObject("caption", "WebAppThymeleaf");
return modelAndView;
}
}
逻辑-应该发生什么
package de.cmr.app.interceptors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class AppMvcInterceptor
implements
HandlerInterceptor {
@Override
public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler,
final Exception ex) throws Exception {
// TODO Auto-generated method stub
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
@Override
public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler,
final ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
System.out.println("User-Agent: " + request.getHeader("user-agent"));
System.out.println("User-Agent-CPU: " + request.getHeader("ua-cpu"));
return HandlerInterceptor.super.preHandle(request, response, handler);
}
}
上通过百里香(start.html
)欢迎使用逻辑-实际情况
StartController
被称为StartController#start()
如期返回StartController#start()
ThymeleafView
抛出ThymeleafView#render()
IllegalStateException
)将用户引向error.jsp
详细的错误消息
ErrorController
我的问题
我在做什么错了?
我的配置出了什么问题?
编辑-2019-08-05
根据建议,我尝试了Spring Initializer
我还更改了Juli 31, 2019 6:14:10 NACHM. org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [spring] in context with path [/de.cmr.app.web.spring.thymeleaf] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: ApplicationObjectSupport instance [org.thymeleaf.spring5.view.ThymeleafView@54e2c72d] does not run in an ApplicationContext] with root cause
java.lang.IllegalStateException: ApplicationObjectSupport instance [org.thymeleaf.spring5.view.ThymeleafView@54e2c72d] does not run in an ApplicationContext
at org.springframework.context.support.ApplicationObjectSupport.getApplicationContext(ApplicationObjectSupport.java:146)
at org.springframework.web.context.support.WebApplicationObjectSupport.getWebApplicationContext(WebApplicationObjectSupport.java:109)
at org.springframework.web.context.support.WebApplicationObjectSupport.getServletContext(WebApplicationObjectSupport.java:133)
at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:198)
at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1371)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1117)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1056)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1839)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:835)
方法,以返回Startontroller#start()
而不是String
我将基于xml的配置更改为基于Java /基于注释的配置
我在ThymeleafView
中实现了ApplicationContextAware并将其手动设置为我的StartController
和
什么都不起作用,总是相同的错误