我第一次使用thymlead。我想有标题的主体和页脚。 我尝试时会遇到异常。
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "templates/layout", template might not exist or might not be accessible by any of the configured Template Resolvers
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
这是我的图块配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="base.definition" template="templates/layout">
<put-attribute name="title" value="" />
<put-attribute name="header" value="templates/header" />
<put-attribute name="menu" value="" />
<put-attribute name="body" value="templates/index" />
<put-attribute name="footer" value="templates/footer" />
</definition>
<definition name="index" extends="base.definition">
</definition>
</tiles-definitions>
这是我的mvc配置类。
package com.myproj.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.extras.tiles2.dialect.TilesDialect;
import org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer;
import org.thymeleaf.extras.tiles2.spring4.web.view.ThymeleafTilesView;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
@Configuration
@ComponentScan("com.myproj.spring")
@PropertySource("classpath:myproj.properties")
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/").setCachePeriod(31556926);
}
@Bean
public ClassLoaderTemplateResolver templateResolver() {
ClassLoaderTemplateResolver result = new ClassLoaderTemplateResolver();
result.setPrefix("/WEB-INF/views/");
result.setSuffix(".html");
result.setTemplateMode("HTML5");
result.setOrder(1);
return result;
}
@Bean
public ThymeleafTilesConfigurer tilesConfigurer() {
ThymeleafTilesConfigurer tilesConfigurer = new ThymeleafTilesConfigurer();
tilesConfigurer.setDefinitions(new String[] { "classpath:tiles/tiles-def.xml" });
return tilesConfigurer;
}
@Bean
public SpringTemplateEngine templateEngine(ClassLoaderTemplateResolver templateResolver) {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
templateEngine.addDialect(new TilesDialect());
return templateEngine;
}
@Bean
public ThymeleafViewResolver viewResolver(SpringTemplateEngine templateEngine) {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine);
viewResolver.setViewClass(ThymeleafTilesView.class);
return viewResolver;
}
}
和我的控制器
package com.myproj.spring.config;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class DefaultController {
@RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
public String index(HttpServletRequest request) {
System.out.println("index page");
return "index";
}
@RequestMapping(value="/login", method = RequestMethod.GET)
public String login(HttpServletRequest request) {
System.out.println("login page");
return "login";
}
}
这是我的项目结构
我使用的是Spring Tools Suite和嵌入式Tomcat 8。 有什么帮助吗?
答案 0 :(得分:4)
解决方案是将模板放在src \ main \ resources中。