Thymeleaf + Apache Tiles配置+ Spring

时间:2013-07-08 09:24:36

标签: spring spring-security tiles thymeleaf apache-tiles

当我尝试运行Web应用程序时,我得到一个空白的登录页面。我仍在尝试使用Thymeleaf和Tiles作为模板引擎。唯一的问题是我一直在设置一个空白的目标网页。我已经为tile使用了xml配置,但是它们的百万美元配置是通过Java Config完成的。我的代码如下。


tiles.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>

template.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}" />

footer.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>

content.html

<div>
    Hello World!!
</div>

的login.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 Config

@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时我会一直收到空白页?在我的百万富翁 - 瓷砖设置中出了什么问题?

0 个答案:

没有答案