Struts

Solo disponible en BuenasTareas
  • Páginas : 6 (1471 palabras )
  • Descarga(s) : 0
  • Publicado : 20 de enero de 2011
Leer documento completo
Vista previa del texto
Introduction to struts and tiles with a working example
Step by Step example using struts tiles. Example application shows how to list, create, edit and save data. Source code is provide. As development environment we used eclipse with the plugin myeclipse.

Generals
Author: Sascha Wolski http://www.laliluna.de/tutorial.html – Tutorials for Struts, EJB, xdoclet and eclipse. Date: November,8st 2004 Software: MyEclipse 3.8 Jboss 3.2.5 Source code: Source code for this tutorial can be downloaded here. The PDF version is located here.

Introduction
This Tutorial explain how to use struts tiles with a simple example. What is struts Consider a example library application whose web page layout has a header, body and footer. On this example i will explain what is tiles and when you willbe use it.

The developer have two alternativ ways to create the layout. He add the header and footer to each page of the site or he use the command , to include the header and footer into the pages. In the first way all pages contains the header and footer source code. When the header or footer will changed, the developer have to change all pages. In the second way the header and footer areplaced in seperated files, so the files can be included into the pages where they will be needed. When he change the layout and add a menu to the library application, shown by the picture below, the developer have to change all pages, because he have to add the inlude command of the menu in each page. On this situation tiles is the best way to develope the page layout, thus the developer don`t have tochange each page.

Tiles use a seperate layout file, that contains the container of the layout. When the layout will be changed only the layout file and the tiles configuration files have to change by the developer. That will save many time on large applications.

Your first tiles application
Create a new struts project with File > New > Project or use the shortcut Strg + n. Select theWizard in J2EE Web Project.

Create a nice name for your project

After creating the project, your Package Explorer looks like the picture below.

For now your project is a normal J2EE project, so we need to add the struts capabilityies. Right click on the project and add the capabilityies for struts with Add Struts Capabilityies.

Change the properties Base package for new classes Defaultapplication resource

and

Create a default page
Ok, now we want to create a default page. Right click (yes again) on the Folder WebRoot in the Project and choose New > JSP.

Set the name to index.jsp and choose on template to use > Standard JSP using Struts 1.1 MyEcplise will use the template to create the JSP File.

You will find the file index.jsp in the folder WebRoot of the project.On the top of the file you will find the struts tag libraries. These includes will be use to access the tags of struts. In your case we only need the logic tag library.

Insert the following line below the included logic tag.

This line will arranges struts to find a forward with the name welcome. If the application don´t find this forward it will leads an error.

Add the tilesfunctionality
Open the file struts-config.xml in the folder WEB-INF and replace the following line

with

Below the line

add



Struts will load the tiles - controller and the tiles - plugin

Create the layout file
Create a new jsp file in WebRoot/jsp/ and set the name to siteLayout.jsp Add the following source code.


The black marked tags are the placeholder fortitle, header, navigation, body and footer. The first tag in the listing above is . This tag retrieves the title as string and insert it there. The following tags will be used to insert different jsp files, strings and action mappings into the siteLayout.jsp.

Create the tiles definitions
Create the file tiles-defs.xml in the folder WEB-INF, if it not exists.

Open the file and add the...
tracking img