Spring 启动应用程序不提供网页内容
Spring Boot application do not serve web content
我有 Spring 最初为内部 Tomcat 服务器构建的启动 Web 应用程序(有效)。
然后我将该应用程序采用为 Web 逻辑服务器上的 运行。
我的应用程序可以毫无问题地编译并部署到服务器,但是当它不为 MVC 页面提供服务时。
每次调用都会抛出 404 错误。
从下面的错误来看,似乎 spring dispatcher servlet 存在,但甚至没有正确设置语言环境。
我不知道这里有什么问题或遗漏但是当我创建 RestController 而不是 MVC 时
作品和内容出现。应用程序基于 Sprint Boot 2.3.2
<16 Aug 2020, 14:12:32,792 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:32.792 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] WARN org.springframework.web.servlet.PageNotFound - No mapping for GET /alf/home> <16 Aug 2020, 14:12:34,922 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:34.922 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.context.support.ReloadableResourceBundleMessageSource - No properties file found for [messages] - neither plain properties nor XML> <16 Aug 2020, 14:12:34,922 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:34.922 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.context.support.ReloadableResourceBundleMessageSource - No properties file found for [messages_en_IE] - neither plain properties nor XML> <16 Aug 2020, 14:12:34,922 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:34.922 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.context.support.ReloadableResourceBundleMessageSource - No properties file found for [messages_en] - neither plain properties nor XML> <16 Aug 2020, 14:12:34,923 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:34.923 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.context.support.ReloadableResourceBundleMessageSource - No properties file found for [messages_en_US] - neither plain properties nor XML> <16 Aug 2020, 14:12:34,969 British Summer Time> <Error> <HTTP> <BEA-101107> <[ServletContext@2002815395[app:alf_war_exploded module:alf-2.0.1 path:null spec-version:4.0]] Problem occurred while serving the error page. javax.servlet.jsp.JspTagException: No message found under code 'page.error.title.text' for locale 'en_US'. at org.springframework.web.servlet.tags.MessageTag.doEndTag(MessageTag.java:294) at jsp_servlet._web_45_inf._pages._error.__page_45_404._jsp__tag0(__page_45_404.java:199) at jsp_servlet._web_45_inf._pages._error.__page_45_404._jspService(page-404.jsp:9) at weblogic.servlet.jsp.JspBase.service(JspBase.java:35) at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:295) Truncated. see log file for complete stacktrace> <16 Aug 2020, 14:12:34,972 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:34.972 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 404 NOT_FOUND> <16 Aug 2020, 14:12:34,974 British Summer Time> <Notice> <Stdout> <BEA-000000> <[[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] [2020-08-16T14:12:34,973] [10.2.16.95-agr5311] INFO AutoLoginFilter::doFilter - END 2020-08-16 14:12:34:972,agr5311,10.2.16.95,2204,2193,11,/alf/home>
申请class:
@SpringBootApplication
@ComponentScan({"ie.gov.agriculture.alf", "ie.gov.agriculture.sso"})
public class AlfApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
public AlfApplication() {
super();
setRegisterErrorPageFilter(false); // disable ErrorPageFilter causing bullshit error messages
}
@Override
public void onStartup(ServletContext servletContext) {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(AppConfig.class);
appContext.register(WebMvcConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
ContextLoaderListener contextLoaderListener = new ContextLoaderListener(appContext);
servletContext.addListener(contextLoaderListener);
// Filter.
FilterRegistration.Dynamic filterReg = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
filterReg.setInitParameter("encoding", "UTF-8");
filterReg.setInitParameter("forceEncoding", "true");
filterReg.addMappingForUrlPatterns(null, true, "/*");
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(AlfApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(AlfApplication.class, args);
}
}
AppConfig class:
@Configuration
@EnableCaching
public class AppConfig {
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
@Bean
public LocalValidatorFactoryBean messageValidator() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(this.messageSource());
return validator;
}
@Bean
public ConversionServiceFactoryBean convertionService() {
return new ConversionServiceFactoryBean();
}
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("alfCache");
}
}
最后 WebMvcConfig class:
@Configuration
@ServletComponentScan
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public ViewResolver getViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Bean
public CommonsMultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
@Bean
public CookieLocaleResolver cookieLocaleResolverExample() {
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setDefaultLocale(Locale.ENGLISH);
localeResolver.setCookieName("alf-locale-cookie");
localeResolver.setCookieMaxAge(3600);
return localeResolver;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.UK);
localeResolver.setDefaultTimeZone(TimeZone.getTimeZone("UTC"));
return localeResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
这个问题的答案总是一个简单的技巧,消息配置 bean 应该看起来像这样并且一切正常,关键是 setBasenema
中的“类路径:”
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
return messageSource;
}
我有 Spring 最初为内部 Tomcat 服务器构建的启动 Web 应用程序(有效)。 然后我将该应用程序采用为 Web 逻辑服务器上的 运行。 我的应用程序可以毫无问题地编译并部署到服务器,但是当它不为 MVC 页面提供服务时。 每次调用都会抛出 404 错误。 从下面的错误来看,似乎 spring dispatcher servlet 存在,但甚至没有正确设置语言环境。 我不知道这里有什么问题或遗漏但是当我创建 RestController 而不是 MVC 时 作品和内容出现。应用程序基于 Sprint Boot 2.3.2
<16 Aug 2020, 14:12:32,792 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:32.792 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] WARN org.springframework.web.servlet.PageNotFound - No mapping for GET /alf/home> <16 Aug 2020, 14:12:34,922 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:34.922 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.context.support.ReloadableResourceBundleMessageSource - No properties file found for [messages] - neither plain properties nor XML> <16 Aug 2020, 14:12:34,922 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:34.922 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.context.support.ReloadableResourceBundleMessageSource - No properties file found for [messages_en_IE] - neither plain properties nor XML> <16 Aug 2020, 14:12:34,922 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:34.922 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.context.support.ReloadableResourceBundleMessageSource - No properties file found for [messages_en] - neither plain properties nor XML> <16 Aug 2020, 14:12:34,923 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:34.923 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.context.support.ReloadableResourceBundleMessageSource - No properties file found for [messages_en_US] - neither plain properties nor XML> <16 Aug 2020, 14:12:34,969 British Summer Time> <Error> <HTTP> <BEA-101107> <[ServletContext@2002815395[app:alf_war_exploded module:alf-2.0.1 path:null spec-version:4.0]] Problem occurred while serving the error page. javax.servlet.jsp.JspTagException: No message found under code 'page.error.title.text' for locale 'en_US'. at org.springframework.web.servlet.tags.MessageTag.doEndTag(MessageTag.java:294) at jsp_servlet._web_45_inf._pages._error.__page_45_404._jsp__tag0(__page_45_404.java:199) at jsp_servlet._web_45_inf._pages._error.__page_45_404._jspService(page-404.jsp:9) at weblogic.servlet.jsp.JspBase.service(JspBase.java:35) at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:295) Truncated. see log file for complete stacktrace> <16 Aug 2020, 14:12:34,972 British Summer Time> <Notice> <Stdout> <BEA-000000> <14:12:34.972 [[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 404 NOT_FOUND> <16 Aug 2020, 14:12:34,974 British Summer Time> <Notice> <Stdout> <BEA-000000> <[[ACTIVE] ExecuteThread: '10' for queue: 'weblogic.kernel.Default (self-tuning)'] [2020-08-16T14:12:34,973] [10.2.16.95-agr5311] INFO AutoLoginFilter::doFilter - END 2020-08-16 14:12:34:972,agr5311,10.2.16.95,2204,2193,11,/alf/home>
申请class:
@SpringBootApplication
@ComponentScan({"ie.gov.agriculture.alf", "ie.gov.agriculture.sso"})
public class AlfApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
public AlfApplication() {
super();
setRegisterErrorPageFilter(false); // disable ErrorPageFilter causing bullshit error messages
}
@Override
public void onStartup(ServletContext servletContext) {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(AppConfig.class);
appContext.register(WebMvcConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
ContextLoaderListener contextLoaderListener = new ContextLoaderListener(appContext);
servletContext.addListener(contextLoaderListener);
// Filter.
FilterRegistration.Dynamic filterReg = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
filterReg.setInitParameter("encoding", "UTF-8");
filterReg.setInitParameter("forceEncoding", "true");
filterReg.addMappingForUrlPatterns(null, true, "/*");
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(AlfApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(AlfApplication.class, args);
}
}
AppConfig class:
@Configuration
@EnableCaching
public class AppConfig {
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
@Bean
public LocalValidatorFactoryBean messageValidator() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(this.messageSource());
return validator;
}
@Bean
public ConversionServiceFactoryBean convertionService() {
return new ConversionServiceFactoryBean();
}
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("alfCache");
}
}
最后 WebMvcConfig class:
@Configuration
@ServletComponentScan
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public ViewResolver getViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Bean
public CommonsMultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
@Bean
public CookieLocaleResolver cookieLocaleResolverExample() {
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setDefaultLocale(Locale.ENGLISH);
localeResolver.setCookieName("alf-locale-cookie");
localeResolver.setCookieMaxAge(3600);
return localeResolver;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.UK);
localeResolver.setDefaultTimeZone(TimeZone.getTimeZone("UTC"));
return localeResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
这个问题的答案总是一个简单的技巧,消息配置 bean 应该看起来像这样并且一切正常,关键是 setBasenema
中的“类路径:”@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
return messageSource;
}