Quillion
Quillion

Reputation: 6476

Spring load and concatenate views

I am switching my project from CodeIgniter to Spring.
The thing I can do in CodeIgniter is load multiple views in sequence and create a new view.
For example let's say that everything inside my <head> tag is going to be identical across all the pages with exception of <title>. I could simply do $this->load->view('header', $header_data); where header represents the view name, and $header_data represents an array which contains value for the title.

In Spring however my controller does the following

@Controller
@RequestMapping("/")
public class IndexController
{
    @RequestMapping(method = RequestMethod.GET)
    public String index(ModelMap model)
    {
        model.addAttribute("title", "Home");
        return "index";
    }
}

It does not seem like I can load header, body, and footer separately like I could with php, and I can't pass different variables into each separate view.
Is there any way I could do this in spring? Any example, or a link will be greatly appreciated.

Upvotes: 0

Views: 741

Answers (2)

manish
manish

Reputation: 20135

In the Java world, a page (view) is generated from one or more templates. A template is (usually) a file, such as JSP, HTML, XHTML, etc. Most applications require pages to follow some sort of common layout(s), such as header, footer, sidebar, etc. This can be achieved by using some of the templates to describe the layout and then using page-specific templates to render the actual page content. Using this approach, anything that is common between pages (with similar layout) is coded in the layout templates and then populated by the page-specific templates.

Layouts can be implemented using one of two different methodologies - Decoration or Composition. Which methodology you (need to) use depends on the view technology you use (and there are far too many of them). For example, if you use pure JSP based templates, you can include common page elements from other templates using <jsp:include />, which is a form of Composition as the view is composed from various templates. FreeMarker and Velocity are two other popular view technologies that provide Composition based layouts. In contrast Tiles, SiteMesh and Thymeleaf provide Decoration based layouts where page templates simply declare which layout they use and the framework then decorates the page content with common layout elements.

You should experiment with some frameworks to determine the one that you will be most comfortable with. From the example you have provided, you will probably be better off with a Decoration based framework like Tiles, SiteMesh or Thymeleaf so you may want to explore those first.

Upvotes: 1

Master Slave
Master Slave

Reputation: 28559

With Spring MVC you're not locked-in on a particular view technology. How this relates to your question is that it means that you can integrate Spring MVC with Apache Tiles a free open-source template that simplifies the development of user interfaces.

Here is a good article with a sample source, explaining the integration and the benefits http://java.dzone.com/articles/spring-mvc-tiles-3-integration. In short, tiles will help you define a basic template of your pages, and give you means to swap the dynamic parts with a single response. The following exemplary tiles definition defines a template that should be self-explanatory

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE tiles-definitions PUBLIC  
        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"  
        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>

    <definition name="DefaultTemplate" 
      template="/WEB-INF/views/template/SiteTemplate.jsp">
        <put-attribute name="title"     value="Home" />
        <put-attribute name="header"    value="/WEB-INF/views/template/header.jsp" />
        <put-attribute name="menu"      value="/WEB-INF/views/template/menu.jsp" />
        <put-attribute name="body"      value="" />
        <put-attribute name="footer"    value="/WEB-INF/views/template/footer.jsp" />
    </definition>

</tiles-definitions>

Upvotes: 0

Related Questions