Using Apache Struts
Needed file structure
To start a Struts project, you will need the following file structure:
*.jsp
WEB-INF/web.xml
WEB-INF/struts-config.xml
WEB-INF/classes/App.properties
WEB-INF/classes/App_en.properties
...
WEB-INF/lib/struts.jar
WEB-INF/lib/jdbc2_0-stdext.jar
WEB-INF/tlds/struts.tld
WEB-INF/tlds/struts-bean.tld
WEB-INF/tlds/struts-logic.tld
WEB-INF/tlds/struts-form.tld
WEB-INF/tlds/struts-html.tld
WEB-INF/tlds/struts-template.tld
This structure contains some variable parts, e.g. the application resource files
App.properties and the configuration file for Struts, struts-config.xml.
The location of these files together with some basic Structs configuration is given in
the web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>App</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Struts Tag Library Descriptor -->
<taglib>
<taglib-uri>/WEB-INF/tlds/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tlds/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tlds/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tlds/struts-template.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/struts-template.tld</taglib-location>
</taglib>
</web-app>
|
Using the Struts Templating System
Even if we do not plan to use Struts as MVC framework, we can use some parts of it, e.g. the template
tag library, to get a unified design for all our pages.
Suppose we have written a template called template.jsp with a header line, a left-side navigation
bar and a lower-right body, using the following content:
<%@ page language="java" %>
<%@ taglib prefix="t" uri="/WEB-INF/tlds/struts-template.tld" %>
<html>
<body bgcolor="#c0c0e0">
<table width="100%" border="0" cellspacing="0" cellpadding="5" valign="top">
<tr>
<td colspan="3" bgcolor="#e0e0f0" height="50px">
<t:get name="Theader"/>
</td>
</tr>
<tr>
<td bgcolor="#e0e0f0">
<t:get name="Tnav"/>
</td>
<td height="300px" bgcolor="#e0e0f0">
</td>
<td width="100%" bgcolor="#ffffff">
<t:get name="Tbody"/>
</td>
</tr>
</table>
</body>
</html>
|
Then we may use this template by calling a page called main.jsp as follows:
<%@ page language="java" %>
<%@ taglib prefix="t" uri="/WEB-INF/tlds/struts-template.tld" %>
<t:insert template="/template.jsp">
<t:put name="Theader" direct="true" content="Hauptseite"/>
<t:put name="Tnav" content="/nav.jsp"/>
<t:put name="Tbody">
Dies ist die Startseite für das Template-Testsystem.
</t:put>
</t:insert>
|
Note that we can fill in the desired content either as literal strings, as is the case with the title
or the main body content, or either as a JSP reference, as is the case with the navigation bar, for which
we use another JSP, containing only the left-side menu bar:
<%@ page language="java" %>
<table width="150px" border="0">
<tr>
<td>
»
</td>
<td width="100%">
Item 1
</td>
</tr>
<tr>
<td>
»
</td>
<td>
Item 2
</td>
</tr>
</table>
|
For this to work, we just need the template tag library, including it in the JSPs as shown above, and in the
deployment descriptor, shown at the very top.
back to toolbox page
|