基于Spring-Tiles注释的配置

时间:2016-03-17 12:15:39

标签: spring spring-mvc tiles

您好我是Spring框架中的新手,在我的应用程序中,我需要将Spring与Tiles集成。如何为我的登录页面添加viewResolver Tiles和internalViewResolver,或者如何将其作为一个。我使用基于注释的配置。我的登录页面位置是

enter image description here

我的配置类是

public class AppConfig  extends WebMvcConfigurerAdapter  {

@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");

    return viewResolver;
}

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    return messageSource;
}
     ........//

}

我应该在哪里放置我的Tiles JSP。我希望我的问题很清楚,请评论以获取更多信息。谢谢!

1 个答案:

答案 0 :(得分:1)

您应该使用UrlBasedViewResolver视图解析程序而不是InternalResourceViewResolver,因为后缀,视图名称和前缀将在Tiles Definition XML文件中指定。

@Bean 
public UrlBasedViewResolver viewResolver() { 
    UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
    viewResolver.setViewClass(TilesView.class);
    return viewResolver;
} 


@Bean 
public TilesConfigurer tilesConfigurer() { 
    TilesConfigurer tilesConfigurer = new TilesConfigurer();
    tilesConfigurer.setDefinitions(new String[]{
            "/WEB-INF/defs/general.xml", 
            "/WEB-INF/defs/tiles.xml"  //another definition
    }); 
    tilesConfigurer.setCheckRefresh(true);
    return tilesConfigurer;
} 

为每个视图添加Tiles Def XML文件,如下所示:

对于网址路径http://localhost:8080/register

@RequestMapping("/register") 

定义:

<definition name="page" template="/WEB-INF/layout/page.jsp">
    <put-list-attribute name="stylesheets">
        <add-attribute value="/resources/css/AdminLTE.min.css" />
    </put-list-attribute>
    <put-list-attribute name="javascripts">
        <add-attribute value="/resources/js/main.js" />
    </put-list-attribute>
</definition>

<definition name="register" extends="page">
    <put-attribute name="title" value="User Registeration" />
    <put-attribute name="body" value="/WEB-INF/jsp/register.jsp" />
    <put-attribute name="current" value="register" />
</definition>   

我有一个关于Spring Tiles的Github示例,但是它正在使用XML Configuration,您可以使用上面的@Bean Configuration来使它使用JavaConfig。