Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Struts2 Tiles integration


May 15, 2021 Struts2



In this chapter, we'll learn the steps involved in integrating the Tiles framework with Struts2. Apache Tiles is a template framework for simplifying the development of the web application user interface.
First, we need to download the tilles jar file from the Apache Tiles website. You need to add the following jar files to the class path of the project.

  • tiles-api-x.y.z.jar

  • tiles-compat-x.y.z.jar

  • tiles-core-x.y.z.jar

  • tiles-jsp-x.y.z.jar

  • tiles-servlet-x.y.z.jar

In addition to the above, we must copy the following jar file from WEB-INF/lib.

  • commons-beanutils-x.y.z.jar

  • commons-digester-x.y.z.jar

  • struts2-tiles-plugin-x.y.z.jar

Now, let's set up the Struts-Tiles integrated web .xml, as shown below. H ere are two important points to note. F irst, we need to tell tille where to find the tiles profile tiles .xml. I n our case, it will be under the /WEB-INF folder. T hen we need to initialize the Struts2 download of the included Tilles listener.

<?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">
   <display-name>Struts2Example15</display-name>
	
   <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.ng.filter.StrutsPrepareAndExecuteFilter
   </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>

Next, create a tiles folder under the /WEB-INF .xml as follows:

<?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="baseLayout" template="/baseLayout.jsp">
      <put-attribute name="title"  value="Template"/>
      <put-attribute name="banner" value="/banner.jsp"/>
      <put-attribute name="menu"   value="/menu.jsp"/>
      <put-attribute name="body"   value="/body.jsp"/>
      <put-attribute name="footer"   value="/footer.jsp"/>
   </definition>

   <definition name="tiger" extends="baseLayout">
      <put-attribute name="title"  value="Tiger"/>
      <put-attribute name="body"   value="/tiger.jsp"/>      
   </definition>

   <definition name="lion" extends="baseLayout">
      <put-attribute name="title"  value="Lion"/>
      <put-attribute name="body"   value="/lion.jsp"/>      
   </definition>
  
</tiles-definitions>

Next, we define .jsp skeleton layout in baseLayout. I t has five re-usable/coverable areas. N amely title, banner, menu, body, and footer. W e provide the default value for baseLayout, and then create two customizations extended from the default layout. T he tiger layout is similar to the basic layout, except that it .jsp tiger as its version and the text "Tiger" as the title. Similarly, the lion layout is similar to the basic layout, except that it uses lion .jsp as its body and text "Lion" as title.
Let's look at the various jsp files. Here's what the baseLayout .jsp file:

<%@ 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">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" />
</title>
</head>

<body>
   <tiles:insertAttribute name="banner" /><br/>
   <hr/>
   <tiles:insertAttribute name="menu" /><br/>
   <hr/>
   <tiles:insertAttribute name="body" /><br/>
   <hr/>
   <tiles:insertAttribute name="footer" /><br/>
</body>
</html>

Here we just put together a basic HTML page with tiles properties. I nsert the tyle property where it needs to be used. N ext, let's create a banner file .jsp following:

<img src="//atts.w3cschool.cn/attachments/tuploads/struts_2/tp-logo.gif"/>

The .jsp will have the following, which are links to TigerMenu.action and LionMenu.action.

<%@taglib uri="/struts-tags" prefix="s"%>

<a href="<s:url action="tigerMenu"/>" Tiger</a><br>
<a href="<s:url action="lionMenu"/>" Lion</a><br>

Lion.jsp file will have the following:

<img src="//atts.w3cschool.cn/attachments/tuploads/struts_2/Lion.jpg"/>
The lion

Tiger.jsp file will have the following:

<img src="//atts.w3cschool.cn/attachments/tuploads/struts_2/tiger.jpg"/>
The tiger

Next, create an action class file, MenuAction .java, which contains the following:

package cn.w3cschool.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class MenuAction extends ActionSupport {
   public String tiger() { return "tiger"; }
   public String lion() { return "lion"; }
	
}

This is a very direct class. W e declared two methods, tiger() and lion(), which returned tiger and lion as results, respectively. L et's put them in the struts .xml file:

<!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" 
         class="org.apache.struts2.views.tiles.TilesResult" />
      </result-types>

      <action name="*Menu" method="{1}" 
         class="cn.w3cschool.struts2.MenuAction">
         <result name="tiger" type="tiles">tiger</result>
         <result name="lion" type="tiles">lion</result>
      </action>

   </package>
</struts>

Let's review what we did in the document above. F irst, we declared a new type of result called "tiles" because we now use tile instead of normal jsp as a view technique. Struts2 supports the Tiles View result type, so we created the result type "tiles" as the result type of the "org.apache.struts2.view.tiles.TilesResult" class.
Next, we'll say that if the request is /tigerMenu.action, the user jumps to the tiger title page, and if the request is /lionMenu.action, the user jumps to the lion title page.
We use some regular expressions to do this. I n the action definition, we say that anything that matches the "Menu" pattern will be handled by this action. T he matching method is called in the MenuAction class. T hat is, tigerMenu.action will call tiger() and lionMenu.action will call lion(). Then we need to map the results to the appropriate title page.

Now, right-click on the project name, and then click "Export" and "WAR File" to create a WAR file. T he WAR file is then deployed in Tomcat's webapps directory. Finally, when you start the Tomcat server and try to access the URL http://localhost:8080/HelloWorldStruts2/tigerMenu.jsp, the following interface is displayed:

Struts2 Tiles integration

Similarly, if you go to the lionMenu.action page, you'll see that the lion page uses the same title layout.