📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Table of contents |
---|
Problem |
Forces |
Solution |
Explanation |
Structure - Class Diagram, Sequence Diagram |
Participants and Responsibilities |
Implementation |
Consequences |
Applicability |
Real world examples |
References |
Problem
Forces
- You want common subviews, such as headers, footers, and tables reused in multiple views, which may appear in different locations within each page layout.
- You have content in subviews which might which frequently change or might be subject to certain access controls, such as limiting access to users in certain roles.
- You want to avoid directly embedding and duplicating subviews in multiple views which makes layout changes difficult to manage and maintain.
Solution
Structure
Class diagram
Sequence Diagram
Participants and Responsibilities
How this pattern works
Implementation
<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>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.7</version>
</dependency>
<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>
@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[] {
"/"
};
}
}
@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/");
}
}
<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>
Consequences
- Improves modularity and reuse
- Adds role-based or policy-based control
- Enhances maintainability
- Reduces maintainability
- Reduces performance
Real world examples
References
Related Presentation Tier Posts
- Intercepting Filter Design Pattern in Java
- Front Controller Design Pattern in Java
- Application Controller Design Pattern in Java
- View Helper Design Pattern in Java
- Composite View Design Pattern in Java
- Context Object Design Pattern in Java
Comments
Post a Comment
Leave Comment