tusar
tusar

Reputation: 3424

which one of these is the better approach of using tiles

Problem: I have two identical pages home.jsp and contact.jsp with same structure. They differs only in body content and title. I want to create a Layout page using tiles framework and reuse the code for the two JSPs. The controller framework is yet not decided, it may be Spring MVC 3 or Struts 2.

Solution A: Calling JSP files/views directly from controller/action classes.

I write a single definition in tiles.xml like:

<definition name="baseLayout" template="/WEB-INF/jsp/layout/baseLayout.jsp">
    <put-attribute name="title" value="" />
    <put-attribute name="header" value="/WEB-INF/jsp/includes/header.jsp"/>
    <put-attribute name="body" value="" />
</definition>

Now in baseLayout.jsp:

<html>
    <head><title><tiles:insertAttribute name="title"/></title></head>
    <body>
        <div class="wrapper">
            <div class="header"><tiles:insertAttribute name="header"/></div>
            <div class="body"><tiles:insertAttribute name="body"/></div>
        </div>
    </body>
</html>

Now in home.jsp

<tiles:insertDefinition name="baseLayout">
    <tiles:putAttribute name="title">
        Title for home page
    </tiles:putAttribute>
    <tiles:putAttribute name="body">
        Content for home page
    </tiles:putAttribute>
</tiles:insertDefinition>

Similarly for contact.jsp :

<tiles:insertDefinition name="baseLayout">
    <tiles:putAttribute name="title">
        Title for contact page
    </tiles:putAttribute>
    <tiles:putAttribute name="body">
        Content for contact page
    </tiles:putAttribute>
</tiles:insertDefinition>

Solution B: Calling the tiles definition of different JSP files from cotrollers/action classes. This approach requires one tiles definition for each of the JSP file I would be writing. So altogether 3 tiles definitions (one is for baseLayout and other two are for home and contact).

tiles.xml :

<definition name="baseLayout" template="/WEB-INF/jsp/layout/baseLayout.jsp">
    <put-attribute name="title" value="" />
    <put-attribute name="header" value="/WEB-INF/jsp/includes/header.jsp"/>
    <put-attribute name="body" value="" />
</definition>

<definition name="home" extends="baseLayout">
    <put-attribute name="title" value="Title for home page" />
    <put-attribute name="header" value="/WEB-INF/jsp/home.jsp"/>
</definition>
<definition name="contact" extends="baseLayout">
    <put-attribute name="title" value="Title for contact page" />
    <put-attribute name="header" value="/WEB-INF/jsp/contact.jsp"/>
</definition>

baseLayout.jsp : Same as **Solution A**

home.jsp : Content for home page

contact.jsp : Content for contact page


I want advice on which one of the above approaches I should stick to.

Upvotes: 3

Views: 3370

Answers (3)

Dimitrios Menounos
Dimitrios Menounos

Reputation: 555

Solution A helps maintenance in large applications by distributing configuration and also promoting name convention over explicit configuration. Moreover it is conceptually compatible with other templating solutions such as JSF or plain tags. For example see this: JSP tricks to make templating easier?

Think an application with hundreds of controllers and views written by different people over a large period of time. With solution B you will have to face a huge tiles.xml file. Add a slight lack of discipline and view names end up an inconsistent mess and content present inside the configuration (like your example).

Upvotes: 0

GingerHead
GingerHead

Reputation: 8230

The second solution is the best way:

  • You can have independent section in your tiles properties for each jsp layout.
  • You can change anytime without effecting other layouts later on
  • The most traditional way using in struts

First solution:

  • May cause problems later on with having to edit them with any change specially when you are deep in the project in advanced levels
  • More generic approach not suitable for struts/tiles architectural designs

Upvotes: 3

BIdesi
BIdesi

Reputation: 371

Solution B is the best approach to implement,

  • you will have individual style template what you can reuse later if required for other jsp's without messing the code in jsp like Solution A.

  • As of understanding the second approach is more clear and a common standard to follow.

Upvotes: 1

Related Questions