Struts Tutorial
by Stephan Wiesner
Struts Tutorial © ! " $ ! % & $ ' ! $ $ ! & !
Table of Contents ( ) * + ,- . + * / * /( * * : / + 0 * (. /) 0 " "* (4 /. 1 ! 2 (7 // 3 + 4 5 6 7 8 " 9'6 : 3 ,- 96 ( 0 , (( 0 ; ((( 0< ). (( 0< ). (() 0< ). ((. 5 0< )/ ( () 6 2& (. *0= (/ 2 (/( " ; . (/ 0 0 . (4 5 (4( ' .. (4 - .. 0 > " "
!
List of Figures ( )( ? * - ) .( < 6; . . ; > 4 /( < 2 ( / 5 @ ( /) A () /. 1 () // " B (/ /4 ? 2 ' (8 /7 2 ' (: 4( $ # ( 7( ? * / 8( 2 C " 7 :( ; " 9'6 ) (( 6 0 , )( ( 0 @, ' )) ((( 0< D )/ (( B$ 03 )7 (/( . (4( ? $ +#E ./ (4 " " .4 (4) 6 - .7
!
Chapter 1. Introduction
$ !! ; ! $ $ 0 F +%%@ G H ! & ,!! $ ,- 9'6 0 ! ! I
F +%% %@!G $ D ! -$ J E$ & D ! ! D $ D ! $ # 1'6 $ ! C * D ! 0 , 0 + * D0 D D3D J ;" E $ # JD @ C$ +#E $ D J @ $ E$ 0 $ D # & DFGD J FGE ! $ 0 (/ FG
!" .& D # 0 $ $ D $
(
Chapter 2. Installation of Struts K $ ! ) J !E A & &
Chapter 3. The First Try: Simple JSP
,- $ @ ! "B@ D D#
First JSP <%@ page language="java" %>
Struts Tutorial: BookView BookView
< ! !$ $ !
)
Chapter 4. Second Try: Struts For Internationalisation
& $ ! $ # K ;
+ > & 2"#<* J & $ E
# ; D $ $ L H H $ ? & ! # , & ! !
Empty struts-config.xml <struts-config>
web.xml
.
. + * #
<web-app> <servlet> <servlet-name>action <servlet-class>org.apache.struts.action.ActionServlet
<param-name>application <param-value>ApplicationResources <param-name>config <param-value>/WEB-INF/struts-config.xml <param-name>debug <param-value>2 <param-name>detail <param-value>2 <param-name>validate <param-value>true 2 <servlet-mapping> <servlet-name>action
*.do <welcome-file-list> <welcome-file>index.jsp <welcome-file>index.html
/WEB-INF/struts-bean.tld /WEB-INF/struts-bean.tld /WEB-INF/struts-html.tld /WEB-INF/struts-html.tld /WEB-INF/struts-logic.tld /WEB-INF/struts-logic.tld /WEB-INF/struts-template.tld
/
. + * # /WEB-INF/struts-template.tld
?$ $ #
BookView.jsp: Introducing Internationalization <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-bean.tld"
prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
BookView
6; 1 ! ; D
! " #$ " $" < ! & > D 2 M D # $ >
4
. + * # 0 @ ! & C ! K$ $ % % ! $ ! & ! & ! @ ! # $ $ ! ; D & & ! L
Annotations #$%&$$'#($% )
$ $ $ * ) $ + -$ ((.( )( / $0$ - $ ! ,"
,
#$%2,3$#($% * 4 $ $ , 15*) 6 ! , 7 * 8 $ * $ $! 2, #$%9 #($% $ $ *
) ' : ! * ; <$ 4 = > 4 = ) $ 00000 000000000 Starting service Internal Services Java Web Services Developer Pack/1.0-ea2 Starting service Java Web Services Developer Pack Java Web Services Developer Pack/1.0-ea2 org.xml.sax.SAXParseException: The processing instruction target matching "[xX][ mM][lL]" is not allowed. at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Er rorHandlerWrapper.java:232) . . . . . org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:203) [ERROR] Digester - Parse Fatal Error at line 2 column 6: The processing instruction target matching "[xX][mM][lL]" is not allowed.
#$%9 #($% ? $+ ' $ $ ) * 4 $
, ' $ ! 4 1 ' , * , , @>A 6 ,' $ $ 1 ,' ' $ * = =8 @$ ) ) ) ' $ ' 6 ))8 $ 4 , )
, $ $ $ '1
$ * #$%5$ B#($% =4 $ '$ $ ) ** ) 4 )) ACDA * $ *
) * $$ , , :; 9 ) $ * 1 , * ' ) $) '$ EAC9 ) $2 @)54 $ @)A 6 @)54 2 F8= $ * =4 $! 4 ! $ $ 4 ) ' 4 ) 4 ! = #$%, 5 #($% C $ ! ' =4 $ ( ' G) $ . * ' ) ! * + 4 $ , $ *
) * , ( ' , 1
1' 2
1
2
1' 2
1)2
1 )))2
1 2
1 2
1 2 2 2= #$% #($% =2 A ) ) * !, = $ * $ C 1 7
. + * # H * $ $ , ) $ , - - $ * $ , ) $ * * 6 ,' * , ))+ * , ) $ D A) $ , , ))8
8
Chapter 5. Struts for Forms J $ E " J"@!E ,- ? * * $ # & !
5.1. First Try at Forms 6D " "@! + 1 Book.java 5 import java.util.Vector; /* A simple book. @author [email protected] 10 */ public class Book { /** The title */ private String title = ""; 15 /** We can have more than one author */ private Vector authors = new Vector(); /** The number of pages the book has */ private int pages = 0; 20 /** Standard constructor. */ public Book() { } 25
/** @param title The new Title */ public void setTitle(String title) { this.title = title; }
30
/** @return The title. */ public String getTitle() { return this.title; }
35
/** @param pages The new number of pages. */ public void setPages(int pages) { this.pages = pages; }
40
/** @return The number of pages. */ public int getPages() { return this.pages; } /** We don't want to work with the Vector here, as it is only a reference we would get! @param author Add another author
45
*/ public void addAuthor(String author) { this.authors.add(author); } 50 /** Pay attention not to use the wrong number. @param position The number of the author to remove. 55
*/
$ J % % E$ 0
6 B D C N $ ! ' $ $ $ ! $ D & # ! I#E
:
/ * public void removeAuthor(int position) { this.authors.remove(position); } /** @return The number of authors the book has. */ public int getNumberOfAuthors() { return this.authors.size(); }
60 }
5 @ " D & <$ 1'6 & # $ J E D H ! $ K J E 2 "B@ +
Title:
H A O$ ! DD " O ? ' L ? " A "@!$ $ D L ! 0 "
% &$ ' ! "
,- 5E!2 1 CreateBook.jsp <%@ 5 <%@ <%@ <%@
page language="java" %> taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
10 15 Create a book
(
/ *
20 Title:
25
0 #+ "0 @! + 1 BookAction.java import javax.servlet.http.*; import org.apache.struts.action.*; 5 /* 10
15
20
25
30
The action for the creation of a book. @author [email protected] */ public final class BookAction extends Action { /** @param mapping The ActionMapping used to select this instance @param form The optional ActionForm bean for this request (if any) @param req The non-HTTP request we are processing @param res The non-HTTP response we are creating @return Return an ActionForward instance describing where and how control should be forwarded, or null if the response has already been completed. */ public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) { System.out.println("Start perform(" + form + ") . . ." ); String title = req.getParameter("title"); Book book = new Book(); book.setTitle( title ); System.out.println("After creation of book: " + book.getTitle() ); req.setAttribute("BOOK", book); return mapping.findForward("bookCreated"); }
35 }
K KJO"??POE
JEL ! 1 !3K-JE $ 2 # & + 1 struts-config.xml including forward and mapping 5 10 <struts-config>
((
/ * 15
20
path="/BookView.jsp"/>
25
type="BookAction" name="bookForm" scope="request" input="/CreateBook.jsp" rel="nofollow">
30
35
' D D"JED D*D DD ! J F(GE 1 ! ! 0 ! D D $ @
-'!> - -E!- *# -'!5-$ "B@ 0 $ $ D0=E!=D + " D*D D"@ D " D# "D
L ; @$ ! ! &CH * , , @!
% ( " )* !
' @ ,03 L ! 3 J ! ! $ ! E ) < "@ " & $ $ D $ L ! $ D < )* ! ! DD L ,- D $
(
/ *
% + ,-
% . " J ! !&E ! " !A D2 0 * D"D # @! 2& + "D &
A <& 0 J D $ E J D # # &E ? $ D ! K
/ "0 , 1&2 0 1 ! 3 " , 1 $ ) !4 , , " 5' ' ! 7 " "$ ()
/ *
5.2. Struts: Introducing the ActionForm 'B 0 * ? & ! D $ 0 * D $ % " &# ! J E ! J#$ # E * $ 0 * $ # @ + ! JE JE # 'B F.8G $ @ " & A $ "
/ "0 +! " 2" "$&2 0 +! " 8,* 3 2" "$- , , 1 " ' ' ' 3 0 + ! ! >5 $ "@! $ ! $ 'B
+ "*@! ! #
! ! " 1! $ >5 !$ L $ 1 + 1 BookForm.java 5
import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.*; /*
10
15
20
ActionForm */ public class BookForm extends ActionForm { private Book book =new Book(); String title = "Ye old Book"; public void setTitle(String title) { book.setTitle(title); } public String getTitle() {
(.
/ * return book.getTitle(); } 25
public void setBook(Book book) { this.book = book; } public Book getBook() { return this.book; } /** * Reset all properties to their default values. * * @param mapping The mapping used to select this instance * @param request The servlet request we are processing */ public void reset(ActionMapping mapping, HttpServletRequest request) { this.book = new Book(); }
30
35
40 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((book.getTitle() == null) || (book.getTitle().length() < 3)) { errors.add("Title", new ActionError("error.book.title")); } return errors; }
45
} 50
JE JE <
+ ! JE 0 * ! # ! 1 ? ! " & ! # & "@! ! # #* 1' 0='!> = , 0=E!> =(% * ! 2 DQ2 D < $ "@ .
%% 8, 9 " " : " . DD +#J
(/
/ *
/ "0 9 " " &2 0 , , ' " " , + ! " " -
"
A "@! " $ 0 * ! D ! $ $
Annotations #$%! #($% : ; , 6*/"8I 1 $ $ =E!= $) =E!= $ '1
) I/ <$ == =E!= $ /// 4 $! $ == ' $ / #$% $#($% $ ' * $ $ I ) $ -- # 1 %J#( 1 % $, *1 $ ) $C $ -- $ 1*) * *) ) 65E!2 8 #$% * #($% * 5 $ *
) 4 $ F+ , ' $ * ) $ E! E!> C$ $ * $ )
* $ E! C , $ $ '+ , $ *
, '* ) ' $ E!> ! ,
*
5.3. A better way to seperate Book and BookForm ! "*@! "@! ! $
! $ D K $ ! 1 "@ + Title:
Pages:
0 $ -'! - DD D L ? ! "*@! $ D ! L ! ! DD "*@! ; R E! 2 $ D $ "*@! + 1 BookForm.java, reduced 5
import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.*;
(4
/ *
/* 10
ActionForm */ public class BookForm extends ActionForm { private Book book = new Book(); public void setBook(Book book) { this.book = book; } public Book getBook() { return this.book; }
15
/** * Reset all properties to their default values. * * @param mapping The mapping used to select this instance * @param request The servlet request we are processing */ public void reset(ActionMapping mapping, HttpServletRequest request) { this.book = new Book(); }
20
25
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((book.getTitle() == null) || (book.getTitle().length() < 3)) { errors.add("Title", new ActionError("error.book.title")); }
30
35
if (book.getPages() < 1) { errors.add("Page", new ActionError("error.book.page")); } return errors; } }
40
Annotations : ; $+ D +
4 . * ) $ '! '! ) $)+ $ ') , 1
$) I E!C : , $ ; , 6= * 6= K * K =8 = 8L ) 0 I)@ 6='! =8L E! '! 0 E!68L '! 6 8L , 6=C* 1 * '! = K '!) 68 8L = = $ $) ='! = H)+ $ A E $ $ *
) $ E!C2 =E!> '* 0 6E!> 8I)C'6='!> =8L= $ * $ $ * ) E! ) $ ' * $
5.4. Handling Invalid Entries ! 2 # & ! action: Populating bean properties from this request action: Validating input form properties action: Validation error(s), redirecting to: /CreateBook.jsp
$ "# *@! $ ! J E ! "0 1!$ D ! ! ? A @ A (7
/ * 0 "@ & ! L H ! C ) L
/ "0 3 "$ &2 0 ; ! " " ' ! "$ " " " "! ! $ , ', " ! " " 3 ! , " 6 " 8! ' !4 ' " 3 8,$ 0 + ! ! >5 $ "@! $ ! $ 'B 1 The changed CreateBook.jsp Create a book
5
10
Title:
Pages:
D # & "0 @! J E ! ! ! # $ ! "B@ L $ $ 1! # $ & "@ $
& , "@ #
(8
/ *
%< & ; 0 1'6 $ @ A @ + S + QOO%T >$ 1 $ D A ?$ A "*@! ! ! DD ! $ @ D <$ D D A * $ ! 0 2 0 K ! $
index.title=Struts Tutorial error.book.title=<member>Error with title of book error.book.page=<member>A book needs at least one page errors.header=Error:
%= 9 & ;
A D C + S QO O QO 7.OT S TSTS%TS T7S% TS T.S% TS% T (:
S T S% T
/ * U S%T S T0 $ C + FG OH , VI +%VI VI + # QO-OF G %VI O S% T S% T
5.5. Recapitualation: Where we are
! K ! " & " K !$ # ! ! ! $ !# ; D ! ! + 9" $ 9*$ 90 $ $ 0 $ $
! ! J @ # ! ! < $ E $ ! A ; ! A A D # $ ! !$ D ! H $ ' ! $ $ &
/ "0 &2" ! 0 " 9 &2 0 ! ! " " ,4 $6 ! "2 " " " ! " " "4 $6 ' " $ " " " , 9 : 2 $ 0 + " $ ! / "0 & 6 " 2$ &2 0 & ! 6 " 2$ 9 " "6$ " $ " >?@9 8,* ? " >?@8,: '* ? + " '" ! " "' @ " ,0 + , +#E
Chapter 6. Using Logic 5 6 ! ! J K E 6 $ ' "B@ + 1 First Iterator, no Struts <% 5
Book books[] = new Book[3]; books[0] = new Book(); books[0].setTitle("Book 0"); books[1] = new Book(); books[1].setTitle("Book 1"); books[2] = new Book(); books[2].setTitle("Book 2"); for(int i = 0; i < 3; i++) { out.println("<member>" + i + ") " + books[i].getTitle() + ""); }
10
15 %>
< 1 4 " 6 $ 1 Iteration, using Struts 5
10
<member> Title =
$ $ K "$ J E$ A & ! # $
! $ K D < # D ! ,- D ,! D$ ! $ ! & $ $ (
4 5 6 H $
Annotations #$%M M'#($% =4 , :$ ; <CM ! ,
6<' $ ' * 4 , $) &@ E8 ? + 4 $! 4 $ ) * , $ ' $ '* * ) ) &@ 4 ' * $ 5 ** $ =M 9AAH+ AAH & , &@ 9AAH"= E ,- * ! N $ * $ '* * $ # )% ) $ $, &@ , ! $ 6' , ) , '
' $ , $ $ 18 4* , * ' $+ 2 ) $ ((2! $)('(
(*$1)$ 4 , ) $ ! $ &- 1
) , )$ ' $ #$%,#($% ? $+ :; 4 ) $)$ $ , $ ' 4 $ $ ) $ $ $ ) $ $ ** $- $ 4 * , ,' -
$) ' 4 * ) $) $ , 4- , $ ,- !) $,
) $ ' , ' $ ) $ = = ,
,- )$ * )
- * + ) , )$ ' $ $ 4* , ! 6 4 8
, $ ' ' $ + ' ) , ) $ 4 $! ,- ' ** ) $ # )% , 2 , O + $ ,
Chapter 7. Templates 5 D O# O ,- # ! # ! ! ,- !$ D W ' D D ,- ! &$ D & ! A ,- $ + 1 Using JSP Include Hello World
<jsp:include page="/copyright.jsp" />
5
10
$ ! $ @ $ A ! $ ! !
0 ! # ! ! ! ! -$ ! , -1- ,-
"$ ! $ & $ D <$ D D + 1 Template: Head.jsp 5
10
15
<%@ <%@ <%@ <%@
page language="java" %> taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
- Template
1 Template: Navi.jsp
)
7
5
10
My Links:
1 Template: Foot.jsp 5
<%=new java.util.Date()%>, by Stephan Wiesner