STRUTS TILES TUTORIAL WITH EXAMPLE CODE

/
0 Comments
The Tiles framework provides a templating mechanism that allows you to separate the responsibilities of layout from those of content. As with the templates described earlier in this chapter, you have the ability to establish a layout and dynamically insert the contents of your pages into that layout at runtime. This is a powerful mechanism if you need to customize your site based on such things as internationalization, user preferences, or just the look-and-feel changes that occur in every web application sooner or later. The Tiles framework provides the following features:
· Template capabilities
· Dynamic page construction and loading
· Screen definitions
· Support for tile and layout reuse
· Support for internationalization
· Support for multiple channels


STEPS TO CREATE YOUR FIRST TILES EXAMPLE
step1: create a new project in ecclipse frame work call
the project name as "tiles2"
step2:add struts capabilities into your new project "tiles2" by
right clicking on it .
step3:open the web-inf folder of your project and double clik
on struts-config.xml file
step4: view the source code of your struts-config.xml and
add the tiles-plugin codes below the application resources.properties
in the struts-config.xml (plu in code is given below)

<plug-in className="org.apache.struts.tiles.TilesPlugin">

<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />

</plug-in>
step5:create new xml called "tiles-defs.xml" in webroot folder
step 6: open tiles-defs.xml and define the tiles definition.the code
in tiles-defs.xml is given below


<?xml version="1.0" encoding="UTF-8"?>lt;!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN" "tiles-config_1_1.dtd">

<tiles-definitions>

<definition name="hello" path="/sitelayout.jsp">

<put name="header" value="/WEB-INF/header.jsp"></put>

<put name="navigation" value="/WEB-INF/navi.jsp"></put>

<put name="footer" value="/WEB-INF/footer.jsp"></put>

</definition>

<definition name="second" extends="hello>

<put name="body" value="/login.jsp"></put>

</definition>

</tiles-definitions>
step7: create jsp pages for our application
create a jsp called header.jsp in web-inf
create a jsp called navigation.jsp in web-inf
create a jsp called footer.jsp in web-inf
step8:create a jsp for defining our layout.named sitelayout.jsp, login.jsp(which is
used as body page) in webroot


the code is given below sitelayout.jsp







step9:create a jsp called index.jsp in webroot




write the logic tag inside the body tag of index.jsp





<logic:forward name="me.do"/>
step10:create a new action called "login" and give forwards to it
forwardname="welcome" and path="second"
open loginAction it contains the below code:



package com.yourcompany.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.form.LoginForm;
/**
* MyEclipse Struts
* Creation date: 05-21-2008
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/form/login.jsp" scope="request" validate="true"
* @struts.action-forward name="welcome" path="hello"
*/
public class LoginAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
return mapping.findForward("welcome");
}
}

step10:create new global forward give its name as "me.do" and path"/login.do"



the worksheet is look like this(design mode of struts-config.xml)
step11: now your project is ready to run. deploy your project into your tomcat5.5 server


step12:start your server and open the browser(internet explorer) type the url in address


bar http://localhost:8080/tiles2/


then you can see the output of your tiles project.it is look like below



Enter your email address:

Delivered by FeedBurner


Subscribe to JOHNMUKKAD by Email


You may also like

No comments: