Using Tiles in Struts- A Proof of Concept

This example builds on the StrutsEntityEJB example and some of the components have been reused. refer post – “EJB with STRUTS”
The following files are unchanged:
application.xml, ApplicationResources.properties, ejb-jar.xml, jboss.xml, jbosscmp-jdbc.xml, mysql-ds.xml, build.xml, build.properties remain unchanged. There is no change in authorsAction or authorsForm classes.
The entity bean classes remain unchanged and are reused.

The structure of work folder is as follows:

mysql-ds.xml
jbosscmp-jdbc.xml
ejb-jar.xml
jboss.xml
application.xml
ApplicationResources.properties
build.xml
build.properties
build.bat
struts-config.xml
web.xml
<build>
<pages>
      |layout.jsp
      |authorsAddTiles.jsp
      |authorsAdd.jsp
      |authorsSearchTiles.jsp
      |authorsSearch.jsp
      |sucessTiles.jsp
      |success.jsp
      |header.jsp
      |menu.jsp
      |copyright.jsp
<src>
      |<shoppingcart>
           |AuthorsBean.java
           |Authors.java
           |AuthorsHome.java
           |AuthorsKey.java
           |authorsForm.java
           |authorsAction.java

web.xml is modified to include struts-tiles.tld
web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.matty</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>authors.jsp</welcome-file>
	</welcome-file-list>
	<taglib>
		<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
		<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
		<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
		<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri> /web-inf/struts-tiles.tld”</taglib-uri>
		<taglib-location> WEB-INF/struts- tiles.tld</taglib-location>
	</taglib>
</web-app>

struts-config.xml has been modfied to call the appropriate jsp in the action mapping.

struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
	<form-beans type="org.apache.struts.action.ActionFormBean">
		<form-bean name="authorsForm" type="shoppingcart.authorsForm" />
	</form-beans>
	<action-mappings type="org.apache.struts.action.ActionMapping">
		<action path="/testout" type="shoppingcart.authorsAction" name="authorsForm"
			scope="request" validate="false">
			<b>
				<forward name="successful" path="/successTiles.jsp" />
			</b>
		</action>
	</action-mappings>
	<message-resources parameter="ApplicationResources" />
</struts-config>

None of the jsps have been reused.
The following jsps have replaced authors.jsp and success.jsp

layout.jsp


<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<html:html>
<head>
<title>Tiles Struts EJB Application</title>
<html:base />
</head>
<body topmargin="0" leftmargin="0" bgcolor="#FFFFFF">
	<table width="100%" border="0">
		<tr>
			<td colspan=2><!—-Header page information--> <tiles:insert
					attribute="header" /></td>
		</tr>
		<tr>
			<td width="20%"><tiles:insert attribute="navibar" /></td>
			<td width="80%"><!—-Main body information--> <tiles:insert
					attribute="body-content" /></td>
		</tr>
		<tr>
			<td colspan=2><!—-copyright information --> <tiles:insert
					attribute="copyright" /></td>
		</tr>
</body>
</html:html>

header.jsp

<center><h1>Search or Find an Entity </h1></center>

menu.jsp


<a href = "/authorsAddTiles.jsp">Add</a><br/>
<a href = "/authorsSearchTiles.jsp">Search</a>

copyright.jsp

<center><h4>(c)This is copyright stuff that you can use</h4></center>

authorsAddTiles.jsp

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<tiles:insert page="layout.jsp" flush="true">
	<tiles:put name="header" value="header.jsp" />
	<tiles:put name="navibar" value="menu.jsp" />
	<tiles:put name="body-content" value="authorsAdd.jsp" />
	<tiles:put name="copyright" value="copyright.jsp" />
</tiles:insert>

authorsAdd.jsp

<html>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<head>
</head>
<body>
	<html:form action="/testout.matty">
		<p>
			<html:errors />
			<br />
			<bean:message key="isbnCode.label" />
			<html:text property="isbnCode" size='10' />
			<br />
			<bean:message key="author.label" />
			<html:text property="author" size='10' />
			</br>
			<html:submit value='Add' property="add" />
	</html:form>
</body>
</html>

authorsSearchTiles.jsp

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<tiles:insert page="layout.jsp" flush="true">
	<tiles:put name="header" value="header.jsp" />
	<tiles:put name="navibar" value="menu.jsp" />
	<tiles:put name="body-content" value="authorsSearch.jsp" />
	<tiles:put name="copyright" value="copyright.jsp" />
</tiles:insert>

authorsSearch.jsp

<html>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<head>
</head>
<body>
	<html:form action="/testout.matty">
		<p>
			<html:errors />
			<br />
			<bean:message key="isbnCode.label" />
			<html:text property="isbnCode" size='10' />
			<br /> <br />
			<html:submit value='Find' property="find" />
	</html:form>
</body>
</html>

successTiles.jsp


<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<tiles:insert page="layout.jsp" flush="true">
	<tiles:put name="header" value="header.jsp" />
	<tiles:put name="navibar" value="menu.jsp" />
	<tiles:put name="body-content" value="success.jsp" />
	<tiles:put name="copyright" value="copyright.jsp" />
</tiles:insert>

success.jsp

<html>
<head>
</head>
<body>
	<%
		if (request.getAttribute("author") != null) {
	%>
	<h3>
		ISBN CODE
		<%=request.getAttribute("isbnCode")%></h3>
	<%
		}
	%>
	<%
		if (request.getAttribute("isbnCode") != null) {
	%>
	<h3>
		AUTHOR
		<%=request.getAttribute("author")%></h3>
	<%
		}
	%>
</body>
</html>

The structure of the ear file is as follows:

authors.ear
    |<meta-INF>
         |application.xml
    |authors.jar
         |<meta-INF>
             |ejb-jar.xml
             |jboss.xml
             |jbosscmp-jdbc.xml
         |<shoppingcart>
             |AuthorsBean.class
             |Authors.class
             |AuthorsHome.class
             |AuthorsKey.class
    |authors.war
         |add.jpg
         |find.jpg
         |layout.jsp
         |authorsAddTiles.jsp
         |authorsAdd.jsp
         |authorsSearchTiles.jsp
         |authorsSearch.jsp
         |sucessTiles.jsp
         |success.jsp
         |header.jsp
         |menu.jsp
         |copyright.jsp
         |<meta-INF>
         |<web-INF>
             |struts-config.xml
             |web.xml
             |struts-bean.tld
             |struts-html.tld
             |struts-logic.tld
             |struts-nested.tld
             |struts-logic.tld
             |struts-tiles.tld
             |<classes>
                 |ApplicationResources.properties
                 |<shoppingcart>
                          |Authors.class
                          |AuthorsKey.class
                          |AuthorsHome.class
                          |authorsForm.class
                          |authorsAction.class
             |<lib>
                 |jnp-client.java
                 |jboss-client.jar
                 |struts.jar

You can start the application by either typing
http://localhost:8080/authorsSearchTiles.jsp
Or
http://localhost:8080/authorsAddTiles.jsp
You can toggle between the search and add pages while the header, navigation bar and footer remain unchanged.

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: