Thursday, January 14, 2010

Use Tiles in your Struts Application

Application Using Tiles:

For Eclise:
1) Create New Web-Project
2)Add Struts Capabilities.
3)Create "layout.jsp"
Use this Syntax:
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

tiles:insert attribute="header"/>
How you want to show your page.Like header,footer,main,left,body etc.Specify it here.

4)Make following Entry in "tiles-defs.xml"

tiles-definitions>
-- Base Tiles Definition
This is the main definition.You extend it in other definition.
It means if you can use same jsp or override it.
-->
definition name="base.definition" path="/Layout.jsp">
put name="header" value="/WEB-INF/JSP/header.jsp" />
put name="left" value="/WEB-INF/JSP/LeftLink.jsp"/>
put name="left" value="/WEB-INF/JSP/welcome.jsp"/>
put name="footer" value="/WEB-INF/JSP/footer.jsp" />


-- Tiles Definition of welcome page -->
definition name="page.welcome" extends="base.definition">
put name="title" value="Welcome page" />
put name="body" value="/WEB-INF/JSP/welcome.jsp" />




5)Edit Struts-Config.XML

Required part:
plug-in className="org.apache.struts.tiles.TilesPlugin">
set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/>
set-property property="moduleAware" value="true" />
set-property property="definitions-parser-validate" value="true" />


And define your actions as you want.
path="/welcome.do"
redirect="true" />

action-mappings>
action path="/welcome"
type="home.WelcomeAction">
forward name="showWelcome" path="page.welcome" />


action path="/leftlink"
type="home.LinkAction">
forward name="showLink" path="page.left" />



6)Create jsp according to your view and URL which you are giving in tiles-defs package

7)Create your actions.

8)Run your application.

I think it is useful.If any required step is still remaining,plz correct it.

Thanks
Deepak

1 comment:

  1. Replace one left with body in tiles-defs.xml
    put name="body" value="/WEB-INF/JSP)/welcome.jsp"

    ReplyDelete