Newsletter |
Working With Struts 2 Tiles, Struts 2 Tiles Example
Struts » on Oct 29, 2011 { 13 Comments } By Sivateja
let us see how to work with tiles frame work in struts 2, tiles is the real time concept every body must know. Actually tiles applications is little different than other applications we worked up to now, let us see what are the changes need to do before going to the application.
- We must create tiles.xml to configure the pages, and this tiles.xml file must be in web-inf folder only just like web.xml
- In the web.xml file add the following code
<context-param> <param-name> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG </param-name> <param-value>/WEB-INF/tiles.xml</param-value> </context-param> <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener> <filter> ---------- ------------
Following jars file need to be added in lib folder of our web application, in order to work with this tiles framework in struts 2
- commons-beanutils-1.7.0.jar
- commons-collections-3.1.jar
- commons-digester-2.0.jar
- commons-fileupload-1.2.1.jar
- commons-io-1.3.2.jar
- commons-lang-2.3.jar
- commons-logging-1.1.jar
- freemarker-2.3.13.jar
- struts2-convention-plugin-2.1.6.jar
- struts2-core-2.1.6.jar
- struts2-tiles-plugin-2.2.1.jar
- tiles-api-2.0.6.jar
- tiles-core-2.0.6.jar
- tiles-jsp-2.0.6.jar
- xwork-2.1.2.jar
- ognl-2.6.11.jar
Example
Files required….
- index.jsp
- body.jsp
- footer.jsp
- head.jsp
- baseLayout.jsp
- page1.jsp
- page2.jsp
- web.xml
- tiles.xml
- struts.xml
- LogingEx.java
Directory Structure
index.jsp
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=welcomeLink.action">
body.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <h2>This Is welcome Body</h2>
footer.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <center> Copyrights java4s.com </center>
head.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <center> <h4> Your Blog Header </h4> <a href="<s:url action="welcomeLink"/>" >Home</a> <a href="<s:url action="page1Link"/>" >Page1</a> <a href="<s:url action="page2Link"/>" >Page2</a> </center> <br>
baseLayout.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <table border="1" align="center" width="400px;"> <tr> <td height="30" colspan="2"> <tiles:insertAttribute name="myHeader" /> </td> </tr> <tr> <td> <tiles:insertAttribute name="myBody" /> </td> </tr> <tr> <td> <tiles:insertAttribute name="myFooter" /> </td> </tr> </table>
page1.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <h2> This is page 1</h2>
page2.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <h2> This is page 2</h2>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG </param-name> <param-value>/WEB-INF/tiles.xml</param-value> </context-param> <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
tiles.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> <tiles-definitions> <definition name="welcome" template="/baseLayout.jsp"> <put-attribute name="myHeader" value="/head.jsp"/> <put-attribute name="myBody" value="/body.jsp"/> <put-attribute name="myFooter" value="/footer.jsp"/> </definition> <definition name="page1" extends="welcome"> <put-attribute name="myBody" value="/page1.jsp"/> </definition> <definition name="page2" extends="welcome"> <put-attribute name="myBody" value="/page2.jsp"/> </definition> </tiles-definitions>
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <result-types> <result-type name="tiles" /> </result-types> <action name="*Link" method="{1}" class="java4s.LogingEx"> <result name="welcome" type="tiles">welcome</result> <result name="page1" type="tiles">page1</result> <result name="page2" type="tiles">page2</result> </action> </package> </struts>
LogingEx.java
package java4s; import com.opensymphony.xwork2.ActionSupport; public class LogingEx extends ActionSupport { private static final long serialVersionUID = -2613425890762568273L; public String welcome() { return "welcome"; } public String page1() { return "page1"; } public String page2() { return "page2"; } }
Outputs
You Might Also Like
::. About the Author .:: | ||
Comments
13 Responses to “Working With Struts 2 Tiles, Struts 2 Tiles Example”
its throwing an error :
type Status report
message There is no Action mapped for namespace / and action name welcomeLink.
description The requested resource (There is no Action mapped for namespace / and action name welcomeLink.) is not available.
@ Twinkle
Yeah you almost reached 🙂
Even i faced thisproblem initially, do one thing, try to change welcomeLink to some thing else.
.
so you need to do the changes in
LogingEx.java, struts.xml, tiles.xml too hope you know this.
Hey do it carefully and slowly if you are fresher i will help you.
Hi!
I have a question.
about this example, if only i need refresh page1 without refresh others, how can I do this?.
thanks
@ Eduardo
Like gmail ? 🙂
Then you need to use Json,Ajax Calls.
anyone tell me how to use persistence in struts tiles … give a demo also plzzzzzzzzzz
Hi!
The above project ended up with the following error when I run on the tomcat server. Could you please help me ………..
HTTP Status 404 – /MySpringDemo/index.jsp
type Status report
message /MySpringDemo/index.jsp
description The requested resource (/MySpringDemo/index.jsp) is not available.Apache Tomcat/7.0.29
wow wonderfull and thnx
Attribute “class” is required and must be specified for element type “result-type”.
like giving error in
can we prevent the header and footer jsp to refresh again and again for each action,instead refresh only body jsp for its corresponding action and keep header and footer static.
thanks in advance
Hi please send me struts2+tiles+hibernate3 complete example
still getting problem of the namaspace not mapped,unknown location of welcome link
Hi,
Please send me the struts 2 tiles using jsp example(contain dropdown,table,textfields)
Thanks. worked great.