J3 Limited
 
Google
WWW J3Ltd
 
First Struts Project

Introduction

In the previous section, the software required to develop a Struts web application was configured. In this section a very basic web project is created to ensure that everything is present and correct.

Create A Web Project

A new Dynamic Web Project is created, called StrutsProject:

new web project

new web project 2

new web project 3

The struts project properties is opened up.

The user library, created earlier, is added to the StrutsProject, and also setup to be part of the deployment:

image

The Struts.1.3 library is earmarked as being exported to dependent projects:

export

The Struts1.3 library is added as a Web Library dependency:

sproperties jee dependencies

A Struts project is typically composed of:

Mandatory files

  • web.xml, the JEE way of defining a web application.
  • struts-config.xml, the file that contains the relevant entries for the struts web application.
  • jsp pages to generate the web pages (view).
  • java classes to provide the functionality (model and controller classes).

Optional files

  • jar library files, for example data access layer components.
  • property files, for the text that is displayed.
  • type library descriptor files (Tld's), these files hold the syntax for custom jsp tag libraries.

Initial Files

A starter web.xml is shown below. This file was originally generated by Eclipse in the WebContent/WEB-INF folder. It is configured to load the Struts servlet:

<?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>StrutsProject</display-name>
<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>
<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>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

The struts action servlet is where all the page requests should go. It is told where to look for the struts-config.xml file, and the debug is set to level 3.

In the mapping, we inform the web container that any page request of the form *.do should be sent to the struts action servlet.

Finally, the default file is called index.jsp.

A starter struts-config.xml is created in the WebContent/WEB-INF folder:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings />
</struts-config>

A new JSP is created, called index.jsp in the WebContent folder:

create index.jsp

The contents of the file are as follows:

<html>

 <body>
 This is index.jsp
 </body>

</html>

First Test Run

In the project tree, the project is right clicked, and debug on server is selected. A dialog pops up:

first debug

The checkbox ("Always use this server when running this project") is clicked and finish is clicked.

The project runs and Eclipse navigates to the welcome page index.jsp:

run project

Running the project at this point is a good idea: it confirms that everything is configured correctly to run a Struts web application.

In the next section, a dynamic web page is developed using Struts.

 

 

  Copyright © 2006 J3 Ltd Permission is granted to reproduce material on this page, on the condition that a reference to "WWW.J3Ltd.com" is given as the source of the material.