Jakarta Struts A beginner’s tutorial

Isabelle HURBAIN 21st October 2002

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

Copyright (c) 2002 Isabelle HURBAIN Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation. with no Invariant Section, with no Front-Cover Text, and with no Back-Cover Text. A copy of the license is included in the appendix entitled ”GNU Free Documentation License”.

1

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

Contents
1 Introduction 1.1 A brief overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 The MVC design pattern and its application to Struts . . . . . . . . . . . . . . . . . . . . . . . . 1.3 Who should read this tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Installation 2.1 JDK . . . . . . . . . . . . 2.2 Win32 installation . . . . 2.2.1 Linux installation . 2.3 Tomcat . . . . . . . . . . . 2.3.1 Win32 Installation 2.3.2 Linux Installation . 2.3.3 Configuration . . . 2.4 Eclipse . . . . . . . . . . . 2.5 Struts . . . . . . . . . . . 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 8 8 9 10 10 11 13 14 15 15 16 19 23 24

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

3 Learning by example: your first Struts application 3.1 Presentation . . . . . . . . . . . . . . . . . . . . 3.2 Creating the application workspace . . . . . . . 3.3 The first page . . . . . . . . . . . . . . . . . . . 3.3.1 First draft . . . . . . . . . . . . . . . . . 3.3.2 A bit of internationalization . . . . . . . 3.4 Enter the application and create the data source 3.4.1 The JSP form . . . . . . . . . . . . . . . 3.4.2 The struts-config.xml file . . . . . . . . . 3.4.3 Create the ActionForm and Action . . . 3.4.4 Refining the login process . . . . . . . . 3.4.5 Creating a data source . . . . . . . . . . 3.5 The main menu . . . . . . . . . . . . . . . . . . 3.6 Adding a user . . . . . . . . . . . . . . . . . . . 3.7 Get information about a user . . . . . . . . . . 4 Conclusion A GNU Free Documentation License

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

2

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

Chapter 1

Introduction
Struts is an open source framework useful in building web applications with Java Servlet and JavaServer Pages (JSP) technology. It encourages software development following the MVC design pattern.

1.1

A brief overview

Many web applications are JSP-only or Servlets-only. With JSP Java code is embedded in the HTML code; with , Servlets the Java code calls println methods to generate the HTML code. Both approaches have their advantages and drawbacks; Struts gathers their strengths to get the best of their association.

1.2

The MVC design pattern and its application to Struts

The MVC design pattern divides applications into three components: the model maintains the state and data that the application represents
 

the view allows the display of information about the model to the user
 

the controller allows the user to manipulate the application
 

In Struts, the view is handled by JSPs and presentation components, the model is represented by Java Beans and the controller uses Servlets to perform its action.

1.3

Who should read this tutorial

This tutorial is for people who want to learn Struts from scratch - that is, from server installation to operational knowledge. It will explain how to setup a whole Struts application using Tomcat 4.0.4, Struts 1.0.2 and Eclipse 2.0. I do not guarantee that what I explain in this tutorial is the best way or even a correct way to do things. Particularly, I’m not an Eclipse guru and my way to do things can seem weird. It is at least my way works;-)

3

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

Chapter 2

Installation
Before we begin an application, we must install all the needed stuff and configure it so that it is useable.

2.1

JDK

In order to do any Java development you need the Java Developer Kit. You can find a JDK1.4 at http://java.sun.com.

2.2

Win32 installation

Just execute the installation executable and follow the instructions. You will also need to setup the PATH and the JAVA HOME variables; to do this, you can write a little batch file called for example javasetup.bat and put it into a directory in your execution path. Mine looks like this:
0 6  62 32 0 ) ( ¡ ' & $" !      ¨  ¨ ¦ ¤ ¢ 8¥75411£©%#¥©§¥£¡

2.2.1 Linux installation
Just execute the installation .bin and follow the instructions. (You may need to su root). You will also need to setup the PATH and JAVA HOME variables; to do this, you can write a little shell script or put it into your .profile. My shell script looks like this (in Bash):
0 6  62 32 0 ) ( ¡ ' & U W X W & U T ¡ V U      ¨  ¨ ¦ ¤ T S R Q `¥4Y411£©£©H¥E¥§E¥¥¢

2.3

Tomcat is a JSP/Servlet container - a web server that will allow you to use JSPs and servlets in your application. You will find it as a subproject of the Jakarta project (http://jakarta.apache.org).

I G F U      ¨  ¨ ¦ a"  A ¨ 9 a  A ¨ 9 ¤ T S R Q ©EE¥©%Y¥¥£@E¥¥¢

I G F $ B      ¨  ¨ ¦ BC B  A ¨ 9 B  A ¨ 9 ¤ ¢ PH¥E¥%D¥¥@¥£¡

Tomcat

4

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

2.3.1 Win32 Installation
To install Tomcat on a Win32 platform, you just have to download the binary distribution and to decompress it into a directory of your choice. To run it, check that your JAVA HOME and PATH variables are set and launch the ”startup.bat” script in the bin directory of your Tomcat directory. You might need to add a CATALINA HOME variable to your batch file pointing to your Tomcat directory (I will call this directory this way from now on).

2.3.2 Linux Installation
To install Tomcat on a Linux platform, you can also use the binary distribution and un-tar.gz it into a directory of your choice. You can also compile it from sources - to do that, read the documentation provided with Tomcat. Then check that your JAVA HOME and PATH variables are set and launch the ”startup.sh” script in the bin directory of your Tomcat directory. You might need to add a CATALINA HOME variable to your shell script pointing to your Tomcat directory (I will call this directory this way from now on).

2.3.3 Configuration
You’ll have to add a line to the file tomcat-users.xml in the conf subdirectory of your $CATALINA HOME. This line will allow you to access Tomcat’s application manager. The line looks like this: where and are values you choose for your manager login and password. After this modification, you will have to relaunch Tomcat by calling the shutdown script (.bat or .sh, depending on your system), waiting a bit for the whole Tomcat to close, and relaunch startup. Check that everything is okay by accessing http://localhost:8080/ with your favourite browser - you should see a Tomcat page. If you don’t, please refer to Tomcat’s installation documentation. Also try http://localhost:8080/manager/ to check that your login and password are working. It should display
‘  ‰ ˆ ‡ ‡ ‚ † x DyED5ƒyw U ( I W c c S ™ I h S I ) I — • ” “ §`e¥dE¥˜–©E¨ ’ „ w ‚ € x …Eƒyw v U e T ¢ t W I W cr ( T W ( I W ¤ ¡ e ¡ ¢ q S T e ( T S h ¡ ¡ W R g ce ( T S h ¡ ¡ W ui¥£`s5¥£©`£¥pi£EPE¥Ef`E©R e ¢ c W I g ce ¢ c W I T ¢ ¡ `8d¥Hf`¥8dE@££HV b

IMPORTANT WARNING The configuration as described here is only a development configuration and should be hardened on a production server!

2.4

Eclipse

Eclipse is a Java IDE, you can find it at http://www.eclipse.org. On Linux and Win32, you’ll just have to unzip / untargz the package and execute eclipse(.exe on Win32) to complete the installation (basically the creation of the workspace). Eclipse isn’t a requirement at all for Struts development, it is just the IDE I use. If you feel more comfortable with another one or without one, it should be relatively easy to adapt the tutorial to it.

2.5

Struts

Struts can also be found as a subproject of the Jakarta project (http://jakarta.apache.org). There is no Struts installation for the moment, just uncompress it in a convenient directory. 5

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

Chapter 3

Learning by example: your first Struts application
3.1 Presentation

We will develop a basic web application: it will allows users to register and to see their personal data . This application will have 5 pages: a main menu
 

a registration form
 

a confirmation of registration
 

a request information form
 

a display of the wanted information
 

The data will be held in a Vector within the application’s scope, so that we do not have to bother with a database or falt files (this is just an example and the aim of this tutorial is to concentrate on Struts, not Java).

3.2

Creating the application workspace

Prior to launching Eclipse, we still have a few things to do in order to setup the web application: Create a directory in the webapps directory of your $CATALINA HOME (which we will refer to $APP BASE). Call it registeruser.
           

In your $APP BASE, create a directory WEB-INF In $APP BASE/WEB-INF/, create a directory classes and a directory lib Copy struts.jar from the struts distribution (in the lib directory) to the $APP BASE/WEB-INF/lib/ directory Copy all the struts*.tld files and the struts-config 1 0.dtd file from the struts distribution (in the lib directory) to the $APP BASE/WEB-INF/ directory Copy $CATALINA HOME/common/lib/servlets.jar to the $APP BASE/WEB-INF/lib/ directory 6

v c W T W R • ¤ G I G `e£EH©U b v ¢ V q W X • c W T W R ¥£`d£EU b ¥£`d£ER b ' v ¢ V q W X • c W T W v ¢ c W I • c W T W R 8dE8d£E©U b £8eE`d£ER b t V F ¢ ( v ¢ c W I • c W T W v c W T W R • ¤ G I 8d£E©H©G b v • • 2 ( ¢ t t S q ¡ G I S G ¤ W c T S | I G y ™ V c h S y ¡ q S T ¤ I S ™ y ™ G y h r ¤ ¢ q X T ¢ ¡ ¡ G y ¤ T S | q ¢ X ¢ q q G W ¤ ¢ ( t I G t t V F ¢ ( ¢ y A • n ¥‚y¥@}¥£`©£©{©EEpEp©¥{©E5¥¥¥£u¥H¥u£@¥¥@¥¥r©E£@¥h• #b v c W T W R • ¤ G I G `e£EH©U b v ¢ V q W X • c W T W R E¥¥`dHU b i©€yH£¥E¥¥U ’ PE w ¥£`d£ER b q c Q2 t G | I S ™ • ¡ ¤ V T ¤ ¡ x “ • s U v ¢ V q W X • c W T W v ¢ c W I • c W T W R `dE`e£EU b E©¥8eE`d£ER b t G | I S ™ v ¢ c W I • c W T W v c W T W R • ¤ G I 8d£E©H©G b v • • I S G ¤ W c T S | I G I S G ¤ W T V t G | I S ™ ¡ ¤ V T ¤ j t I G I G W ¤ I S ™ ¢ ™ T V S ¡ ¢ T ”   ¢ y ¤ S ¤ y ¤ W R ¢ X G ¤ W q ¢ T • ¤ Q ¢ ¤ I S ! • n {©¥E£`P£©t¥£¥©E¥@E¥d©H©¥§£¥r¥@¥pu¥£ErE£¥©@• #b v c W T W R • ¤ G I G `e£EH©U b v¢Vq¥WEX£•`cdWET£WU b £¥¥¥E££E££¥£`d£ER b R ¡ ¢ ™ T V S ¡ ¢ ~ I S G ¤ W ™ G q R R ¨ v ¢ V q W X • c W T W v¢8PE8P£EU b ¥E¥E¥8eE`d£ER b c W I • c W T W R I S G ¤ W ™ G q R R W v ¢ c W I • c W T W v c W T W R • ¤ G I 8d£E©H©G b v • • ¡ ¡ W q ™ ¢ ¡ W F ¢ q ( I V F ¡ ¢ ™ T V S ¡ ¢ ~ • n h¥r£E@¥pE¥p• #b v¥¡¡Wq£™•E¤¥¢¥qX¥T¢£¡U b ¤¥¢£qXT¥¢¥jEI©SGE¤™E¨72YI©S¥GE¤£%uH¥%5©%y¥¥¥¥£¥£¡ b ™ W2 ¡ ¤ V T ¤ ¡2 ¢ y ™ W R W2 t T S v ¡ ¡ W q ™ • ¤ ¢ q X T ¢ v ¢ c W I • ¤ ¢ q X T ¢ ¡ 8eH¥¥¥£U b ¥E©8eE¥£¥£¡ b I S G ¤ ™ W v ¢ c W I • ¤ ¢ q X T ¢ v ¤ ¢ q X T ¢ £¥¥¡ b v • • I S G ¤ W T V t G | I S ! ¤ ¢ q X T ¢ j I S G ¤ ™ ¨ • n ¥}©¥E£¥E©–¥¥¥¥¥{¥Eh• 5b v R R W • F ¢ E£h b v e ( ¤ (2 '  '  R R W • F ¢ h U ¡ ( ¤ ( U ¢ ¢ ' & U c S ™2 I V ¡2 W X W & U U" R ¤ ¤ y d145¥E¥¥H¥£¥¥`d¥%Y©fD£¥%Y¥ze e x  U U '2 ' I S G ¤ W ™ G q R R ¨ F w 1©%#d£E£¥E¥r¢ ro A o ¥%DH©vu`PE©Er¥Pt£H¥9 U U2 ™ I “ r ¡ c ¢ ¤ ¡ g ¡ S T ™ G  I V j U U • e ! “ ” s — R R W • F ¢ h  9 q A ! on ¥E£r¥¥£ p5b v g H¥¥¥¥©`©E££H£i`71£`¥¥E¥h8©Q b e 0 • m l k k •  j “ e t I G ( S ™ I ¢ e 62 0 e I S G ¡ T ¢ X q c g

f

Now it’s time to open Eclipse. When you first open Eclipse, you have 4 windows, called views. I will not speak any further about views and perspectives - each view and what it does are pretty well documented in Eclipse documentation. The main view contains a help file, you can read it or close it:). We want to create a new application. To do this, click on File New Project... . A window opens, in which we select Java in the first frame and Java Project in the second frame. Click on Next. Type a project name (like registeruser) in the Project Name field. Uncheck the box ”Use defaults” for the Project Content, and browse your disk to set the directory to $APP BASE. Click on Next, a message box asks you if you want to create the project now, click on Yes. On the Source tab, set the ”Build output folder” to $APP BASE/WEB-INF/classes. Click on ”Use source folders contained in the project”, and then on ”Add existing folders” and there select $APP BASE/src. On the Librairies tab, click on Add external JARs and add the two JARs of $APP BASE/WEB-INF/lib/. Click on Finish. Open Perspective Resource. Open the Resource perspective with Window We also have to import the classes for Struts and for Java Servlets. Right-click on $APP BASE/src in the navigator and select Import. Select Zip File and Next. Go to $APP BASE/WEB-INF/lib and select struts.jar. Click on Next. Repeat the operation with the file $CATALINA HOME/common/lib/servlet.jar. You also have to create a web.xml file, which should look like this:

f

f

Isabelle HURBAIN

Create a $APP BASE/src directory.

7
v R V ¤ T W ¤ ¡ • I S • ( W S q H¥¥¥E©£¥U b ¥£E¥£q b ' v R V ¤ T W ¤ ¡ • I S • ( W S f  

Jakarta Struts - A beginner’s tutorial

v B e I W ¢ F e Q G | ¢ T R e ( q ¤2 I W ¢ F • ¡ ¤ V T ¤ ¡ i1©z`E¥hi¥4y£E¥©U ’ ©E w d`£E@©£@B b x “ • s U e G T V F G q t W ¤ „ v B e W X W & e ¢ t W V t I W q ¢ t W R „ i8P`£E¥©r£hB b

v R R W • F ¢ h EU b v F G q t W ¤ EP£U b vEI©SGE¤£W¥™Sq¥•E£¥U b £E4uE¥£H¥U ’ © w ¥E££EP£¤ b F G q t W ¤ ( q ¤2 ™ G t S q • ¡ ¤ V T ¤ ¡ x “ • s U v I S G ¤ W ™ S q • F G q t W v G T V • F G q t W ¤ E£E£U b £E4D¥H£¥¥U ’ © w £EEP£¤ b ( q ¤2 ™ G t S q • ¡ ¤ V T ¤ ¡ x “ • s U v G T V • F G q t W v F G q t W ©£¤ b v F G q t W ¤ EP£U b vI£S¥GE¤£W™S¥q•H©E£U b ¥€#`£H¥U ’ © w ¥E££EP£¤ b F G q t W ¤ ( q ¤2 q c ¤ y • ¡ ¤ V T ¤ ¡ x “ • s U v I S G ¤ W ™ S q • F G q t W v G T V • F G q t W ¤ ¥E©H©¥U b ¥4Di©¥¥U ’ © w £EEP£¤ b ( q ¤2 q c ¤ y • ¡ ¤ V T ¤ ¡ x “ • s U v G T V • F G q t W v F G q t W ©£¤ b v F G q t W ¤ EP£U b vI£S¥GE¤£W™S¥q•H©GtE¤£U b ¥q€2YI¢•£H¤¥T¡U ’ ©“ w ¥E££EP£¤ b F q W ( ¤ W F ¡ V ¤ x • s U v I S G ¤ W ™ S q • F G q t W v¥ET©•H©Gqt¥¤U b (¥¤4yI£¢•¥¤V¥T¤¡U ’ © w £EEP£¤ b G V F W q 2 W F ¡ x “ • s U v G T V • F G q t W v F G q t W ©£¤ b g T W T F G ” t W A ¡ ¤ V T ¤ j • n ©Hh£hE££u• 5b v • • ¡ T S ¤ R G T ™ ¡ uE©E©¢ o v F G q t W ¤ EP£U b vEI£S¥GE¤£W£™Sq•EGt£¤¥U b qE42YR¥U ’ © w ¥E££EP£¤ b F q W ( ¤ R W x “ • s U v I S G ¤ W ™ S q • F G q t W vET•EGqtE¤£U b (£q4Y¥U ’ © w £EEP£¤ b G V F W ¤2 R R W x “ • s U v G T V • F G q t W v F G q t W ©£¤ b g T W T F G ” t W A I S G ¤ W ™ G q R R ¨ • n ¥£©E@£§¥£¥¥h• 5b v • • T S ¤ R G T ™ ¡ ¥§£©¢ o v ¤ ¡ G q • ¢ q G | • ¢ c S ™ q ¢ h ¥¥£E£8e¥©U b v¢¥qGE|£•¢`cdS¥™U b ©%y¥©¥£E£8e¥h b q ¢ h R ¡ &2 Q ¢ ( I G v ¢ q G | • ¢ c S ™ q ¢ v ¤ ¡ G q • ¢ q G | • ¢ c S ™ q ¢ ©£¥E£Hd¥h b v • • ¤ ¡ G ” ¢ q ¥–Hp¥¥G ’ 8e¥¢ w ¥h• 5b ¢ c S ™ q ¢ y A • n v t I G R R W c • ¤ ¢ q X T ¢ ¡ ©H©`d¥£¥£U b vE¥¥¥£EU b 4DE£¥£V b I T ¢ ¤ ¤ W R • q T V S (2 ƒ v I T ¢ ¤ ¤ W R • q T v¢8ceWHI•¥¥¥£U b ¥E©8eE¥£¥£¡ b ¤ ¢ q X T ¢ ¡ I S G ¤ ™ W v ¢ c W I • ¤ ¢ q X T ¢ v t I G R R W c • ¤ ¢ q X T ¢ ©H¥`d£¥¥¡ b v • • t I G R R W  ¤ ¢ q X T ¢ j I S G ¤ ™ ¨ • n r©H©Eu¥¥¥¥¥{¥Eh• 5b v ¤ ¢ q X T ¢ ¡ ¥£¥£U b

3.3

The project is now created, it’s time to check that everything is all right. Restart Tomcat and go to http://localhost:8080/registeruser/ (if $CATALINA HOME/webapps/registeruser is your $APP BASE, else adapt it to your case. I will use http://localhost:8080/registeruser/). You should see a directory listing with files .classpath, .project, and a src directory. It is normal that you don’t see the WEB-INF directory.

3.3.1 First draft

Back to Eclipse, right-click on registeruser to add a new file. Call it index.jsp, and edit it to get the following result:

Isabelle HURBAIN

The first page

8 Jakarta Struts - A beginner’s tutorial

Do not forget to close all your tags! You can do it either with a closing tag (like in ) or with a closed tag (like in ). Save the file, reload http://localhost:8080/registeruser/ and admire the result;-) OK, it’s not that impressive. Let’s talk about the code. The first line is a requirement to tell Tomcat that this page is a JSP and that it should treat it as such. The lines import new tags, such as the that you can see below. The html taglib could be the only one to insert - however I just copy and paste the whole beginning of the document so that I’m sure I do not forget anything;-) The rest of the script is rather classical, except maybe for (that renders a tag) and (that renders a tag to point the absolute reference of the current file).

Reload your application. In most browsers there is a way to configure the language of it, so do it - put French (fr) in first and reload index.jsp.
2 ¡ ¤ V T ¤ j I S G ¤ W ™ G q R R W ¢ T ¢ G c ¢ T R W c ¤ ¡ ¢ G ™ ¢ ! DE¥¥£{¥E¥E¥–¥£`Pp8ih¥ui`¥¥4y£©G 0 ¤ Q ¢ ¤2 Q ¢ ( I ¡ ¤ V T ¤ j I S G ¤ W ™ G q R R W ¢ T ¢ G c ¢ T R W  ¢ q ¤ G ¤2 Q ¢ ( I n E¥£{£E£¥E–££iPpEhr¥E4y£©G

Now you can reload you application by going to http://localhost:8080/manager/reload?path=/registeruser. As you can see, there is not much difference. Now create a file ApplicationResources fr.properties $APP BASE/WEB-INF/classes, and edit it so that it looks like that:

2 I S G ¤ W ™ G q R R W ¡ ¤ V T ¤ j ¤ ¡ T G | g c ¡ G ¡ G y A Y£E£¥E@¥£§©EEhH“u¥H¥@i`¥¥4y£©G 0 ¤ Q ¢ ¤2 Q ¢ ( I I S G ¤ W ™ G q R R W ¡ ¤ V T ¤ j ¤ ¡ T G | g  ¢ q ¤ G ¤2 Q ¢ ( I n ©¥E£¥¥EhE¥–Euhr¥E4y£©G

It is really easy to create internationalized web application that display things according to the language of the client browser. You have seen in the web.xml file a section concerning resource bundles - that’s what we will use now. Create a file ApplicationResources.properties in $APP BASE/WEB-INF/classes, and edit it so that it looks like that:

‰ ˆ w † ‹ D…i#`…

‰ ˆ w † ‹ Œ ˆ w † ‹ ui#’‘i#`…

‰ ˆ w † ‹ Œ ˆ w † ‹ ui#’‘i#`…

Ž ‡ ˆ  ‚ E5#y†

‰ „ ˆ † ‡ † Š … ‰ „ ˆ † ‡ † Duy8ƒiDDDu5`ƒi…

v ( W ¢ y £U b v¥¢£WEp#`©y b U ¡ F" q c ¤ v¢£q¤GE¤£U b7n IS¥G¤£W¥™GqRRWu¡E¤V£T¤£j–¤¡TE|rgv¥E¤ b G ¢ q ¤ G v ( W ¢ y b v q c ¤ y" q c ¤ 8©€#8©y b v B e ™ G t S q e Q G | ¢ T R e ( q ¤2 ™ G t S q • ¡ ¤ V T ¤ ¡ pHEP`©E¥pi£E4D¥E££E¥©U ’ ©E w d`£E@©£@B b x “ • s U e G T V F G q t W ¤ „ v B e q c ¤ y e Q G | ¢ T R e ( q ¤2 q c ¤ y • ¡ ¤ V T ¤ ¡ i88©z`E¥hi¥4D`©£E¥©U ’ ©E w d`£E@©£@B b x “ • s U e G T V F G q t W ¤ „

3.3.2 A bit of internationalization

Isabelle HURBAIN

Now, edit your index.jsp file to replace

‰ „ ‡ ‚ Ž #`yY`…

‰ Š „ ‡ ‚ ŽŒ ˆ w † ‹ u#`yƒf…i58…

v q c ¤ y" q c ¤ y `P€#`PU b v g ( S F EU b 2 I S G ¤ W ™ G q R R W ¡ ¤ V T ¤ j ¤ ¡ T G | g c ¡ G ¡ G y Y©¥E£¥¥EhE¥§EE@Hv@H¥A v g ( S ¥£EF b

with

‰ — „ ™  † — – „ ˆ ‚ • ‰ ˆ ˆ w † ‹ Œ ˆ w † ‹ šY1D¥˜uD1yu”`5’‘`5`…

(in title) with

‰ Š — „ ˆ † ‡ † ¨ § „ ‘ € ‡ — – x „ ¥ „  ‚ ‡ ‡ „ wŒ € ‚ „ Ž D}šy`Yz©u#5HH˜#1y¦#11Dy…7¤`DY8…

(in body) with

‰ Š — ¯ † § „ † ¨ § „ ‘ € ‡ — – x „ ¥ „  ‚ ‡ ‡ „ wŒ € ‚ „ Ž D}yuu#z©u#5HH˜#1y¦#11Dy…7¤`DY8…

9
¨ € ‰ ‡ † ‚ • ‡ ˆ † † ‚ ‡ † ™  † Ÿ † ‡  ‡  x w ‡ ‡ ‡ ‡ ¤`1Y1u5YD`®…51Di 8…8Yžy­#¬#…‹ ¢ € ‰ ‡ † ‚ • ‡ ˆ † † ‚ ‡ † ™  † Ÿ † ‡  ‡  £iiƒiD#ƒu`¡ƒ5u1 H…`yžx ‰ ˆ w † ‹ Œ ˆ w † ‹ ui#’‘i#`…   ª «  › œ  ‰ „ ‡ ‚ Ž Œ ˆ w † ‹ DiyY’‘i#`…

Jakarta Struts - A beginner’s tutorial

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

So we’ve seen a new tag: bean:message . This tag is used to get messages form resource bundles (like ApplicationResources.properties) and to display them. If you have a package for your application, you can put your resource bundle in the directory of your package (if you have a package com.ihurbain.registeruser it will be in $APP BASE/WEB-INF/classes/com/ihurbain/registeruser for example), provided that you modify your web.xml file to fit it (in this example, you should replace ApplicationResources.properties with com.ihurbain.registeruse After this little deviation, let’s go back to our application.

3.4

Enter the application and create the data source

Let’s suppose you want to make an authentification at login. To do this, you need a little form with a login and a password. We will do a very basic identification, just for example purpose, with a login ihurbain and a password foobar. We also want the registration utility to be available from now on. IMPORTANT NOTICE Doing an authentification the way it’s done here is generally not a good idea! It is just for example purpose... So we’ll need four things: a JSP form
 

something to store the login information
 

something to check the authentification
 

something to create the data source
 

Struts uses a configuration file called struts-config.xml to define the behaviour of an application. In this file, we define how actions demanded by the client must be treated.

3.4.1 The JSP form
Let’s first create the login form. Edit your index.jsp to get a file like this:
vBpHEP`©E¥pi£E4D¥E££E¥©U ’ ©E w d`£E@©£@B e ™ G t S q e Q G | ¢ T R e ( q ¤2 ™ G t S q • ¡ ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ vi88©z`E¥hi¥4D`©£E¥©U ’ ©E w d`£E@©£@B B e q c ¤ y e Q G | ¢ T R e ( q ¤2 q c ¤ y • ¡ ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ v B e I W ¢ F e Q G | ¢ T R e ( q ¤2 I W ¢ F • ¡ ¤ V T ¤ ¡ i1©z`E¥hi¥4y£E¥©U ’ ©E w d`£E@©£@B x “ • s U e G T V F G q t W ¤ „ v B e W X W & e ¢ t W V t I W q ¢ t W R „ i8P`£E¥©r£hB b b

v ( W ¢ y £U b v U ¢ ¡ W F" q c ¤ ¥£Ep#`©y b v¢¥q¤G¤£U b vUhe`¢¥q¤GE¤42yQ£¢(I©G©e8g¥¢E)u£¥¥8²Y©F b ¥E¤ b ¢ t W ¡ ¡ ¢ c" I W ¢ v ¢ q ¤ G v ( W ¢ y b v e ¢ V T ¤ e ¢ q W ™ S q q c ¤ y" q c ¤ P`£e`£¥r8©€#8©y b

±

vUue`£SE©¡¥E€yEd´`¥E@¥8³YF b ( T h ¡ W R2 ¤ R c S T R e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v U T rF b v Uhe1©GEP`¥¥u¥€#`©y b I t S q e g ¤ T ¢ R S T R ¤ Q ¢ ¤" q c ¤ v Uh1I©£S%2y¤REd´`¥E@¥8³YF b e G t q c S T R e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v e I G t S q U e I S G ¤ ™ W c T S |" q c ¤ d1©E£¥P`©¥EtP£4#8©y b v g ( S ¥£EF b

°

b b

10

v e ( ¤ (2 6  0  t G | I S ™ • ¡ ¤ V T ¤ ¡ U ¡ ( ¤ ( U ¡ ¤ V T ¤ ¡ U t T S2 ¢ y ™ W R W2 W ¤ T W ) W & U U" R ¤ ¤ y P1¥4y¥©H£E£E¥E¥¥¥£%5©E7#£E©7Y¥´e e x  U U 62 0 I S G ¤ W T V t G | I S ! ¡ ¤ V T ¤ i41­¥E£¥E@E££j o A o ¥E£S ’ £E¥–©¥£de U U I S G ¤ W ( I V ¢ T W h ¤ | S j ¢ y ™ W R ¨ U U • ! “ ” s — 9 t G | I S ™ • ¡ ¤ V T ¤ ¡  9 q A ! on H¥r¥£E¥©d¥¥£ p5b v g H¥¥¥¥©`©E££H£i`71£`¥¥E¥h8©Q b e 0 • m l k k •  j “ e t I G ( S ™ I ¢ e 62 0 e I S G ¡ T ¢ X q c g

It is a good idea to always externalize your strings; it is not much more work to do when you create a JSP and it can save you a precious amount of time if you want to modify them. There are several new HTML tags here:

This file must be created into the $APP BASE/WEB-INF/ directory. It is a XML file, divided into 3 main sections.

This is normal. Look at the tag: we define an action, /login. But where is this action defined? At the moment, nowhere. It is time to look at the struts-config.xml file.

‰ w  ‰  Œ ˆ w † ‹ `#z…i58…

I S G ¤ ™ ¢ q q S ™ ¡ I W ¢ s c T £E¥¥¥–HE©£S ’ ¥Eu£rE©EE©¥EE@©Er£ºY©¥E£E¥¥£¥¥7y¥¥¥¥£fy£& I S G ¤ ™ ¨ T S ¡ t I G R R W  I S G ¤ ™ ¨ ( I G | ¤ S I I W ! " I S G ¤ R ¢ ™ Q  ¤ ¢ q X T ¢ j2 ¤ ¢ q X T ¢ ¡2 Q W X W

Isabelle HURBAIN

and your ApplicationResources.properties to get

v q c ¤ y" q c ¤ y `P€#`PU b v g ( S F EU b v c T S |" q c ¤ y `©4#`PU b v ¤ G c F V ¡" q c ¤ y `©fD`©U b vUh1©E£¥%y¥©©`£Er£¥H²YF b e I G t S q2 Q ¢ ( I G e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ¤ G c F V ¡" q c ¤ ©i©%#`©y b v U T rF b v U e ( T S h ¡ ¡ W R e g ¤ T ¢ R S T R ( T S h ¡ ¡ W R" q c ¤ @i£EPEz8¥r£©Ep#`©y b

renders a

‰ w  ‰  `#1…

renders a

‰ — † § „ † — – „ † x † † ™ † € ‡ ¶u5¥˜Yu¬5uDE5…

renders a

‰ — ‘  ‰ ˆ ‡ ‡ ‚ † — – „ † x † † ™ † € ‡ Eš#1yHuyY£˜Y1D·5uDE5…

‰ – x †  „ † ‰  † ‘  ‰ ˆ ‡ ‡ ‚ † Œ ˆ w † ‹ 5DDD1Y`55¦#1yEDyY’‘i#`…       ‰ – x †  „ † ‰  † † § „ † Œ ˆ w † ‹ 5#uD1Yi#5®uu#z‘i#`… ‰ – € ‰ ‡ † • ‚ w  ‰  Œ ˆ w † ‹ #y`1ƒ85µiDe‘i#`…

I G t S ” ©£Eh§©£%y£©G I G t S q2 Q ¢ ( I T ¢ ¡ — T ¢ ¤ ¡ G t ¢ ~ S ¤ ¢ c S ™ q ¥£H£¥¥E¥˜u8d¥¢ w r¥E4y£©G ¢ q ¤ G ¤2 Q ¢ ( I

renders a

‰ — † ‡ w Ž ™ ‡ — – „ † x † † ™ † € ‡ ¶`¸uE`šuyD¹5DuH#…

3.4.2 The struts-config.xml file

If you reload the application and your JSP you have a nice error message beginning with ,

" ( T S h ¡ ¡ W 9 ( T S h ¡ ¡ W R2 ¤ R c S T 5£E©¥E£¥£E©E€ydR " I G t S ” I G t S q2 ¤ R c S T Y©E££©E%ydR

Let’s edit this file.

The form-beans section. The form beans are used to store the information from the form and to validate them. A form bean is associated with each form.

The action-mappings section. This maps an action to a name and describes what should be done.

The global-forwards section. Once an action is performed, its results are forwarded to another page.

tag

11
      ‰ † ‡ w Ž ™ ‡ Œ ˆ w † ‹ 5`¸D£‘i#`…  

tag. tag Jakarta Struts - A beginner’s tutorial tag

¿ ¨ † † † ‹ ¨ † „ ˆ Á  „ ‡ ¨ § ‚ Á ‚ ©¤u5fyui¼1y1DÀ

¿ ¨ € ‰ ‡ † • ‚ ¨ ‡ † ™  † ‡ ¨ „ ‹ • ‚ † ‚ ¨   ©¾i1Y`#P½ƒ51D8©yH5Y`d¼#1‰ f

Create a new class in your project by right-clicking on src, New Other, then choosing Java Class. Put LoginForm in the name field, and org.apache.struts.action.ActionForm in Superclass, and click Finish. A new Java file has been created. The packages and should also be imported. Edit this file to add a String login property and a password property:
c T P£S ’ ©£EvY£H££E¥S ’ £E©¥75%D£& I G t S ” " I S G ¤ R ¢ ™ Q  ( I V ¤ S x ¡ ¡ W q !2 t I W q2 W X W " c T P£S ’ ©£Ei¥§£–uPE£©d¥E£EvY©¥E£E¥¥£¥¥7y¥¥¥¥£fy£& I G t S ” ¡ ¡ W q ™ | S I W ¢ F t I G ¤ W ¢ T ™ I S G ¤ R ¢ ™ Q  " I S G ¤ R ¢ ™ Q  ¤ ¢ q X T ¢ j2 ¤ ¢ q X T ¢ ¡2 Q W X W ‰ ‡ € ‚ „ Ž » w  ‰  1¸8#y``D…

The first lines are XML definitions lines - they define the document type. The real configuration begins with . We first define an action (in the section). The action path corresponds to what we used for the form action, that is to say ”/login”. This is the path to be mapped to the action. Then there is a type: . this is the name of the Servlet class behind the JSP The name corresponds to a bean defined in . The scope defines in which context the login information will be available; we want it to be session-wide, so we put ”session”. The input property defines from where the action is called. The section also contains an element: it is the bean associated to the login form. Its name corresponds to the ”name” field of the action mapping and its type is the name of the used class. The forward maps a name to the following JSP in the application flow. When we reload the application and the index.jsp, we have another error:

‰ ‡  € ‡ † † ‚ w » € ‰ ‡ † • ‚ 1ƒ5H…D`…HYi1Y`#D… ‰  ‡  € ‰ • » ‡ † ™  † ‡ #iyyii5i…51DHy…

v ¡ ( T W h T S | • q W F S q t £E£E£££¥EU b v U e R ¡ &2 V I ¢ c I G W c U e y ¤ W R e V I ¢ c I G W ce ¢ c W h1©%y£8P`dP8¥£E˜18©8f`8eEI ( T W h T S E¥£| b v ¡ ( T W h T S | • q W F S q ¥E£¥££¥Et b v ¡ I W ¢ F • c T S | £H©`©£U b vUPeD©S ’ ©EEz`¥¤ c T I G t S ” e ¢ R g e c T D©S ’ ©EP`HdEI I G t S q e ¢ c W I W ¢ F • c T S ©`©£| b v • • I W ¢ F c T S | I S t S ” • n §©§©rEp• #b v ¡ I W ¢ F • c T S ¥H`©£| b v t G | I S ™ • ¡ ¤ V T ¤ E£¥E£¡ b

3.4.3 Create the ActionForm and Action

Isabelle HURBAIN

This is logical, as we didn’t create the LoginForm class. So let’s do it.

12
‰ ‡ € ‚ „ Ž » w  ‰  1¸8#y``D… v t G | I S ™ • ¡ ¤ V T ¤ ¡ ©E¥¥E¥¥U b v ¡ t I G R R W c • I S G ¤ ™ W £E©H¥`dE©¥E¥U b v I S G ¤ ™ W E£EU b vPi©%y£©d`¥©G e R ¡ &2 Q ¢ ( I G U e ¤ V R I e I S G ¡ ¡ ¢ ¡ e ¢ R S ™ 1££P`£¡ e c T D©£S ’ ©E£¥P`8eEI I G t S q e ¢ c W ei¥E©E£z`£¤ I S G ¤ ™ ¨ I G t S ” e ¢ R g e I G t S q U e y ¤ W i©E£¥P`¥ER I S G ¤ ™ ©¥EW b v • • I S t S q T ¢ ¡ V W ¡ ¡ ¢ ™ S T 9 • n –£r¥£H˜u£p• #b v ¡ t I G R R W c • I S G ¤ ™ ¥E©E`d¥EW b

Jakarta Struts - A beginner’s tutorial

You can generate the accessors with Source Generate getter and setter. You have to define a getter and a setter for each property of a formbean class. Save this file, reload the application and the index.jsp and you can see a nice login form. But if you fill in the form and validate, you still have an error:

Now reload the application and you should not see any error. Great, isn’t it? Well, actually, not exactly. We do have a form, but it doesn’t perform much. You can enter with any login/pass.
V I ¢ c I G W c ¢ y ¤ ¡ G ¡ G y A I S G ¤ W ¤ I ¢ ¡ ¢ T R2 V I ¢ c I G W n £8h©`˜¥˜}E¥¥£E££pY£8©©`c V I ¢  I G W  ¢ q ¤ G ¤2 V I ¢ c I G W ¥£§©E£¥¥E€Y£8©©`c

This class must contain the method as defined above; at the moment, perform only returns a forward to the next JSP Save this file, reload the application, fill in the form and validate: the error is now .

Still nothing abnormal, as we didn’t create any Action Servlet. So create the class LoginAction in your src directory; this class must have org.apache.struts.action.Action for superclass. Edit it to have something like this:

2 ¢ q F W q G W X W g q ¤ I ¢ T T V ™ ¤ S I ¡ G Æ ¢ q F W q G W X W I V g q ¤ I ¢ T T V ™ ¡ G I S G ¤ ™ W ¤ ¢ q X T ¢ jÄ ¢ ™ G X T ¢ ¡ ( ¢ ¤ ¡ ¢ V à ¢ T ¢ y 5¥E©¥§££©§EiuH¥¥£Eu££©u{©¥E§£¥¥£fÅ£E¥¥£{¥©¥¥@¥A

Isabelle HURBAIN

and create the corresponding messages in ApplicationResources.properties:

vBpHEP`©E¥pi£E4D¥E££E¥©U ’ ©E w d`£E@©£@B e ™ G t S q e Q G | ¢ T R e ( q ¤2 ™ G t S q • ¡ ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ vi88©z`E¥hi¥4D`©£E¥©U ’ ©E w d`£E@©£@B B e q c ¤ y e Q G | ¢ T R e ( q ¤2 q c ¤ y • ¡ ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ v B e I W ¢ F e Q G | ¢ T R e ( q ¤2 I W ¢ F • ¡ ¤ V T ¤ ¡ i1©z`E¥hi¥4y£E¥©U ’ ©E w d`£E@©£@B x “ • s U e G T V F G q t W ¤ „ v B e W X W & e ¢ t W V t I W q ¢ t W R „ i8P`£E¥©r£hB b b b b

2 ¢ q F W q G W X W ¤ S I ¡ G Æ R ¡ &2 V I ¢ c I G W c UÄ ¢ ™ T V S ¡ ¢ T ( ¢ ¤ ¡ ¢ V à ¢ T ¢ y 5£E¥£–£˜r`©7Y£8©©`d%Å£E£r¥©¥¥@¥A w  ‰   „ `#Dy†

C Æ Æ e V I ¢ c I G W ce Ä ( T W h T 1£8`%P5£E££S ’ PE4yPH`²y¥¥£T ( I G |2 t I G R R W cÄ I T V ¤ ¢

Ç}IS¥GE££E£¥¥£¥Èy¥E££E©¥rH¥¤ ¤ R ¢ ™ Q  ¤ ¢ q X T ¢ j r I S G ¤ R ¢ ™ Q   “ ¡ h S T y Æ ¢ ¡ I S R ¡ ¢ T ¢ ¡ I S R ¡ ¢ ~ ¤ ¢ q X T ¢ j R ¤ ¤ HHE©{£EE©¥¥¥¥¥¥E¥¥ r ¤ ¡ ¢ V à ¢ T ¤ ¡ ¢ V à ¢ ~ ¤ ¢ q X T ¢ j R ¤ ¤ 5£¥d©¥¥¥¥¥¥¥¥E¥¥ r c T S | c T ƒ©£}©£S ’ ¥E©E¨ I S G ¤ ™ r t I G R R W c t I G R R W  I S G ¤ ™ #©H©`u©H¥E©E¨ Ä c T S | T ¢ R ( T W h T …©£¥r£¥£S ’ ©¥EEiE¥R I S G ¤ ™ ¨ ™ G q F V Ç I S G ¤ ™ ¨ ¡ ( I ¢ ¤ Q ¢ I S G ¤ ™ ¨ I G t S ” ¡ ¡ W q ™ ™ G q F V {¥EhE¥§£EEPE£Ep¥uER Cuƒf2YI©S¥GE7DE¥¥f5PE%5£–EEeG ¤ ™ W2 ¡ ¤ V T ¤ ¡2 ¢ y ™ W R W2 t T S ¤ T S R c Cufy¥€5¥¥££fy££–EEeG ƒ2 R ¤ ¤ y2 ¤ ¢ q X T ¢ ¡2 Q W X W & ¤ T S R c C ƒ2 ¤ ¢ q X T ¢ ¡2 Q W X W & ¤ T S R c uf5¥¥££fy££–EEeG C I S G ¤ R ¢ ™ Q   “2 S G2 W X W & ¤ T S R c y©¥E£E¥%#¥f#£–EEeG

f

So let’s create a mainmenu.jsp file:

13
b b b b v c y" q c ¤ y q`P¤€#`PU vEU g ( S F v¥Ure1IS¥G¤£W¤I£¢¡¢TR€2yVI£¢8cIPGW`cfe`g¥¢E)@¢t£¥¡¥¢8c²"Y©F W ¡ I W ¢ v g ( S ¥£EF v ( W ¢ y £U b v U ¢ ¡ W F" q c ¤ ¥£Ep#`©y b v¢£q¤GE¤£U b vUue8¢¥q¤GE¤€2YVI£¢8c©I©GW`cfe8g¥¢E)u£¥¥8²Y©F b ¥E¤ b ¢ t W ¡ ¡ ¢ c" I W ¢ v ¢ q ¤ G v ( W ¢ y b v e ¢ V T ¤ e ¢ q W ™ S q q c ¤ y" q c ¤ P`£e`£¥r8©€#8©y b É É C ( T S h ¡ ¡ W R t I G T ¤ j ¢ ¤ W X G T 5£EPEuPE£r£ER C I G t S q t I G T ¤ j ¢ ¤ W X G T yPE£§PE£r£ER

Jakarta Struts - A beginner’s tutorial

v ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S q ££¥£€DEU b v U e R ¡ &2 Q ¢ ( I G U T ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ¤ ™ ¢ T G ( ¢ T" ™ G t S ri%y¥©££H¥¥E¥£P`£r©E¥4uE£q b v e ¡ T ¢ ¡ V e ¢ c W I ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S PH¥£Hz88dE–£¥E€D£q b

v ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S q ££¥£€DEU b v U e R ¡ &2 Q ¢ ( I G U T ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ¤ ™ ¢ T G ( ¢ T" ™ G t S ri%y¥©££H¥¥E¥£P`£r©E¥4uE£q b v e c T Pu©£S ’ PE£P88dE–£¥E€D£q b I G t S q e ¢ c W I ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S

v T y b EU b ¥£¥4DE¥¢ v q V T ¢ ¤ S S |2 ¡ T S T T v ” " t I G ( ¢ ¢ ™ S T R ¢ T S | ¢ F Æ ¡ Ä T S T T ¢ t I G h S q q S | ¢ y ¤ ¤ ™ ¢ T T S ™ ¤ ¡ V c V S E— b yPE¥£h¥˜f5££–©H¥@¥r¥£¥§EEpEq v Ì y U b U b £¥d£E£EP`¥e`£¥t| b y b ¥£€DE¥¢ v ¤ I S | T S T T  I S G ¤ W ( G q W  v e ( ¢ T e T S q S ™ ¤ I S v Ì T ¢ ( W ¢ y2 ¡ T S T T

To correct it, we should add a method to the ActionForm. The method will check that the login and pass are OK before it allows the user to enter the application. Edit the LoginForm file and add this method:

When an error is detected, the forward is done to the input read in the action tag of struts-config.xml - here, back to login.jsp. To display the errors, you can add a tag in the body of index.jsp. This displays the error, but lacks formatting. You can add something like

‰ Š ‡  ‰   „ Œ ˆ w † ‹ D¦…15ud‘`5`…

2 ( T S h ¡ ¡ W R ( I W I G t S q V S g ) ™ ¢ y ™ ¢ ¡ W ¢ q 9 2 ( ¢ q G W | I G t S ” I G t S q2 T S T T 5£E©¥E@©{©E£r§P©–£¥EËy£¥¥{©EE£©£%y¥¢

É É C ¡ T S T T ¢ I T V ¤ ¢ uE£¥¥§¥¥£T C Æ Æ e I G t S q2 T S T T ¢ e Ä T S T T  I S G ¤ ™ ¨ h ¢ I r e I G t S q e Ä ( ( W2 ¡ T S T T 1£i©E£7y££dd5££EErÈ`i©E£dd5£7DE£¥¥¢ C Æ Ä ¡ T S T T  I S G ¤ ™ ¨ h ¢ I 1zE£¥E©Er£i@E£¥¥uE£¥E©E¨ ¡ T S T T ¢ ¡ T S T T  I S G ¤ ™ Ç ¢ ¡ q r£¢ C q q V I I T V ¤ ¢ Dr¥¥£T Æ Æ e T W F S S | e Ä ¡ q W V à ¢2 ( T S h ¡ ¡ W R Ê Ê Æ e I G W F T V y G e Ä ¡ q W V à ¢2 I G t S qÄ | £i£eeuE£¥7yE©@r£i©E¥©©dE¥£7Y©Ef¦©G Ç Æ ¤ ¡ ¢ V à ¢ T ¤ ¡ ¢ V à ¢ ~ ¤ ¢ q X T ¢ j R ¤ ¤  r t I G R R W c t I G R R W  I S G ¤ ™ ¨Ä ¢ ¤ W ( G q W X ¡ T S T T  I S G ¤ ™ ¨ ™ G q F V h8¥¥{¥£¥¥¥¥¥E£5PH`u©H¥E£EE7D£@E£¥¥EpER

„ † ‚ ‘ ‡ ˆ ‚ 5i5i#D5Á

„ † ‚ ‘ ‡ ˆ ‚ #1#i5uyÁ

3.4.5 Creating a data source

in mainmenu.jsp. This little piece of code checks the existence of the loginForm bean (which is not generated if someone enters the application directly with mainmenu.jsp) and redirects the request to index.jsp if it does not exist. So now we have a login system. We also wanted to create a data source.

We want the application to have a global users pool. To do that, we can simply create a vector of users with an application scope. You can edit your LoginAction.java to add

in your resource bundle; it will be much nicer. This is still not perfect: if someone directly types the mainmenu.jsp, he can enter anyway. We can correct it by adding

in the perform method. This way, if the users vector does not exist, it is created and set as a parameter of the application. To ensure there is no problem, you can add a

C Æ ¡ T ¢ ¡ V r e ¡ T ¢ ¡ V e Ä ¢ ¤ V F G T ¤ ¤ ¨ ¤ ¢ ¡2 Æ Ä ¤ Q ¢ ¤ I S ! ¤ ¢ q X T ¢ j ¤ ¢ t2 Æ Ä I S G ¡ ¡ ¢ j ¤ ¢ t2 ¤ ¡ ¢ V à ¢ 1E¥H“`HE£¥Hzd5E¥¥£fuz#¥¥¥£¥¥€zy£¥¥4y©¥¥T C Æ Ä T S ¤ ™ ¢  h ¢ I 1z#£u£i@E¥£Hh£ ¡ T ¢ ¡ V T S ¤ ™ ¢ Ç Æ q q V I ©EEi§£H£HzP#©E¥¥7´5©¥¥E£¥¥€zy£¥¥4y©¥¥75G Æ e ¡ T ¢ ¡ V e Ä ¢ ¤ V F G T ¤ ¤ ¨ ¤ ¢ t2 Æ Ä ¤ Q ¢ ¤ I S ! ¤ ¢ q X T ¢ j ¤ ¢ t2 Æ Ä I S G ¡ ¡ ¢ j ¤ ¢ t2 ¤ ¡ ¢ V à ¢ TÄ |

3.4.4 Refining the login process

When the login is validated, this method returns null, so that no error is returned. When the login is not validated, an error is returned. This error has an ”error.login” key, that we must define in the resources bundle:

Isabelle HURBAIN

in mainmenu.jsp.
É

14 Jakarta Struts - A beginner’s tutorial

vBpHEP`©E¥pi£E4D¥E££E¥©U ’ ©E w d`£E@©£@B e ™ G t S q e Q G | ¢ T R e ( q ¤2 ™ G t S q • ¡ ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ vi88©z`E¥hi¥4D`©£E¥©U ’ ©E w d`£E@©£@B B e q c ¤ y e Q G | ¢ T R e ( q ¤2 q c ¤ y • ¡ ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ v B e I W ¢ F e Q G | ¢ T R e ( q ¤2 I W ¢ F • ¡ ¤ V T ¤ ¡ i1©z`E¥hi¥4y£E¥©U ’ ©E w d`£E@©£@B x “ • s U e G T V F G q t W ¤ „ v B e W X W & e ¢ t W V t I W q ¢ t W R „ i8P`£E¥©r£hB b b b b

W ¤ W ( q W I S ¡ T ¢ R ¡Î T ¢ ¡ V £@E©¥E¥pf5¥£HÍ££E£¥£E££E€Y£8©©`c h ¢ G  T ¢ ¡ V h ¢ G X2 V I ¢ c I G W T ¢ ¡ V ( ( ¨ T ¢ ¡ V ( ( W2 V I ¢ c I G W ¥H§¥¥¥H¥£7Y£8©©`c

v q c ¤ y" q c ¤ y `P€#`PU v g ( S F EU vH)¥©%#`©U b ui¥£H¥££E4y£8P`f`¥E§££E8²Y©F b ©1P%Y¥£E££E¥£H£¥¥£P8¥–£©%#8©y I G q" q c ¤ y v U e T ¢ ¡ V h ¢ G X2 V I ¢ c I G W c e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v e R ¡ &2 T ¢ ¡ V h ¢ G X U T ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ) I G q" q c ¤ v U T ¥–F v ) I G q" q c ¤ y H¥P%#`PU b ¥ui¥H¥£7Y£8©©`f8¥E{¥8²y£F b Pi©fy¥H¥£¥¥£H£¥¥£P8¥–£©%#8©y v U e T ¢ ¡ V ( ( W2 V I ¢ c I G W c e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v e R ¡ &2 T ¢ ¡ V ( ( W U T ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ) I G q" q c ¤ v g ( S ¥£EF b b b b b b b b b b b b b b b b b v ( W ¢ y £U v U ¢ ¡ W F" q c ¤ ¥£Ep#`©y b v¢£qE¤£U b vue8¥E€Y£8©©`f8¥Eu£¥¥8²Y©F b ¥E¤ b ¤ G U ¢ q ¤ G ¤2 V I ¢ c I G W ce g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ¢ q ¤ G v ( W ¢ y v ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S q ££¥£€DEU vrei&%yQ¥(©GT££H¥¥E¥£P`£r©E¥4uE£q b U R ¡ 2 ¢ I U ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ¤ ™ ¢ T G ( ¢ T" ™ G t S v e ¡ T ¢ ¡ V e ¢ c W I ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S PH¥£Hz88dE–£¥E€D£q v ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S q ££¥£€DEU v UriR%2y¥¢I©UT£¢£¡HV¥¥E¥£P`£r©E¥4uE£q b e ¡ & Q ( G T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ¤ ™ ¢ T G ( ¢ T" ™ G t S v e c T Pu©£S ’ PE£P88dE–£¥E€D£q I G t S q e ¢ c W I ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S v e ¢ V T ¤ e ¢ q W ™ S q q c ¤ y" q c ¤ P`£e`£¥r8©€#8©y vBpHEP`©E¥pi£E4D¥E££E¥©U ’ ©E w d`£E@©£@B e ™ G t S q e Q G | ¢ T R e ( q ¤2 ™ G t S q • ¡ ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ vi88©z`E¥hi¥4D`©£E¥©U ’ ©E w d`£E@©£@B B e q c ¤ y e Q G | ¢ T R e ( q ¤2 q c ¤ y • ¡ ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ v B e I W ¢ F e Q G | ¢ T R e ( q ¤2 I W ¢ F • ¡ ¤ V T ¤ ¡ i1©z`E¥hi¥4y£E¥©U ’ ©E w d`£E@©£@B x “ • s U e G T V F G q t W ¤ „ v B e W X W & e ¢ t W V t I W q ¢ t W R „ i8P`£E¥©r£hB

This will be a very short section. The menu allows the user to choose between adding a new user and displaying a user’s personal data. So we only need two links on mainmenu.jsp. Your mainmenu.jsp should now look like this:

3.5

3.6

We want to gather some information about a new user, so we define a new form, called adduser.jsp. You could do something like this:

Isabelle HURBAIN

As you modified the resources bundle, do not forget to reload the application.

You should also add in your ApplicationResources.properties

The main menu

Adding a user

15
b b b b b b v¢¥GEU b U@`¥4y¥£E¥£P8¥Eu£¥¥8²Y©F b ¥E¤ b q ¤ ¤ v e ¢ q ¤ G ¤2 T ¢ ¡ V ( ( W e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ¢ q ¤ G v ( W ¢ y v ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S q ££¥£€DEU vrei¡%yQ¥(©GT££H¥¥E¥£P`£r©E¥4uE£q b U R &2 ¢ I U ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ¤ ™ ¢ T G ( ¢ T" ™ G t S v e ¡ T ¢ ¡ V e ¢ c W I ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S PH¥£Hz88dE–£¥E€D£q v ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S q ££¥£€DEU v UriR&%y¥¢I©UT£¢£¡HV¥¥E¥£P`£r©E¥4uE£q b e ¡ 2 Q ( G T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ¤ ™ ¢ T G ( ¢ T" ™ G t S v e c T Pu©£S ’ PE£P88dE–£¥E€D£q I G t S q e ¢ c W I ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S v e ¢ V T ¤ e ¢ q W ™ S q q c ¤ y" q c ¤ P`£e`£¥r8©€#8©y

Jakarta Struts - A beginner’s tutorial

v q c ¤ y" q c ¤ y `P€#`PU v g ( S F EU vE)¥I©Gq%"Dq`c©¤U b vUue1)P™WEF€2yVI£¢8cIPGW`cfe`¥E§££E8²Y©F b ©1P%ƒ£H©`e¥£H£¥¥£P8¥–£©%#8©y y g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v e R ¡ &2 V I ¢ c I G W c U T ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ) I G q" q c ¤ v c T S |" q c ¤ y `©4#`PU v U ¤ G c F V ¡ " q c ¤ {i©©f#8©y v ¢ q F W ¤ ¥E©£U v T ¤ £U v ( ¤ £U b ¥@1£Hz8¥r¥4#`©y b ¤ b v U e y ¤ T G F e g ¤ T ¢ R S T R ¤ Q ¢ ¤" q c ¤ v ( v ( ¤ £U b u8££H€5¥£H¥¥£P`£Er£¥H²YF b ¤ b v U e ¢ ¤ W ( y ¤ T G F2 T ¢ ¡ V ( ( W e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ( v T ¤ v T ¤ £U v ( ¤ £U b ¥@8£`Pd8¥r¥4#`©y b ¤ b v U e q G W c ¢ e g ¤ T ¢ R S T R ¤ Q ¢ ¤" q c ¤ v ( v ( ¤ £U b ¥@8£`P75¥£H¥¥£P`£Er£¥H²YF b ¤ b v U e q G W c ¢2 T ¢ ¡ V ( ( W e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ( v T ¤ v T ¤ £U v ( ¤ U b @`8dE£P8¥r¥4#`©y b ¤ b v U e ¢ c W x ¤ ¡ W q e g ¤ T ¢ R S T R ¤ Q ¢ ¤" q c ¤ v ( v ( ¤ U b @`8dE£%5¥£H¥¥£P`£Er£¥H²YF b ¤ b v U e ¢ c W I ¤ ¡ W q2 T ¢ ¡ V ( ( W e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ( v T ¤ v T ¤ £U v ( ¤ £U b u88dE¥©EEe8¥r¥4#`©y b ¤ b v U e ¢ c W x ¤ ¡ T G | e g ¤ T ¢ R S T R ¤ Q ¢ ¤" q c ¤ v ( v (¤£U b u88dE¥©EE45¥£H¥¥£P`£Er£¥H²YF b ¤ b v U e ¢ c W I ¤ ¡ T G |2 T ¢ ¡ V ( ( W e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ( v T ¤ v T ¤ v T ¤ £U v ( ¤ £U b v G W " c y ¥S¥E(£T€#q`©¤©U b ¡HvdHHz`¥¢Xi`¥¤Ee`¥©@¥E£4#`©y b  e ¡ e V q W e ¢ q G ¤ e g ¤ T ¢ R S T R S G ( W T" q c ¤ v¥¥GE£W€"#`c©y©U b deize`¥Vie`¥¤Eee`¥©@¥E£4#`©y b S ( T q ¤ T v T  ¢ q W X ¢ q G ¤ g ¤ T ¢ R S T R S G ( W T" q c ¤ v ( ¤ b v ( ¤£U b @eEi©H²25¥£¡H¥(¥£Pe`£¢Er£¥H²YF b ¤ b v U ¡ c T c T ¢ V ( W g ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ( v T ¤ v ¢ q F W ¥¤ vPi£¢£HV¥(UP`©¥EtP£4#8©y e T ¡ ( W e I S G ¤ ™ W c T S |" q c ¤ vr1I£E£¤£¡Rpy¥¢£HV£(£Pe`g¥¢E@£¥¥8²Y©F U e S G ¤ W I ¢ ¢ T 2 T ¡ ( W ) ¢ t W ¡ ¡ ¢ c" I W ¢ v U ¡ T S T T ¢" q c ¤ rE£¥7#8©y v g ( S ¥£EF b b b b b b b b b b b b b b b b b b b b b b v ( W ¢ y £U b v U ¢ ¡ W F" q c ¤ ¥£Ep#`©y b

Isabelle HURBAIN

in their corresponding sections. The form bean, called UserForm.java, would be something like this:

v U e R ¡ &2 I S G ¤ W T ¤ ¡ G t ¢ T U e y ¤ W r1P%Y£E£©E¥P`¥ER

Then, you have to configure Struts, by adding

16
C ¤ ¡ ¢ V à ¢ ~ ¤ ¢ q X T ¢ j R ¤ ¤ 2 R ¤ ¤ y2 ¤ ¢ q X T ¢ ¡2 Q W X W & ¤ T S R c y¥¥£¥¥¥€y¥€5¥¥££fy££–EEeG vUueiR©¡%y££H¥P`©G &2 T ¢ ¡ V ( ( W U e ¤ V R I e ¤ ¡ ¢ V à ¢ T e ¢ R S ™ `¥£e`¥¡ e c T u©£S ’ ££Hz`¥8dEI T ¢ ¡ V e ¢ c W e I S G ¤ ™ ¨ T ¢ ¡ — e ¢ R g 1£EE££8´`¥¥¤ e T ¢ ¡ V ( ( W U e y ¤ W `¥£H¥¥£P`¥£ER e I S G ¤ W T ¤ ¡ G t ¢ T e ¢ c W 1¥E¥e`¥8dEI I S G ¤ ™ ¥W b ( T W h T S £E££| b v U e c T @u©£S ’ ££8´`¥¥¤ T ¢ ¡ — e ¢ R g e c T u©£S ’ ££Hz`¥8dEÏ©`P£| b T ¢ ¡ V e ¢ c W I I W ¢ F • c T S

Jakarta Struts - A beginner’s tutorial

Ç Æ Æ e e Ä ¡ q W V à ¢2 ¢ q ¤ G ¤ Ñ Ñ q q V I u£dE¥£75¥©E¾¡¥Ep¥§£E7#G ¢ q ¤ G ¤Ä | C Æ Ä ¡ T S T T  I S G ¤ ™ ¨ h ¢ I 1zE£¥E©Er£i@E£¥¥uE£¥E©E¨ ¡ T S T T ¢ ¡ T S T T  I S G ¤ ™ Ç Æ ¤ ¡ ¢ V à ¢ T ¤ ¡ ¢ V à ¢ ~ ¤ ¢ q X T ¢ j R ¤ ¤  r t I G R R W c t I G R R W  I S G ¤ ™ ¨Ä ¢ ¤ W ( G q W X ¡ T S T T  I S G ¤ ™ ¨ ™ G q F V h8©¥¥d¥¥¥£¥¥£E¥#©H©`rPHE¥¥E7#£@E©¥EEiE¥R É C ¢ q ¤ G ¤ #¥©Eh–¥©E4DE¥¤ ¢ q ¤ G ¤2 ¡ G y Ç Æ ¢ q ¤ G ¤ t I G T ¤ jÄ ¢ q ¤ G A ¤ ¢ ¡ ( G S X ™ G q F V hH£E§©E¥£f#¥E¥§iE¥R É C ¢ c W x ¤ ¡ W q #8d¥@{HdE¥%DE¥¤ ¢ c W x ¤ ¡ W q2 ¡ G y Ç Æ ¢ c W x ¤ ¡ W q t I G T ¤ jÄ ¢ c W x ¤ ¡ W ” ¤ ¢ ¡ ( G S X ™ G q F V hHHdE¥tPE£fD8dE¥©E¥¥§iE¥R É C ¢ c W x ¤ ¡ T G | D8dE¥©EEp{8eE¥E©E4DE¥¤ ¢ c W x ¤ ¡ T G |2 ¡ G y Ç Æ ¢ c W x ¤ ¡ T G | t I G T ¤ jÄ ¢ c W x ¤ ¡ T hH8d¥Ed©£f#HdE¥G ’ ¥§iE¥R ¤ ¢ ¡ ( G S X ™ G q F V É C q G W c ¢ D¥8P@r¥8P7DE¥¤ q G W c ¢2 ¡ G y Ç Æ q G W c ¢ t I G T ¤ jÄ q G W c  ¤ ¢ ¡ ( G S X ™ G q F V hE£`P{©E¥£fD¥`¥¥§iE¥R É C y ¤ T G F y¥©Hp{¥©H€DE¥¤ y ¤ T G F2 ¡ G y Ç Æ y ¤ T G F t I G T ¤ jÄ y ¤ T G s ¤ ¢ ¡ ( G S X ™ G q F V h`£H–©E¥£fy¥¥H¥¥§iE¥R É C ¢ q ¤ G ¤ I T V ¤ ¢ #¥©E–¥¥£T Ç Æ Ä ¢ q ¤ G A ¤ ¢ t t I G T ¤ j ™ G q F V hz#¥©E¥r©E£hE¥R É C ¢ c W x ¤ ¡ W q I T V ¤ ¢ #HdE¥{¥¥£T Ç Æ Ä ¢ c W x ¤ ¡ W ” ¤ ¢ t t I G T ¤ j ™ G q F V @z#HdE¥E¥¥r©E£hE¥R É C ¢ c W x ¤ ¡ T G | I T V ¤ ¢ #8eE¥E©E–¥¥£T Ç Æ Ä ¢ c W x ¤ ¡ T @z#8eE¥E©G ’ ¥r©E£hE¥R ¤ ¢ t t I G T ¤ j ™ G q F V É C q G W c ¢ I T V ¤ ¢ D¥8P§¥¥£T Ç Æ Ä q G W c  ¤ ¢ t t I G T ¤ j ™ G q F V hzD¥8¥¥r©E£hE¥R É C y ¤ T G F I T V ¤ ¢ y¥©Hr¥¥£T Ç Æ Ä y ¤ T G s ¤ ¢ t t I G T ¤ j ™ G q F V hzy¥©H¥¥r©E£hE¥R C y ¤ T G F t I G T ¤ j ¢ ¤ W X G T y£HuPE£r£ER C q G W c ¢ t I G T ¤ j ¢ ¤ W X G T D£`P–PE£r£ER C ¢ c W x ¤ ¡ W q t I G T ¤ j ¢ ¤ W X G T #8dE£§PE£r£ER C ¢ c W x ¤ ¡ T G | t I G T ¤ j ¢ ¤ W X G T D8dE¥©EErPE£r£ER C ¢ q ¤ G ¤ t I G T ¤ j ¢ ¤ W X G T #£ErPE£r£ER Ç c T Щ£S ’ ©¥EEiE£¥¥}©£S ’ ¥£8i¥uER I S G ¤ ™ ¨ ¡ ( I ¢ ¤ Q ¢ c T T ¢ ¡ — ¡ ¡ W q ™ ™ G q F V C5©EH£HE4Y©¥E7DE¥¥f5PE%5£–EEeG t I G R R W  I S G ¤ ™ ¨2 I S G ¤ ™ W2 ¡ ¤ V T ¤ ¡2 ¢ y ™ W R W2 t T S ¤ T S R c C c T ƒ©£S ’ £HE4Y©¥E7DE¥¥f5PE%5£–EEeG I S G ¤ ™ ¨2 I S G ¤ ™ W2 ¡ ¤ V T ¤ ¡2 ¢ y ™ W R W2 t T S ¤ T S R c C ¡ T S T T  I S G ¤ ™ ¨2 I S G ¤ ™ W2 ¡ ¤ V T ¤ ¡2 ¢ y ™ W R W2 t T S ¤ T S R c DE£HE4Y©¥E7DE¥¥f5PE%5£–EEeG C T S T T  I S G ¤ ™ ¨2 I S G ¤ ™ W2 ¡ ¤ V T ¤ ¡2 ¢ y ™ W R W2 t T S ¤ T S R c 5£HE4Y©¥E7DE¥¥f5PE%5£–EEeG

Isabelle HURBAIN

17 Jakarta Struts - A beginner’s tutorial

vBpHEP`©E¥pi£E4D¥E££E¥©U ’ ©E w d`£E@©£@B e ™ G t S q e Q G | ¢ T R e ( q ¤2 ™ G t S q • ¡ ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ vi88©z`E¥hi¥4D`©£E¥©U ’ ©E w d`£E@©£@B B e q c ¤ y e Q G | ¢ T R e ( q ¤2 q c ¤ y • ¡ ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ v B e I W ¢ F e Q G | ¢ T R e ( q ¤2 I W ¢ F • ¡ ¤ V T ¤ ¡ i1©z`E¥hi¥4y£E¥©U ’ ©E w d`£E@©£@B x “ • s U e G T V F G q t W ¤ „ v B e W X W & e ¢ t W V t I W q ¢ t W R „ i8P`£E¥©r£hB b b b b

As you can see, the validation is made on required fields. Of course, we could do something a bit more sophisticated, like checking the validity of the format of the email address or the validity of the birthdate. The Action gets the UserForm and adds it to the users vector:

Isabelle HURBAIN

And a last page to tell the user that all is OK is a good idea too. This page is called registration.jsp:

É É C1£ei£¥GE£WE©E¥zd5£¥£S ’ ©E4y©H8s¡¥¥£T Æ Æ I S ¤ T ¤ ¡ G t ¢ T e Ä ( T W h T ( I G |2 t I G R R W cÄ I T V ¤ ¢ C1Æ¡T¥¢£¡HV“r`eE¡ET¢£¡HVzeeÄ5¢¤VFGET¥¤¤¤¥£%2uz5¤Q¢¥¥¥¥¥4zy©¥£¥45¥£T ¨ ¢ ¡ Æ Ä ¤ I S ! ¤ ¢ q X T ¢ j ¤ ¢ t2 Æ Ä I S G ¡ ¡ ¢ j ¤ ¢ t2 ¤ ¡ ¢ V à ¢ C Æ e T 1Æ#c©£S ’ T¥¢HzP#©E¥¥45¥£75%DE¥HV ¡ V e Ä ¢ ¤ V F G T ¤ ¤ ¨ ¤ ¢ t2 ¤ ¡ ¢ V à ¢ TÄ ( ( W2 ¡ T ¢ ¡ C Æ Æ e ¡ T ¢ ¡ V e Ä ¢ ¤ V F G T ¤ ¤ ¨ ¤ ¢ t 1£H¥£Hze#PE¥¥42 Æ Ä ¤ Q ¢ ¤ I S ! ¤ ¢ q X T ¢ j ¤ ¢ t ´5¥¥£¥¥¥42 Æ Ä I S G ¡ ¡ ¢ j ¤ ¢ t zy£¥¥42 ¤ ¡ ¢ V à ¢ TÄ Æ T S ¤ ™ ¢  ©¥¥7­8£©4Ä hE¥Hu£ ¡ T ¢ ¡ V T S ¤ ™ ¢ Çt©¥E£E¥¥£¥¥Èy©¥E£E¥@H£¤ I S G ¤ R ¢ ™ Q  ¤ ¢ q X T ¢ j r I S G ¤ R ¢ ™ Q   “ ¡ h S T y Æ ¢ ¡ I S R ¡ ¢ T ¢ ¡ I S R ¡ ¢ ~ ¤ ¢ q X T ¢ j R ¤ ¤ H£H©E©–£H©E©£¥¥£¥E¥ r ¤ ¡ ¢ V à ¢ T ¤ ¡ ¢ V à ¢ ~ ¤ ¢ q X T ¢ j R ¤ ¤ 5©¥¥{¥¥£¥¥£¥E¥ r c T S | c T …©d©£S ’ ¥E¨ I S G ¤ ™ r t I G R R W c t I G R R W  I S G ¤ ™ 5PH`hPHE¥¥E¨ Ä c T S | T ¢ R ( T W h T …©£¥r£¥£S ’ ©¥EEiE¥R I S G ¤ ™ ¨ ™ G q F V Ç I S G ¤ ™ ¨ ¡ ( I ¢ ¤ Q ¢ I S G ¤ ™ ¨ T ¢ ¡ — ¡ ¡ W q ™ ™ G q F V {£EEhE£¥§©¥EE¥¥£8i¥uER Cuƒf2YI©S¥GE7DE¥¥f5PE%5£–EEeG ¤ ™ W2 ¡ ¤ V T ¤ ¡2 ¢ y ™ W R W2 t T S ¤ T S R c Cufy¥€5¥¥££fy££–EEeG ƒ2 R ¤ ¤ y2 ¤ ¢ q X T ¢ ¡2 Q W X W & ¤ T S R c C ƒ2 ¤ ¢ q X T ¢ ¡2 Q W X W & ¤ T S R c uf5¥¥££fy££–EEeG C T S ¤ ™ ¢ 2 q G ¤ V2 W X W & ¤ T S R c 5£©€#£E€#£–EEeG C I S G ¤ R ¢ ™ Q   “2 S G2 W X W & ¤ T S R c y©¥E£E¥%#¥f#£–EEeG

CÆÆe`¢8cdWEI£¤¡Wq%2u¡ET£STT£dd5££EErv``HdE¥Pd5¥£%DE¥¢ ¢ e Ä T S T T  I S G ¤ ™ ¨ h ¢ I r e ¢ c W I ¤ ¡ W q e Ä ( ( W2 ¡ T S T T Ç Æ Æ e e Ä ¡ q W V à ¢2 ¢ c W x ¤ ¡ W q Ñ Ñ q q V I u£dE¥£758d¥¤¡E¥h–8dE£f#G ¢ c W x ¤ ¡ W qÄ | C Æ Æ e ¢ c W I ¤ ¡ T G |2 ¡ T S T T ¢ e Ä T S T T  I S G ¤ ™ ¨ h ¢ I r e ¢ c W I ¤ ¡ T G | e Ä ( ( W2 ¡ T S T T 1¥`HdE¥E4D£¥ed5£¥¥Er£v``8eE¥E©Eed5¥£%DE¥¢ Ç Æ Æ e e Ä ¡ q W V à ¢2 ¢ c W x ¤ ¡ T G | Ñ Ñ q q V I –du¥E¥¥7#8dE¥©EE¾¥¡Ep–8dE¥©EE7#G ¢ c W x ¤ ¡ T G |Ä | C Æ Æ e ¢ q ¤ G ¤2 ¡ T S T T ¢ e Ä T S T T  I S G ¤ ™ ¨ h ¢ I r e ¢ q ¤ G ¤ e Ä ( ( W2 ¡ T S T T 1`¥4DE£¥dd#£¥¥Er£Ò``¥©Eed5¥£%DE¥¢ É É

18
v¥Uue)©S72YIS¥GE¤WT¤¡Gt¥¢Tee`g¥¢E)@£¥¥8²Y©F b ¢ t W ¡ ¡ ¢ c" I W ¢ v U ¡ T S T T ¢" q c ¤ ¥E£¥7#8©y b v g ( S ¥£EF b v ( W ¢ y £U b v U ¢ ¡ W F" q c ¤ ¥£Ep#`©y b v¢¥q¤GE¤U b vU@e`¢¥q¤G¤42yT¥¢£¡EV¥((£WPe8g¥¢E)u£¥¥8²Y©F b ¥E¤ b ¢ t W ¡ ¡ ¢ c" I W ¢ v ¢ q ¤ G v ( W ¢ y b v e ¢ V T ¤ e ¢ q W ™ S q q c ¤ y" q c ¤ P`£e`£¥r8©€#8©y b C ¡ T S T T ¢ I T V ¤ ¢ uE£¥¥§¥¥£T É É É

Jakarta Struts - A beginner’s tutorial

vBpH™ESP`©E|¥Rpi£qE4D™¥E£S£¡E¥©U ’ ©E w d`£E@©£@B e G t q e Q G ¢ T e ( ¤2 G t q • ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ vie88©¤z`E|¥Rhi¥q4Dq`©y£¡EV¥©U ’ ©E w d`£E@©£@B B q c y e Q G ¢ T e ( ¤2 c ¤ • ¤ T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ v Bi1I©Fz`QE¥¢hei¥¤4yW•£E¤V¥T©U ’ ©E w d`£E@©£@B e W ¢ e G | T R ( q 2 I ¢ F ¡ ¤ ¡ x “ • s U e G T V F G q t W ¤ „ v B e W X W & e ¢ t W V t I W q ¢ t W R „ i8P`£E¥©r£hB b b b b ‰ † • „ ˆ „ ‡ 5`y#`y… ‰ € ‰ ‡ † † ‰ Œ ˆ w † ‹ ƒ`1ƒ#ie`58… ‰ † • „ ˆ „ ‡ Œ ˆ w † ‹ 5`5u#`£‘i#`…

To get information about a user, you first want to know which user you want to know about. So we need another form! This one is very simple - it uses a and a to render a component from a vector.

2 ( ¢ T ¤ ¡ G t ¢ T I ¢ ¢ F ¡ W y T ¢ ¡ V ¢ y A y¥¥E¥r£˜E@¥Hh¥h}P%Y¥£¥E¥T ) S2 I S G ¤ W T ¤ ¡ G t ¢ q G W c  ¥8u¥¥`d7y¥£E¥£W q G W c ¢2 T ¢ ¡ V ( ( Æ g g g g U c c U ( (Ä ¢ ¤ W ( y ¤ T G s 8¥£`¥d7«£–¥¥Hp§£¥E€y¥£E¥£W ¢ ¤ W ( y ¤ T G F2 T ¢ ¡ V ( ( ¢ c W I ¤ ¡ W ” ƒ ¢ c W I ¤ ¡ W q2 T ¢ ¡ V ( ( 8dEhE@8eE¥¥%y¥£E¥£W ¢ c W I ¤ ¡ T 8dEuG ’ 8d¥E4y¥£E¥£W ƒ ¢ c W I ¤ ¡ T G |2 T ¢ ¡ V ( ( 2 ¡  DHh@`²y¥£E¥£W ¡ c2 T ¢ ¡ V ( ( 2 T  yh–E²y¥£E¥£W T c2 T ¢ ¡ V ( ( ¢ q ¤ G A ƒ ¥prui©E²y¥£E¥£W ¡ c T c2 T ¢ ¡ V ( ( 2 ( ¢ T G V à ¢ T ¢ T W ƒ y ¤ G h ( ¢ ) T W c ¡ ( q ¢ G | ¢ y ¤ q q ¨ 2 c T S | ¢ y ¤ ¢ ¤ ¢ q R c S ™ ¢ ¡ W ¢ q 9 I S G ¤ W ¤ I ¢ ¡ ¢ T R2 T ¢ ¡ V ( ( y£H¥£@£h{¥Hh¥E££8ºE££@¥h¥ː©@¥u¥¥EEe¥Ð£¥E£©¥E£££€y¥£E¥£W T ¢ ¡ V W ( ( ¨ ¥Hp–¥h–¥4y¥£E¥£W ¢ q ¤ G ¤2 T ¢ ¡ V ( ( 2 I G ¤ G q q G | ¢ ¡ W ¢ q 9 2 ( q ¢ G | ( ¢ T G V à ¢ T W ¡ G ¢ c W I ¤ ¡ W ” ¢ c W x ¤ ¡ W q2 ¡ T S T T YP§r£Eu££E£y££Er¥©H¥¥ph§8eE§©E£HdE¥%DE¥¢ 2 I G ¤ G q q G | ¢ ¡ W ¢ q 9 2 ( q ¢ G | ( ¢ T G V à ¢ T W ¡ G ¢ c W I ¤ ¡ T Y©r–¥Eh£¥Ëy££r¥H£¥ph–8d{E©G ’ 8eE¥E©E4DE¥¢ ¢ c W x ¤ ¡ T G |2 ¡ T S T T 2 I G ¤ G q q G | ¢ ¡ W ¢ q 9 2 ( q ¢ G | ( ¢ T G V à ¢ T W ¡ G ¢ q ¤ G A ¢ q ¤ G ¤2 ¡ T S T T YP§r£E@£¥EËy££Er¥©H¥¥i@d¥¥¥©E4DE¥¢ V I ¢ c I G W c S ¤ ) ™ W s £Hp©`“–PpdPEpY£8©©`c ) ™ W F2 V I ¢ c I G W

v q c ¤ y" q c ¤ y `P€#`PU v g ( S F EU v ) I G q" q c ¤ y E¥©%D`©U b u1PE€y£8P`f`¥E§££E8²Y©F b ©1P%ƒ£H©`e¥£H£¥¥£P8¥–£©%#8©y v U e ) ™ W F2 V I ¢ c I G W c e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v e R ¡ &2 V I ¢ c I G W c U T ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ) I G q" q c ¤ v U T ¥–F v ) I G q" q c ¤ y H¥P%#`PU b ¥ui¥H¥£7Y£8©©`f8¥E{¥8²y£F b Pi©fy¥H¥£¥¥£H£¥¥£P8¥–£©%#8©y v U e T ¢ ¡ V ( ( W2 V I ¢ c I G W c e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v e R ¡ &2 T ¢ ¡ V ( ( W U T ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ) I G q" q c ¤ v U T ¥–F b b b b b b

3.7

With this, you can register as many users you want (well, to be honest, as many users your computer supports;-) )

Isabelle HURBAIN

Do not forget to edit your ApplicationResources.properties file to match the messages:

Get information about a user

19
v g ( S ¥£EF b b b b b b b b v ( W ¢ y £U v U ¢ ¡ W F" q c ¤ ¥£Ep#`©y b v¢¥GEU b U@`¥4y¥£E¥£P8¥Eu£¥¥8²Y©F b ¥E¤ b q ¤ ¤ v e ¢ q ¤ G ¤2 T ¢ ¡ V ( ( W e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ¢ q ¤ G v ( W ¢ y v ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S q ££¥£€DEU vrei¡%yQ¥(©GT££H¥¥E¥£P`£r©E¥4uE£q b U R &2 ¢ I U ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ¤ ™ ¢ T G ( ¢ T" ™ G t S v e ¡ T ¢ ¡ V e ¢ c W I ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S PH¥£Hz88dE–£¥E€D£q v ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S q ££¥£€DEU v UriR&%y¥¢I©UT£¢£¡HV¥¥E¥£P`£r©E¥4uE£q b e ¡ 2 Q ( G T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ¤ ™ ¢ T G ( ¢ T" ™ G t S v e c T Pu©£S ’ PE£P88dE–£¥E€D£q I G t S q e ¢ c W I ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S v e ¢ V T ¤ e ¢ q W ™ S q q c ¤ y" q c ¤ P`£e`£¥r8©€#8©y

Jakarta Struts - A beginner’s tutorial

C Æ Æ e c T 1£D©¥S ’ ££8¥££8zd#©E¥¥45¥£71P£S ’ £H¥¥£H€«–¥Hd©£S ’ £H¥¥8— h ¢ G  T ¢ ¡ V e Ä ¢ ¤ V F G T ¤ ¤ ¨ ¤ ¢ t2 ¤ ¡ ¢ V à ¢ TÄ Æ c T h ¢ G  T ¢ ¡ —Ä T ¢ ¡ V c T h ¢ G  T ¢ ¡ Ç}¥E££E£¥¥£¥Èy¥E££E©¥rH¥¤ I S G ¤ R ¢ ™ Q  ¤ ¢ q X T ¢ j r I S G ¤ R ¢ ™ Q   “ ¡ h S T y Æ ¢ ¡ I S R ¡ ¢ T ¢ ¡ I S R ¡ ¢ ~ ¤ ¢ q X T ¢ j R ¤ ¤ HHE©{£EE©¥¥¥¥¥¥E¥¥ r ¤ ¡ ¢ V à ¢ T ¤ ¡ ¢ V à ¢ ~ ¤ ¢ q X T ¢ j R ¤ ¤ 5£¥d©¥¥¥¥¥¥¥¥E¥¥ r c T S | c T ƒ©£}©£S ’ ¥E©E¨ I S G ¤ ™ r t I G R R W c t I G R R W  I S G ¤ ™ #©H©`u©H¥E©E¨ Ä c T S | T ¢ R ( T W h T …©£¥r£¥£S ’ ©¥EEiE¥R I S G ¤ ™ ¨ ™ G q F V Ç I S G ¤ ™ ¨ ¡ ( I ¢ ¤ Q ¢ I S G ¤ ™ ¨ h ¢ G  T ¢ ¡ — ¡ ¡ W q ™ ™ G q F V –¥EhE£¥¥{¥E££H£¥£8i¥uER Cuƒf2YI©S¥GE7DE¥¥f5PE%5£–EEeG ¤ ™ W2 ¡ ¤ V T ¤ ¡2 ¢ y ™ W R W2 t T S ¤ T S R c C ƒ2 R ¤ ¤ y2 ¤ ¢ q X T ¢ ¡2 Q W X W & ¤ T S R c ufy¥€5¥¥££fy££–EEeG C ƒ2 ¤ ¢ q X T ¢ ¡2 Q W X W & ¤ T S R c uf5¥¥££fy££–EEeG C T S ¤ ™ ¢ 2 q G ¤ V2 W X W & ¤ T S R c 5£©€#£E€#£–EEeG C I S G ¤ R ¢ ™ Q   “2 S G2 W X W & ¤ T S R c y©¥E£E¥%#¥f#£–EEeG

You can easily see that the form-bean information will be the same as the UserForm, except that the validation is not correct anymore. So you can inherit the new bean (UserViewForm.java) from UserForm and override the validate method so that it returns null.

Isabelle HURBAIN

The associated Action gets the corresponding user in the users vector and sets it as a request attribute.

É É C q q V I I T V ¤ ¢ Dr¥¥£T Ç Æ ¤ ¡ ¢ V à ¢ T ¤ ¡ ¢ V à ¢ ~ ¤ ¢ q X T ¢ j R ¤ ¤  r t I G R R W c t I G R R W  I S G ¤ ™ ¨Ä ¢ ¤ W ( G q W X ¡ T S T T  I S G ¤ ™ ¨ ™ G q F V h8©¥¥d¥¥¥£¥¥£E¥#©H©`rPHE¥¥E7#£@E©¥EEiE¥R Ç c T tP£S ’ ¥8˜E¥t©£S ’ ££H£¥£8i¥uER T ¢ ¡ — ¡ ( I ¢ ¤ Q ¢ c T h ¢ G  T ¢ ¡ — ¡ ¡ W q ™ ™ G q F V C5©EH£HE4Y©¥E7DE¥¥f5PE%5£–EEeG t I G R R W  I S G ¤ ™ ¨2 I S G ¤ ™ W2 ¡ ¤ V T ¤ ¡2 ¢ y ™ W R W2 t T S ¤ T S R c C ¡ T S T T  I S G ¤ ™ ¨2 I S G ¤ ™ W2 ¡ ¤ V T ¤ ¡2 ¢ y ™ W R W2 t T S ¤ T S R c DE£HE4Y©¥E7DE¥¥f5PE%5£–EEeG C ¤ ¡ ¢ V à ¢ ~ ¤ ¢ q X T ¢ j R ¤ ¤ 2 R ¤ ¤ y2 ¤ ¢ q X T ¢ ¡2 Q W X W & ¤ T S R c y¥¥£¥¥¥€y¥€5¥¥££fy££–EEeG

v q c ¤ y" q c ¤ y `P€#`PU v g ( S F EU v c T S |" q c ¤ y `©4#`PU v U ¤ G c F V ¡ " q c ¤ {i©©f#8©y v ¤ ™ ¢ q ¢ ¡" q c ¤ y ¥£f#`PU vU@e`¢8cdWx¥¤¡Wqde`g¤T£¢RST9©q¢FWq@e`¢8cdWx¥¤¡Wqde`g¤T£¢RSTR˜eH¡ET£¢£Hz`¥E¥¥–H©¥E7#`©y b ¡ V e I S G ¤ ™ ¢ q q S ™ ¡ I S G ¤ R S" q c ¤ v de`¢8dE£P8¥r££f#8©y c W x ¤ ¡ W q e g ¤ T ¢ R S T R ¤ ™ ¢ q ¢ ¡ " q c ¤ v e T ¢ ¡ V h ¢ G X U e I S G ¤ ™ W c T S |" q c ¤ Pi¥H£££P`©¥EtP£4#8©y b b b b b b b

Add parameters to struts-config.xml:

20
v U e R ¡ &2 T ¢ ¡ V ( ( W U e ¤ V R I ui©%y££H¥P`©G e ¤ ¡ ¢ V à ¢ T e ¢ R S ™ i£¥e`¥¥¡ e c T D©£S ’ ££H£¥£Hz88dEI h ¢ G  T ¢ ¡ V e ¢ c W eiI¥E££H£¥£8´8¥¤ S G ¤ ™ ¨ h ¢ G  T ¢ ¡ — e ¢ R g e T ¢ ¡ V h ¢ G X U e y ¤ W i¥H£££P`££ER I S G ¤ ™ ©¥EW b v U e R ¡ &2 S | I G h ¢ G X U e y ¤ W u1P%#©H£££P`££ER e S | I G h ¢ G X e ¢ c W I 8©H£Ee`HdEÓE¥£| b ( T W h T S v U e c T hD©£S ’ ££H£¥£8´8¥¤ h ¢ G  T ¢ ¡ — e ¢ R g e c T DP£S ’ £H¥¥£Ez`8eEr©`©£| b h ¢ G  T ¢ ¡ V e ¢ c W I I W ¢ F • c T S

Jakarta Struts - A beginner’s tutorial

v q c ¤ y" q c ¤ y `P€#`PU v g ( S F EU vE¥I©q%Dq`c©¤U b Uue1P™EF€yV£¢8IPW`fe`g¥E)§££E8²Y©F b ©1P%ƒ£H©`e¥£H£¥¥£P8¥–£©%#8©y ) G " y v ) W 2 I c G c ¢ ¢ t W ¡ ¡ ¢ c" I W ¢ v e R ¡ &2 V I ¢ c I G W c U T ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ) I G q" q c ¤ v U T ¥–F vH)¥I©G%"#`c©yU b UueiT¥£¡H¥h££GE42yI£8cPG`cf`¥¢E)§¢t££E8²Y©F b ©1P%Y¥£E££E¥£H£¥¥£P8¥–£©%#8©y q q ¤ v ¢ V ¢ X V ¢ I W e g W ¡ ¡ ¢ c" I W ¢ v e R ¡ &2 T ¢ ¡ V h ¢ G X U T ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ) I G q" q c ¤ v ¢ q F W ¤ ¥E©£U v T ¤ £U v ¤ (£U b ¥@1£Hz8¥ºi¥Hz`HdEuE€YF b ¤ b v U e y ¤ T G F e g ¤ T ¢ R S T R e T ¢ ¡ V e ¢ c W I ¢ ¤ G T h" I W ¢ v ( v(£U b u8££H€5¥£H¥¥£P`£Er£¥H²YF b ¤ b ¤ v U e ¢ ¤ W ( y ¤ T G F2 T ¢ ¡ V ( ( W e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ( v T ¤ v T ¤ £U v¤£U b v¥U@8£`Pd8¥ºi¥Hz`HdEuE€YF b ¤ b ( e q G W c ¢ e g ¤ T ¢ R S T R e T ¢ ¡ V e ¢ c W I ¢ ¤ G T h" I W ¢ v ( v(£U b ¥@8£`P75¥£H¥¥£P`£Er£¥H²YF b ¤ b ¤ v U e q G W c ¢2 T ¢ ¡ V ( ( W e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ( v T ¤ v ¢ q F W ¥¤ v U e ¢ c W x ¤ ¡ W q e g ¤ T ¢ R S T R e T ¢ ¡ V e ¢ c W I ¢ ¤ G T h" I W ¢ @`8d¥d`£ºi££Hz`¥8dE@©E€Y©F v U e ¢ c W x ¤ ¡ T G | e g ¤ T ¢ R S T R e T ¢ ¡ V e ¢ c W I ¢ ¤ G T h" I W ¢ @`8dE£EEz`£ºi££Hz`¥8dE@©E€Y©F v U e ¢ q ¤ G ¤ e g ¤ T ¢ R S T R e T ¢ ¡ V e ¢ c W I ¢ ¤ G T h" I W ¢ @8¥Ez`£ºi££Hz`¥8dE@©E€Y©F b b b b b b b b b b b b b b v U ¡ T S T T ¢" q c ¤ rE£¥7#8©y b v g ( S ¥£EF b b b b b b b b b b b b b b v ( W ¢ y £U v U ¢ ¡ W F" q c ¤ E€#8©y v q ¤ ¤ ¢¥©GE£U b ¥u`¥E4y££H£Ee`£Er£¥H²YF b ¥E¤ v U e ¢ q ¤ G ¤2 T ¢ ¡ V h ¢ G X e g ¢ ) ¢ t W ¡ ¡ ¢ c" I W ¢ v ¢ q ¤ G v ( W ¢ y v ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S q ££¥£€DEU vrei&%yQ¥(©GT££H¥¥E¥£P`£r©E¥4uE£q b U R ¡ 2 ¢ I U ¢ ¡ V T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ¤ ™ ¢ T G ( ¢ T" ™ G t S v e ¡ T ¢ ¡ V e ¢ c W I ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S PH¥£Hz88dE–£¥E€D£q v ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S q ££¥£€DEU v UriR%2y¥¢I©UT£¢£¡HV¥¥E¥£P`£r©E¥4uE£q b e ¡ & Q ( G T ¢ ¤ ¡ G t ¢ T U e | ¢ T y ¤ ™ ¢ T G ( ¢ T" ™ G t S v e c T Pu©£S ’ PE£P88dE–£¥E€D£q I G t S q e ¢ c W I ¤ I ¢ ¡ ¢ T 9 ¤ S I" ™ G t S v e ¢ V T ¤ e ¢ q W ™ S q q c ¤ y" q c ¤ P`£e`£¥r8©€#8©y vBpH™ESP`©E|¥Rpi£qE4D™¥E£S£¡E¥©U ’ ©E w d`£E@©£@B e G t q e Q G ¢ T e ( ¤2 G t q • ¤ V T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ vie88©¤z`E|¥Rhi¥q4Dq`©y£¡EV¥©U ’ ©E w d`£E@©£@B B q c y e Q G ¢ T e ( ¤2 c ¤ • ¤ T ¤ ¡ x “ • s U e G T V F G q t W ¤ „ v Bi1I©Fz`QE¥¢hei¥¤4yW•£E¤V¥T©U ’ ©E w d`£E@©£@B e W ¢ e G | T R ( q 2 I ¢ F ¡ ¤ ¡ x “ • s U e G T V F G q t W ¤ „ v B e W X W & e ¢ t W V t I W q ¢ t W R „ i8P`£E¥©r£hB

C Æ e S | I G h ¢ G X e Ä ( T W h T 1£`©H££ed5E¥£S ’ ©E45©H©`h¥¥£T ( I G |2 t I G R R W c I T V ¤ ¢ É É C Æ Æ G Ä ¤ ¢ t2 ¡ T ¢ ¡ V r e T ¢ ¡ V e Ä ¢ ¤ V F G T ¤ ¤ ¨ ¤ ¢ ¡2 ¤ ¡ ¢ V à ¢ 1’#¥4D¥£H“``¥£Hze#PE¥¥£fy©¥¥T Ç Æ Æ Æ Ä ¢ c W x ¤ ¡ W ” ¤ ¢ t2 T ¢ ¡ VÄ ¡ q W V à ¢2 Æ Ä ¢ c W x ¤ ¡ W ” ¤ ¢ t2 Æ Æ Æ G Ä ¤ ¢ t2 ¡ T ¢ ¡ VÄ Æ c T £¥z58dE©E¥€y£H4E¥71z58dE££E¥€’#¥4D¥£H4i©£S ’ ¥£8€¥5G T ¢ ¡ —Ä Ä Ä | Ç Æ Õ Õ G C Æ Ä ¢ Ô G ¡2 ¡ T ¢ ¡ pH£“1zDfuE¥£HV b Ò#{©’5| G C 6 G ¤ I G Ä T S C Æ Æ e ¡ T ¢ ¡ V e Ä ¢ ¤ V F G T ¤ ¤ ¨ ¤ ¢ t2 Æ Ä ¤ Q ¢ ¤ I S ! ¤ ¢ q X T ¢ j ¤ ¢ t2 Æ Ä I S G ¡ ¡ ¢ j ¤ ¢ t2 ¤ ¡ ¢ V à ¢ TÄ Æ T S ¤ ™ ¢ Ä 1¥£8E¥£8´d#PE4zy££¥£7zy©¥£¥45¥£718€¦hE¥Hu£ ¡ T ¢ ¡ V T S ¤ ™ ¢

As you can see, the information is displayed thanks to ”property” of the bean (in any scope) with the name ”name”. Do not forget to edit the ApplicationResources.properties file:
É É

‰ „ † ‡  ˆ Œ € ‚ „ Ž D58ƒ#’¤8#Y8…

Isabelle HURBAIN

The last page I will describe here is the page that displays the information about the user (viewinfo.jsp):

21 Jakarta Struts - A beginner’s tutorial tags; these tags print the property

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

I S G ¤ W c T S | I G T ¢ ¡ — ¥E`©£©§¥£Hp–¥E€y¥£H¥££EX ¢ q ¤ G ¤2 T ¢ ¡ V h ¢ G

Reload the application... you now have finished this tutorial!

22

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

Chapter 4

Conclusion
I hope that this tutorial will have given you a good overview of Struts. You can get more information (particularly about the tags library) on Struts website (http://jakarta.apache.org/struts/). I’ve tried to make this tutorial as clear and bug-free as I could. Also keep in mind that Struts, Eclipse and the J2EE specification keeps on evolving every day. However, if you have any question or if you find a bug, please do not hesitate to email me at isabelle.hurbain@free.fr.

23

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

Appendix A

GNU Free Documentation License
Version 1.1, March 2000 Copyright (C) 2000 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.

0. Preamble
The purpose of this License is to make a manual, textbook, or other written document ”free” in the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others. This License is a kind of ”copyleft”, which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General Public License, which is a copyleft license designed for free software. We have designed this License in order to use it for manuals for free software, because free software needs free documentation: a free program should come with manuals providing the same freedoms that the software does. But this License is not limited to software manuals; it can be used for any textual work, regardless of subject matter or whether it is published as a printed book. We recommend this License principally for works whose purpose is instruction or reference.

1. Applicability and definitions
This License applies to any manual or other work that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License. The ”Document”, below, refers to any such manual or work. Any member of the public is a licensee, and is addressed as ”you”. A ”Modified Version” of the Document means any work containing the Document or a portion of it, either copied verbatim, or with modifications and/or translated into another language. A ”Secondary Section” is a named appendix or a front-matter section of the Document that deals exclusively with the relationship of the publishers or authors of the Document to the Document’s overall subject (or to related matters) and contains nothing that could fall directly within that overall subject. (For example, if the Document is in part a textbook of mathematics, a Secondary Section may not explain any mathematics.) The relationship could be a matter of historical connection with the subject or with related matters, or of legal, commercial, philosophical, ethical or political position regarding them. The ”Invariant Sections” are certain Secondary Sections whose titles are designated, as being those of Invariant Sections, in the notice that says that the Document is released under this License. The ”Cover Texts” are certain short passages of text that are listed, as Front- Cover Texts or Back-Cover Texts, in the notice that says that the Document is released under this License. A ”Transparent” copy of the Document means a machine-readable copy, represented in a format whose specification is available to the general public, whose contents can be viewed and edited directly and straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file format whose markup has been designed to thwart or discourage subsequent modification by readers is not Transparent. A copy that is not ”Transparent” is called ”Opaque”.

24

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standard-conforming simple HTML designed for human modification. Opaque formats include PostScript, PDF, proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machine-generated HTML produced by some word processors for output purposes only. The ”Title Page” means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, ”Title Page” means the text near the most prominent appearance of the work’s title, preceding the beginning of the body of the text.

2. Verbatim copying
You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this License, the copyright notices, and the license notice saying this License applies to the Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to obstruct or control the reading or further copying of the copies you make or distribute. However, you may accept compensation in exchange for copies. If you distribute a large enough number of copies you must also follow the conditions in section 3. You may also lend copies, under the same conditions stated above, and you may publicly display copies.

3. Copying in quantity
If you publish printed copies of the Document numbering more than 100, and the Document’s license notice requires Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all these Cover Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on the back cover. Both covers must also clearly and legibly identify you as the publisher of these copies. The front cover must present the full title with all words of the title equally prominent and visible. You may add other material on the covers in addition. Copying with changes limited to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be treated as verbatim copying in other respects. If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed (as many as fit reasonably) on the actual cover, and continue the rest onto adjacent pages. If you publish or distribute Opaque copies of the Document numbering more than 100, you must either include a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a publicly-accessible computer-network location containing a complete Transparent copy of the Document, free of added material, which the general network-using public has access to download anonymously at no charge using public-standard network protocols. If you use the latter option, you must take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy (directly or through your agents or retailers) of that edition to the public. It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document.

4. Modifications
You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modified Version under precisely this License, with the Modified Version filling the role of the Document, thus licensing distribution and modification of the Modified Version to whoever possesses a copy of it. In addition, you must do these things in the Modified Version: A. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those of previous versions (which should, if there were any, be listed in the History section of the Document). You may use the same title as a previous version if the original publisher of that version gives permission. B. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modifications in the Modified Version, together with at least five of the principal authors of the Document (all of its principal authors, if it has less than five). C. State on the Title page the name of the publisher of the Modified Version, as the publisher. D. Preserve all the copyright notices of the Document. E. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices.

25

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

F. Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified Version under the terms of this License, in the form shown in the Addendum below. G. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document’s license notice. H. Include an unaltered copy of this License. I. Preserve the section entitled ”History”, and its title, and add to it an item stating at least the title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no section entitled ”History” in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modified Version as stated in the previous sentence. J. Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document, and likewise the network locations given in the Document for previous versions it was based on. These may be placed in the ”History” section. You may omit a network location for a work that was published at least four years before the Document itself, or if the original publisher of the version it refers to gives permission. K. In any section entitled ”Acknowledgements” or ”Dedications”, preserve the section’s title, and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein. L. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles. M. Delete any section entitled ”Endorsements”. Such a section may not be included in the Modified Version. N. Do not retitle any existing section as ”Endorsements” or to conflict in title with any Invariant Section. If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document, you may at your option designate some or all of these sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version’s license notice. These titles must be distinct from any other section titles. You may add a section entitled ”Endorsements”, provided it contains nothing but endorsements of your Modified Version by various parties–for example, statements of peer review or that the text has been approved by an organization as the authoritative definition of a standard. You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover Text and one of Back- Cover Text may be added by (or through arrangements made by) any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one. The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modified Version.

5. Combining documents
You may combine the Document with other documents released under this License, under the terms defined in section 4 above for modified versions, provided that you include in the combination all of the Invariant Sections of all of the original documents, unmodified, and list them all as Invariant Sections of your combined work in its license notice. The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents, make the title of each such section unique by adding at the end of it, in parentheses, the name of the original author or publisher of that section if known, or else a unique number. Make the same adjustment to the section titles in the list of Invariant Sections in the license notice of the combined work. In the combination, you must combine any sections entitled ”History” in the various original documents, forming one section entitled ”History”; likewise combine any sections entitled ”Acknowledgements”, and any sections entitled ”Dedications”. You must delete all sections entitled ”Endorsements.”

6. Collections of documents
You may make a collection consisting of the Document and other documents released under this License, and replace the individual copies of this License in the various documents with a single copy that is included in the collection, provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects. You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy of this License into the extracted document, and follow this License in all other respects regarding verbatim copying of that document.

26

Isabelle HURBAIN

Jakarta Struts - A beginner’s tutorial

7. Aggregation with independent works
A compilation of the Document or its derivatives with other separate and independent documents or works, in or on a volume of a storage or distribution medium, does not as a whole count as a Modified Version of the Document, provided no compilation copyright is claimed for the compilation. Such a compilation is called an ”aggregate”, and this License does not apply to the other self-contained works thus compiled with the Document, on account of their being thus compiled, if they are not themselves derivative works of the Document. If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document is less than one quarter of the entire aggregate, the Document’s Cover Texts may be placed on covers that surround only the Document within the aggregate. Otherwise they must appear on covers around the whole aggregate.

8. Translation
Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License provided that you also include the original English version of this License. In case of a disagreement between the translation and the original English version of this License, the original English version will prevail.

9. Termination
You may not copy, modify, sublicense, or distribute the Document except as expressly provided for under this License. Any other attempt to copy, modify, sublicense or distribute the Document is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance.

10. Future revisions of this license
The Free Software Foundation may publish new, revised versions of the GNU Free Documentation License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. See http://www.gnu.org/copyleft/. Each version of the License is given a distinguishing version number. If the Document specifies that a particular numbered version of this License ”or any later version” applies to it, you have the option of following the terms and conditions either of that specified version or of any later version that has been published (not as a draft) by the Free Software Foundation. If the Document does not specify a version number of this License, you may choose any version ever published (not as a draft) by the Free Software Foundation.

27