当我尝试运行 Web 应用程序时,我得到一个空白的登录页面。我仍在尝试掌握使用 Thymeleaf 和 Tiles 作为模板引擎的窍门。唯一的问题是我不断得到一个空白的登陆页面。我已经为瓷砖使用了 xml 配置,但它们的 thymeleaf 配置是通过 Java Config 完成的。我的代码如下。
瓷砖.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name="indexLayout" template="layouts/template" templateType="thymeleaf">
<put-attribute name="title" value="Afrikana - Sign in" type="thymeleaf" />
<put-attribute name="head" value="fragments/head :: head" type="thymeleaf" />
<put-attribute name="footer" value="fragments/footer :: footer" type="thymeleaf" />
</definition>
<!-- Index page -->
<definition name="login.html" extends="indexLayout">
<put-attribute name="content" value="pages/login :: content" type="thymeleaf" />
</definition>
</tiles-definitions>
模板.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org">
<head tiles:fragment="head">
<title tiles:string="title">
</title>
</head>
<body>
<div tiles:fragment="content"></div>
<div tiles:fragment="footer"></div>
</body>
</html>
header.html
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" th:href="@{/resources/css/bootstrap.css}" />
<link rel="stylesheet" th:href="@{/resources/css/bootstrap-responsive.css}" />
页脚.html
<footer>
<div class="container">
<p>
Built with all the love in the world by <a
href="http://www.exacode.net">Exacode</a>.
</p>
</div>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js"></script>
</footer>
内容.html
<div>
Hello World!!
</div>
登录.html
<h1>Login page</h1>
<p th:if="${loginError}">Wrong user or password</p>
<form th:action="@{/j_spring_security_check}" method="post">
<label for="j_username">Username</label>:
<input type="text" id="j_username" name="j_username" /> <br />
<label for="j_password">Password</label>:
<input type="password" id="j_password" name="j_password" /> <br />
<input type="submit" value="Log in" />
</form>
Thymeleaf Java 配置
@Configuration
public class ThymeleafConfig {
@Bean
public BeanNameViewResolver beanViewResolver() {
BeanNameViewResolver resolver = new BeanNameViewResolver();
resolver.setOrder(1);
return resolver;
}
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setOrder(1);
return resolver;
}
@Bean
public MarshallingHttpMessageConverter marshallingMessageConverter() {
MarshallingHttpMessageConverter converter= new MarshallingHttpMessageConverter(
jaxb2Marshaller(),
jaxb2Marshaller()
);
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.APPLICATION_XML);
converter.setSupportedMediaTypes(mediaTypes);
return converter;
}
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(stringHttpMessageConverter());
converters.add(marshallingJsonConverter());
converters.add(marshallingMessageConverter());
restTemplate.setMessageConverters(converters);
return restTemplate;
}
@Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
return new StringHttpMessageConverter();
}
@Bean
public MappingJacksonHttpMessageConverter marshallingJsonConverter(){
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.APPLICATION_JSON);
MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
converter.setSupportedMediaTypes(mediaTypes);
return converter;
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan(new String[]{
"org.tutiworks.orm.*"
});
return marshaller;
}
@Bean
public ThymeleafTilesConfigurer tilesConfigurer() {
ThymeleafTilesConfigurer tilesConfigurer = new ThymeleafTilesConfigurer();
tilesConfigurer.setDefinitions(new String[]{"/WEB-INF/tiles.xml"});
return tilesConfigurer;
}
@Bean
public TilesDialect tilesDialect(){
TilesDialect dialect = new TilesDialect();
return dialect;
}
@Bean
public SpringSecurityDialect springSecurityDialect(){
SpringSecurityDialect dialect = new SpringSecurityDialect();
return dialect;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
Set<IDialect> dialects = new HashSet<IDialect>();
dialects.add(springSecurityDialect());
dialects.add(tilesDialect());
engine.setAdditionalDialects(dialects);
engine.setTemplateResolver(templateResolver());
return engine;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setViewClass(ThymeleafTilesView.class);
resolver.setOrder(2);
resolver.setCharacterEncoding("UTF-8");
String[] views = {"*.html","*.xhtml"};
resolver.setViewNames(views);
return resolver;
}
@Bean
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
viewResolvers.add(beanViewResolver());
viewResolvers.add(thymeleafViewResolver());
resolver.setViewResolvers(viewResolvers);
resolver.setContentNegotiationManager(manager);
return resolver;
}
}
我的问题是为什么我在调用 url login.html 时总是得到一个空白页面?我的百里香 - 瓷砖设置做错了什么?