Doku

  • Uploaded by: pavan
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Doku as PDF for free.

More details

  • Words: 16,343
  • Pages: 54
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  D3 D 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" %> <bean:message key="index.title"/><br /> <br /> <br /> <br />

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  @)5 4 $ @)A  6 @)5 4 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 <bean:message key="index.title"/> 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



      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"J ED  D*D     D D ! J F(GE  1       !    !  0      !    D D      $ @    

                -' !>  -     -E !- *#                -' !5-$       "B@  0 $  $            D 0=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 !C 2  =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:
    errors.footer=



 %= 9  & ; 

     A                      D  C +        S    QO  O  QO 7.OT S TS TS% 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" %>

<bean:message key="index.title"/> - Template

1 Template: Navi.jsp

)

  7   

5

10

My Links:

1 Template: Foot.jsp 5


<%=new java.util.Date()%>, by Stephan Wiesner


<            1 Template: BookTemplate.jsp 5

<%@ page language="java" %> <%@ taglib uri='/WEB-INF/struts-template.tld' prefix='template' %>

10

15



20

                              D  D

                 J    $  E 1 Template: CreateBookTemplate.jsp <%@ taglib uri='/WEB-INF/struts-template.tld' prefix='template' %> 5 10

 !      $      

.

  7   

 =   $      !

      $ @       '   &   $           1 $ * < !         J $      E$               !             ,-       D             !                    $    &                    $                A

     A                  !              !         

Annotations #$ %M 9 ,#($ % A   !P * A  $ $ 5E ! 2  $ + ' $   1    *+ $$     )  *+ $$      

+  !P * 16 $  * ',  

/

Chapter 8. Beans to XML 5 9'6    C      ,;P(.   L             $            $     !       !   &    !%,-          $     #      B  

 &M3  '   ! $   !"              9'62   9'6; $      ,;P(.      9'6 F.8G   C        A $      !         # C @ $                      $     #  9'6 H     9'6   &            0 $   #  $   $) ,     

 $ 7" *   !    "@ ! "   C   0 D   @ !  C D                 L $   !         !  $    D !         <    ,-     " C@       J        #  E+ 1 BookSerialize.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" %> <%@page import="java.beans.XMLEncoder, java.beans.XMLDecoder, java.io.*"%> <bean:message key="index.title"/>

Book Serialization



20 <%

25

30

Book book = new Book(); book.setTitle("A great Book about Struts"); book.setPages(100); out.println("<member>First Title: " + book.getTitle() + ""); try { XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream( new FileOutputStream("/Sample.xml"))); encoder.writeObject( book ); encoder.close(); }catch(Exception ex) { out.println(ex); } book.setTitle("A great Book about Struts, Second Edition"); out.println("<member>Second Title: " + book.getTitle() + "");

35

40

45

try { XMLDecoder decoder = new XMLDecoder( new BufferedInputStream( new FileInputStream("/Sample.xml"))); book = (Book) decoder.readObject( ); decoder.close(); }catch(Exception ex) { out.println("

" + ex + "

"); } out.println("<member>Third Title: " + book.getTitle() + ""); %>


4

  8 "   9'6

 A & )  B " 8 "           &      A                 9'6  6       + <   <      &        <         +        L * $           +X     +X  &      $        $  $ @ , ! #                           3         L

/ "0 +'    $"C  $ &2 0 9 "      3     D"'  ) $5?) $ 2$?777ED 5"  7 +   "0 +            &    A 0        A       , ! 0-          0  +                            A ? $     +     C "   9'6 F.8G       +                      "   !  !  *      ! $                   D   "  1 $       ,- $                  $  #      !      $           L ; L                 !       DJED           ,- ; D      L        &$           D  1   $   "@ !  D  ! A     $    L            JE                $ !      $      A

   60-01              "@ !  ; D      $            J   <%%9-  !       $    :8    E <  L 7

  8 "   9'6

           !        'B F.8G                     $    !      60-01   <$               !      A           $       $        $             ,-                     !  H      ,-$    #     ,-      !                 $   #                            !           

             + out.println("<member>File:" + this.getClass().getResource( "Sample.xml" ).toString() + ""); Will print (on my system): File:file:/C:/tomcat/webapps/strutsShop/WEB-INF/classes/Sample.xml.

0            + config.getServletContext().getRealPath(".");

   !           $       ! + +X X X  X

         A       !%      "  9'6 $    #  C     ,;P(.                 $       #       

/ "0 '  &2 0 9 "       !   "  3 , '    "  .'   ) $ 2$ ,  , "'-

/ "0 C   F) '  ) &2 0 +  D"$ D F)     !  , "     9 "       ) 0 +  $        H ! 

       &      +

<% session.setAttribute("myBook", book); %>

8

Chapter 9. Replacing JSP with XSLT            9'6%96     $     ,-  !     ,;P(. < 4  24               $            9'6  C   5 96       9'6$        ,-     9'6 F.8G% 96 F.8G                                   "@ !      $            #     9'6$    D   !  &            !        9'6 1 XMLOutServlet.java 5

10

15

20

import import import import

java.io.*; javax.servlet.*; javax.servlet.http.*; java.beans.*;

public class XMLOutServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/xml"); XMLEncoder encoder = new XMLEncoder(response.getOutputStream()); Book book = new Book(); book.setPages(100); // will NOT be written! book.addAuthor("Stephan"); book.setTitle("XML Book"); encoder.writeObject( book ); encoder.close(); } }

25

9'6?!   $        C      9'6    #     $              H   ,;P(.    9'62   9'6         

:

  : 3    ,-  96

 G    !  8,  H;

/ "0 +!      "   B&2 0 +!    "    "  H;- 1 !  "  '   6 ! 4 "   "  $" "   $ 0 +    C              ?      $    $                            B H      0 JE      $   <$       K  $   D         96       96   #      9'6 0      $  $ 1'6 '               $    D ! !       !               $       !       96           & # $                 

)

Chapter 10. Introducing AspectJ                              #        &       !    !                  5 0  ?  -   J0?-E  0 ,        +%%   %@ ! F  +%%   %@ ! G         0 , F  +%%  @G              0 , /  !     &     ?    !         &            0 ,L   @               &    6D             !     C                  $                                !      K      D  $      !                $    D            D        0 ,           6@ !       0             !     "$           "@ !             + 1 Aspect Logger.java 5

public aspect Logger { pointcut log(): call(* Book.*(..)) ; before() : log() { System.out.print("Enter " + thisJoinPoint.toLongString() + " Args:'"); Object[] os = thisJoinPoint.getArgs(); for(int i = 0; i < os.length; i++) { System.out.print(os[i].getClass() + "(" + os[i] + ") "); } System.out.println("'\n"); }

10

15

} 20

      -2 E !2  7 ))2 -          3   $    " C@          

/   D     923?9 $           !    $      $    J  E

 0 ,

)(

  (     0 ,

 I 

" '    F

              &A 1 Aspect Timer.java 5

10

15

import java.io.*; import java.util.*; import java.text.*; public aspect Timer { Locale currentLocale = new Locale("DEU", "DEU"); DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale); SimpleDateFormat formatter = new SimpleDateFormat( "ssSSSSSS", currentLocale); int zeit = 0; String maxZeitMethode = ""; int maxZeit = 0; pointcut timer(): call(* Book.*(..)) ;

20 before() : timer() { System.out.print(thisJoinPoint.toLongString() + ":"); zeit = Integer.parseInt( formatter.format( new Date() ) ); }

25

after() : timer() { int zeit2 = Integer.parseInt( formatter.format( new Date() ) ) - zeit; System.out.println( zeit2 ); if( zeit2 > maxZeit) { maxZeitMethode = thisJoinPoint.toLongString() + ":" + zeit2; } }

30

35 pointcut cleanUp(): call(void Book.write()); after() : cleanUp() { System.out.print( "\nMethod with longest execution time:\n" + maxZeitMethode ); }

40 }

     !     !     JE       "@ ! $          C   H            ,        $       !             C 0      "@ !            0      '  J      !             E     

)

  (     0 ,

 I   *F  $ ! ;   0?-            $       ! !                     @

))

Chapter 11. Ant for Compilation and Distribution                  !          &   !               #             5 0       !    

11.1. Installation of ANT ;      !  0< F  +@     G C:\Prog>ant -version Ant version 1.4.1 compiled on October 11 2001 C:\Prog>

2&      J  +X@ X E

       !  +

      '          !     <   D  #! D   #  H           !

11.2. What is ANT 0<  !       H                  $   $    @   $   $  0           9'6         D           $   0<                  6      ,03      6 & !     

          $    D !      !  $     &#         $    H           &            $   

11.3. Configuration of ANT 0<      9'6        &            1 build.xml for ANT 5

10

<project name="MyProject" default="dist" basedir="."> <property name="src" value="WEB-INF/classes/"/> <property name="build" value="build"/> <mkdir dir="${build}"/>

15 <javac srcdir="${src}" destdir="${build}"/> 20

).

  (( 0      ; <mkdir dir="${build}/lib"/> 25

30

<jar jarfile="StrutShop-${DSTAMP}.jar">


  9'6     K  &           0<    $        D#  D$    !  @  &       

11.4. Usage of ANT <           &   D D

   "D ,"' )               $  $   0<  D !   D 60-01         +

<javac srcdir="${src}" destdir="${build}" classpath="WEB-INF/lib/struts.jar" />

        !$    D  $              ( FG0 ,  $           &   $   

<javac srcdir="${src}" destdir="${build}" classpath="WEB-INF/lib/struts.jar" excludes="Logger.java, Timer.java" />

  ! ,03               03 $          ,-        !      &   + 1 build.xml for creation of WAR file

)/

  (( 0      ; <project name="MyProject" default="dist" basedir="."> 5 <property name="src" value="WEB-INF/classes/"/> <property name="build" value="build"/> 10

15

20

25

30

35

40

45

50

<mkdir dir="${build}"/> <javac srcdir="${src}" destdir="${build}" classpath="WEB-INF/lib/struts.jar" excludes="Logger.java, Timer.java" /> <echo message="Starting JAR" /> <mkdir dir="dist"/> <jar jarfile="dist/StrutShop-${DSTAMP}.jar"> <echo message="Starting WAR" /> <war warfile="myapp.war" webxml="WEB-INF/web.xml"> <exclude name="*.xml"/> <exclude name="web.xml"/>

55

<$     J  E               P5CQ?PA( '   3                $      

)4

  (( 0      ;

  :  4

  " "" " $ +C

 0        !    !     "$     $    *?         $      !           '     ,03          

/ "0 ( "  FC &2 0 9 "    2$     FC     "        

       0<          03       

)7

Chapter 12. Conclusions 3      J  E !                    ;!   K      !              ? $              &     #   W'  > ; ! 0                    D           D     D  !    $   

)8

Chapter 13. Large Scale Example 1    &     F  +%%C!   %Y//4%%&  C G    2,"        &         2,"       F  +%% #  %@ ! G             " ;     !     2,"              ,22 !                  $         $        .& J !      .(:E  !           !            @  2,"$                  J         A +#E        $       !  D                         &              !                   

J     E      $        !               $    D                     DD                       #     !       0             #         &       !      $             ;     "               $        !

):

Chapter 14. FAQ             K         !   "      K $           

=+

     " S  T +S%  T

   .&     &    D     "      

0+

=+

  $     !     &   #JE H     $              ;           A 9'6  S  T,  - +S%  T 1  $   D         K +                A     A          A 0$       @   # $     #K      9'6  9  96 -#       A ;      A

0+

=+

H$  !    J &  E 9  &    !   !  

   60-01

' # &         S  T'  +S%  T D       (E ' # &              $            +%%@     %%  %# Z(Z                           

0+

=+

    ;;    -5"6  H2' J          E   0<;06?<2 Q 352 '       & S  T'  +S%  T D       E '    &     QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ @ ! &!@ , 2&  + '       &

0+ =+

H         $       '       @ ! &!    &   @ ! &!  N .

  (. *0=

S  T, +S%  T *    $        # "@ !  1!$             "0 @ !       + "0 @ ! +(+   @ ! &!    & #  @ ! &!  NI F=G        @ ! &!  A 0+

  ! 0-    , !  H  !    $     J!E       $         *   ,03     60-01

.(

Chapter 15. Epilogue 15.1. Technical Background of this Document        9'6      ;;         !   @    5 !  0    F  +%%   G  6M  F  +%%  G$ >      ;" F.8G   $    H$           $   C     D ! 9'6D   @   &   J,2 E      J,2 E      0                 J   E  4  )  , *  $ @'    M'  * 5 ,)$ M) E !    $                  $  "<    D     $ $   

15.2. About the Author                 5 !  0    F  +%%   G  6M $ >  1        0> F  +%% G  6M $                    K   -1-   , !      $  &          !        (::8$ D   &  D J  KE  9'6 96 1  ! !        @     F  +%% G

 % )  " + " H  !         F  +%%   G        #         ([$         $        (4 F..G  ,               Stephan Wiesner Höpenweg 7 21357 Wittorf Germany

.

  (/ 2 

.)

Chapter 16. User Comments 16.1. Mails                    @     '     !#       ?

 $+ 4   $! ,  *  ,  

     4    )     *  $ G1 , * 5 * +  >  4 $  ' ,) *   !    4 $  ! $ )$        ,  '    * B            , $ 4    $ '    * >    C  4  . C $  4 $  2 1 *

,   , * '   )    I 4* ,       > + 4  ,   '" $! )+ 5$ E<+ BPGH GPH4C7 HG7A""" H

, , )   * """ $!+ M 9 , B     $   

 , $ * 1 $! ,  , $ *  )   )$ 1   $  1  111 H)+ 1 5$ ?

 $+ 4 2   $! ,  *  ' $) $ -   - 4 $  *   

 , $ *  3  $ ) !  ,+ 5  HRR 5 *  ?

 ,    3R >2     !, + &  4 $   $    ,    4   $  *  !  , ! ?   4       $  *  !-    $  )$ , $*  4 $ ! *  $     '  $  *  ! C $   4 *  $   

,   """ $    )    )  ) * $  *  ! 4   )   1    ',   , ) " 4     $ 4    $  *  ! , 

 M  $ <+ $  ? ) <) 4  *

) ,     )   .   6 $!8 4   5$  O *  >   4 $     $ E !  E !C     S$ SS   $  * $ 1 *)   5E !2  '  

)  *

)   -5  * C  )  C >  E 

 -    ,   $   / $! ,  , $ B   ,+ ? ) <) :$    ' $ $   ; <

+ $ 4       $ 8 :  $)    ; B   ' ,  , ',  L8+ : 

+  18; ' $   2   ,  ! $ 4 *$ ,     4    $ 4    

,    , * $  )   **+   * $ '    '  )   ) 4 $! 4 

    '  $ =)= $  '  @$  $ 

)    )$  $ *  4  ) )  ,  '    

   $ $ $ * +   $ $  >  #1%  E       ) $! ,  , $ *  ,       E )+ &5    $ 9$ 

16.2. Post Cards

..

  (4 5  

 < " +4 "   067 "   "              0          $    !        !        " 

./

  (4 5  

 < 8  $ 8  6                     - 

.4

  (4 5  

 <  $   

.7

Appendix A. Glossary Glossary ;"

;"  !           $ $     $          ;"        ! ;;   &  >'6 9'6 6 1'6$ ;"  &            >'6%9'6 FG ;"   (        (::(  @  @  1 6    ?D3     $ !               C  $   ; !  >    # (::8$        JE    ? C     0 !           J?0E J    E

2&  '  6  

'  B   2&    6  

9'6                 #       )     '  B   2&    6    )    

.8

Appendix B. Bibliography Bibliography Books F(G M) @ 2   3  ?@#?    1 >   ,   B  (::/ #(#4))4(# 0  #     50  

      ; -  

FG M) @ 1 A   * H'  P'21P  *  >   1 ,   B  0  #  50 

#

F)G M E ! $ M*  D ;"<   6  '  (/4/:#/8#7 ?D3 50 

     $      K    

Weblinks F(G $ &  C@4 *   @ ) 6&C@8    ST

 +%%@ !  %&%@ & % %((% % % &  F  +%%@ !  %&%@ & % %((% % % & GS%T FG 7 N C $ ST  +%%&   F  +%%&  GS%T F)G  

D M  ST O  +%%#(4% ! %@ ! % %@# F  +%%#(4% ! %@ ! % %@#G S%T F.G $  ?  ) ST  +%%@     % F  +%%@     %G S%T F/G $ C $ &! ?  ) ST  +%%@      F  +%%@     G S%T F4G $ C & ?  ) ST  +%%  @ F  +%%  @G S%T F7G &  )R  ST  +%%@ !   C   F  +%%@ !   C  G S%T F8G , P  ?  @) ST S%T

 +%%   %@ !

.:

F  +%%   %@ ! G

Related Documents

Doku
June 2020 6
Doku
May 2020 11
Doku Bosch Einspritzung
October 2019 10
Nf Ianus Doku
October 2019 16
Sasarbasar-doku Ll
July 2019 13

More Documents from ""