Java 索引文件如何影响spring mvc项目?
我有一个SpringMVCWeb应用程序项目,我正在使用SpringToolsSuite进行,我正在使用maven添加依赖项,使用thymeleaf进行查看 我用基于代码的方法配置了我的项目,我没有xml文件 我没有您可以配置的默认页面Java 索引文件如何影响spring mvc项目?,java,spring,spring-mvc,thymeleaf,Java,Spring,Spring Mvc,Thymeleaf,我有一个SpringMVCWeb应用程序项目,我正在使用SpringToolsSuite进行,我正在使用maven添加依赖项,使用thymeleaf进行查看 我用基于代码的方法配置了我的项目,我没有xml文件 我没有您可以配置的默认页面 <welcome-file-list> <welcome-file></welcome-file> </welcome-file-list> 但是它会转到index.html页面而不是home.jsp,如
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
但是它会转到index.html页面而不是home.jsp,如果我像这样为index.html页面创建一个控制器
@RequestMapping(value = "/index")
public String showIndex() {
logger.info("***INDEX***");
return "index.html";
}
给我以下错误:
HTTP 500-请求处理失败;嵌套异常是org.thymeleaf.exceptions.TemplateInputException:异常解析文档:template=index.html,第56行-第4列
我认为这个错误是因为我使用的是thymeleaf,在这一行中,我有一个打开的标记,而没有一个关闭的标记
我的问题是,如果我没有创建一个控制器,我的index.html页面为什么会工作,而且工作正常,但是如果我像我想做的那样创建一个控制器,它就不工作了
我的config类中只有一个viewResolver,是thymeleaf视图解析器,如果我只有一个解析器,这个index.html怎么能显示得毫无问题呢
这是我的配置类
@EnableWebMvc
@ComponentScan(basePackages = {"com.abc.myapp"})
@Configuration
public class ConfigApp extends WebMvcConfigurerAdapter{
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/");
// resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setOrder(1);
resolver.setCacheable(false);
return resolver;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:messages/messages");
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(0);// # -1 : never reload, 0 always reload
return messageSource;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
engine.setMessageSource(messageSource());
return engine;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCache(false);
return resolver;
}
这是我的webInitializer类
public class InitiaApp extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { ConfigRoot.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { ConfigApp .class };
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
当您不使用控制器时,请不要使用Thymeleaf视图解析器。这意味着浏览器是一个翻译html文件的浏览器,它更加宽容,并且不需要严格的HTML5。使用控制器时,您必须使用Thymeleaf视图解析器。在视图解析器中,将模板模式设置为HTML5。此外,Thymeleaf还要求使用格式良好的XML,这意味着每个打开的标记都需要一个关闭标记。请阅读Thymeleaf文件的1.2以了解进一步的说明 您应该给出更详细的答案。我想web服务器仍在尝试使用合理的默认值来显示欢迎页面。在您的情况下,由于web服务器可以在根上下文中找到index.html,因此它只显示页面,而不向spring控制器发送请求。在第二种情况下,您实际上是在调用返回thymeleaf视图的控制器。Thymeleaf需要格式良好的xml才能在web浏览器中正确显示。如果删除index.html并使用其他一些视图名称,则一切都应按预期进行
public class InitiaApp extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { ConfigRoot.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { ConfigApp .class };
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
@Configuration
public class ConfiguracionRoot {
}