Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 815
tusar Avatar asked Dec 31 '25 19:12

tusar


2 Answers

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
like image 81
GingerHead Avatar answered Jan 03 '26 09:01

GingerHead


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.

like image 36
BIdesi Avatar answered Jan 03 '26 09:01

BIdesi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!