使用由多个子视图组成的复合视图。整个模板的每一个子视图能够总体动态地包括在内,而且能够独立于内容来管理页面的布局。css
Apache Tiles和SiteMesh 框架使用Composite View Design Pattern。html
为简单起见,这种模式分为若干部分,如问题,动因,结构,解决方案,实施等。java
目录
- 问题
- 动因
- 解决方案
- 说明
- 结构 - 类图,序列图
- 参与者和责任
- 履行
- 后果
- 适用性
- 现实世界的例子
- 参考
(问题部分描述了开发人员面临的设计问题)spring
您但愿从模块化的原子组件部件构建视图,这些部件组合在一块儿以建立复合总体,同时独立地管理内容和布局。sql
(本节描述了列出影响问题和解决方案的缘由和动机。动因列表突出了人们可能选择使用模式并提供使用模式的理由的缘由)apache
使用由多个原子子视图组成的复合视图。整个模板的每一个子视图能够总体动态地包括在内,而且能够独立于内容来管理页面的布局。设计模式
例如,门户网站包含许多独立的子视图,例如新闻源,天气信息和单个页面上的股票报价。能够独立于内容来管理页面的布局。bash
此模式的另外一个好处是,Web设计人员能够对站点的布局进行原型设计,将静态内容插入每一个模板区域。随着站点开发的进展,实际内容将替换这些占位符。该方法提供了改进的模块化和可重用性,以及改进的可维护性。架构
咱们使用UML类图来显示解决方案的基本结构,本节中的UML序列图介绍了解决方案的动态机制。并发
下面是表示Composite View Design Pattern关系的类图。
Client - 客户端发送到视图。
View- 视图。
SimpleView- 表示的复合总体的原子部分。它也被称为视图片断或子视图。
CompositeView- 复合视图由多个视图组成。这些视图中的每个要么是一个简单视图,要么自己多是一个复合视图。
Template- 模板,表明视图布局。
为了理解这种模式,咱们举一个例子。在下图中,您能够看到网页的典型结构。
这种结构称为“经典布局”。模板根据此布局组织页面,将每一个“块”放在所需的位置,以使标题上升,页脚向下等。
可能会发生这种状况,例如点击连接,只须要更改页面的一部分,一般是正文。
如您所见,页面不一样,但它们的区别仅在于正文部分。可是请注意,页面是不一样的,它不像框架集中的框架刷新!
使用复合视图模式,页面的其余部分已被重用,而且已保留布局一致性。
在此示例中,View管理是使用标准JSP标记实现的,例如jsp:include标记。使用标准标签来管理视图的布局和组合是一种易于实施的策略。
标准标签视图管理策略示例 :
<html>
<body>
<jsp:include
page="/jsp/CompositeView/javabean/banner.seg" flush="true"/>
<table width="100%">
<tr align="left" valign="middle">
<td width="20%">
<jsp:include page="/jsp/CompositeView/javabean/ProfilePane.jsp"
flush="true"/>
</td>
<td width="70%" align="center">
<jsp:include page="/jsp/CompositeView/javabean/mainpanel.jsp"
lush="true"/>
</td>
</tr>
</table>
<jsp:include page="/jsp/CompositeView/javabean/footer.seg"
flush="true"/>
</body>
</html>
复制代码
Apache Tiles是一个免费的开源模板框架,彻底基于Composite设计模式。在Apache Tiles中,经过组合称为Tiles的子视图组合来构建页面。
第1步: 提供依赖关系配置
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.7</version>
</dependency>
复制代码
第2步:定义平铺布局文件
<tiles-definitions>
<definition name="template-def"
template="/WEB-INF/views/tiles/layouts/defaultLayout.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header"
value="/WEB-INF/views/tiles/templates/defaultHeader.jsp" />
<put-attribute name="menu"
value="/WEB-INF/views/tiles/templates/defaultMenu.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer"
value="/WEB-INF/views/tiles/templates/defaultFooter.jsp" />
</definition>
<definition name="home" extends="template-def">
<put-attribute name="title" value="Welcome" />
<put-attribute name="body"
value="/WEB-INF/views/pages/home.jsp" />
</definition>
</tiles-definitions>
复制代码
第3步: ApplicationConfiguration和其余类
@Controller
@RequestMapping("/")
public class ApplicationController {
@RequestMapping(
value = {
"/"
},
method = RequestMethod.GET)
public String homePage(ModelMap model) {
return "home";
}
@RequestMapping(
value = {
"/apachetiles"
},
method = RequestMethod.GET)
public String productsPage(ModelMap model) {
return "apachetiles";
}
@RequestMapping(
value = {
"/springmvc"
},
method = RequestMethod.GET)
public String contactUsPage(ModelMap model) {
return "springmvc";
}
}
public class ApplicationInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class << ? > [] getRootConfigClasses() {
return new Class[] {
ApplicationConfiguration.class
};
}
@Override
protected Class << ? > [] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] {
"/"
};
}
}
复制代码
最后,在ApplicationConfiguration类中,咱们使用TilesConfigurer和TilesViewResolver类来实现集成:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.baeldung.tiles.springmvc")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
tilesConfigurer.setDefinitions(
new String[] {
"/WEB-INF/views/**/tiles.xml"
});
tilesConfigurer.setCheckRefresh(true);
return tilesConfigurer;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
TilesViewResolver viewResolver = new TilesViewResolver();
registry.viewResolver(viewResolver);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("/static/");
}
}
复制代码
第4步: 平铺模板文件
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><tiles:getAsString name="title" /></title>
<link href="<c:url value='/static/css/app.css' />"
rel="stylesheet">
</link>
</head>
<body>
<div class="flex-container">
<tiles:insertAttribute name="header" />
<tiles:insertAttribute name="menu" />
<article class="article">
<tiles:insertAttribute name="body" />
</article>
<tiles:insertAttribute name="footer" />
</div>
</body>
</html>
复制代码
最后,欢迎作Java的工程师朋友们加入Java高级架构进阶Qqun:963944895
群内有技术大咖指点难题,还提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)
比你优秀的对手在学习,你的仇人在磨刀,你的闺蜜在减肥,隔壁老王在练腰, 咱们必须不断学习,不然咱们将被学习者超越!
趁年轻,使劲拼,给将来的本身一个交代!