From jbosstools-commits at lists.jboss.org Tue Mar 10 10:39:18 2009 Content-Type: multipart/mixed; boundary="===============6639721800265897305==" MIME-Version: 1.0 From: jbosstools-commits at lists.jboss.org To: jbosstools-commits at lists.jboss.org Subject: [jbosstools-commits] JBoss Tools SVN: r14147 - branches/jbosstools-3.0.x/struts/docs/struts_tools_tutorial/en. Date: Tue, 10 Mar 2009 10:39:18 -0400 Message-ID: --===============6639721800265897305== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: ochikvina Date: 2009-03-10 10:39:17 -0400 (Tue, 10 Mar 2009) New Revision: 14147 Added: branches/jbosstools-3.0.x/struts/docs/struts_tools_tutorial/en/master_ou= tput.xml Log: https://jira.jboss.org/jira/browse/JBDS-655 - adding master_output.xml; Added: branches/jbosstools-3.0.x/struts/docs/struts_tools_tutorial/en/maste= r_output.xml =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- branches/jbosstools-3.0.x/struts/docs/struts_tools_tutorial/en/master_o= utput.xml (rev 0) +++ branches/jbosstools-3.0.x/struts/docs/struts_tools_tutorial/en/master_o= utput.xml 2009-03-10 14:39:17 UTC (rev 14147) @@ -0,0 +1,1340 @@ + + + + + + + + + + + + + + + + + + + + = + +]> + = + + Struts Tools Tutorial + = + AnatolyFedosik + OlgaChikvina + SvetlanaMukhina<= email>smukhina(a)exadel.com + = + April 2008 + + 2007 + 2009 + JBoss, a div= ision of Red Hat + + + Version: 3.0.0.GA + + + + + + PDF version + + + + + = + = + = + = + = + + + + + JBoss Tools + Eclipse + Java + JBoss + + + Introduction + The following chapters describe how to deal with classic/old style= of Struts development. We + recommend users to use JBoss Seam to + simplify development, but until then you can read about classical Stru= ts usage here. + + We are going to show you how to create a simple + Struts application + using the JBoss Tools. The completed application will ask = a user to enter + a name and click a button. The resulting new page will display the fam= iliar message, + "Hello <name>!" + This document will show you how to create such an application from= the beginning, along the + way demonstrating some of the powerful features of JBoss Tools. With t= he help of our tutorial you will design the + application, generate stub code for the application, fill in the stub = coding, compile the + application, and finally run it all from inside the Eclipse. + = +
+ Other relevant resources on the topic + = + All JBoss Developer Studio/JBoss Tools documentation you can fin= d here. + The latest documentation builds are available here. + = +
+ = +
+ + = + + + + + JBoss Tools + Struts + Struts Application + + + = + Creating a Simple Struts Application + = + Firstly, we assume that you have already launched Eclipse with J= Boss Tools installed and + also that the Web Development perspect= ive is the current perspective. (If not, make it + active by selecting + Window > Open Perspective > = Other > Web + Development + from the menu bar.) + = +
+ Starting Up + We are first going to create a new project for the applicati= on. + + + Go to the menu bar and select + File > New > Project= ... + . + + + Select + JBoss Tools Web > Strut= s > Struts Project + in the New Project dialog box + + + Click + Next + + + + Enter "StrutsHello" as the project name + + + Leave everything else as is, and click + Next + + + + Click + Next + again + + + Make sure that + struts-bean.tld + , + struts-html.tld + , and + struts-logic.tld + are checked in the list of included tag librar= ies and then click + Finish + + + + A "StrutsHello" node should appear in the upper-left Package + Explorer view. + + + Click the plus sign next to + StrutsHello + to reveal the child nodes + + + Click the plus sign next to + WebContent + under StrutsHello + + + Click the plus sign next to + WEB-INF + under WebContent + + + Then, double-click on the + struts-config.xml + node to display a diagram of the Struts applic= ation configuration + file in the editing area + + + At this point, its empty except for the background grid line= s. +
+ = +
+ + Creating the Application Components + Now, we will design the application by creating the individu= al components as + placeholders first. (We dont have to complete all of the detai= ls inside the components + until afterwards.) +
+ Creating JSP Page Placeholders + Next, let's create and place two JSP pages. We will not = write any code + for the files, but only create them as placeholders so tha= t we can create links to + them in the diagram. We will write the code a little bit l= ater. +
+ Creating the Page Placeholders + + + Bring the Web Projects view to the front of = the Package Explorer view + by selecting the + Web Projects + tab next to that tab. + + + Right-click the + StrutsHello &g= t; WEB-ROOT (WebContent) + folder in the Web Projects view an= d select + New > Folde= r... + + + + Enter + pages + for a folder name and click + Finish + + + + + We will keep our presentation files in this = folder + + + + Right-click the pages folder and select + New > Fila = > JSP... + + + + + For Name type in + inputname + (the JSP extension will be automat= ically added to the file), + for Template select + StrutsForm + and then click on the + Finish + button + + + Right-click the pages folder again and selec= t + New > File = > JSP... + + + + For Name type in + greeting + , for Template leave as Blank, and = then click on the + Finish + button + + + Just leave these files as is for now. +
+
+ Placing the Page Placeholders + Lets now place the two pages just created on the dia= gram. + + + Click on the + struts-config.= xml + tab in the Editing area to bring t= he diagram to the + front + + + Click on the + inputname.jsp<= /property> + page in the Web Projects view, dra= g it onto the diagram, and + drop it + + Click on the + greeting.jsp + page in the Web Projects view, drag it= onto the diagram, and + drop it to the right of the + /pages/inputname.j= sp + icon with some extra space + + You should now have two JSP pages in the diagram. +
+
+
+ Creating an Action Mappings + Using a context menu on the diagram, we are next going t= o create an Action + mapping. + + + Right-click between the two icons and select + Add > Action + + + + Enter the following values: + + + + Action values + + + + path + /greeting + + + name + GetNameForm + + + scope + request + + + type + sample.GreetingAction + + + validate + <leave blank> + + + +
+ ("GetNameForm" is the name for a form bean that we will = create + later.) + + + Click + Finish + + + + + The + /greeting + action should appear in four places, in the di= agram, under the + action-mappings node, under the struts-config.xml node in = Tree view, in Web Projects + view and in the Outline view. Also, note the asterisk to t= he right of the name, + struts-config.xml, in the Outline view showing that the fi= le has been changed, but + not saved to disk. +
+
+ Creating a Link + Let's now create a link from the inputname.jsp page to t= he action. + + + On the left-hand side of the diagram in the colu= mn of icons, click on this + icon: + + +
+ Create New Connection Icon + + + + + +
+ + + In the connect-the-components mode you are in no= w, click on the + /pages/inputname.j= sp + icon in the diagram and then click on = the + /greeting + action + + + A link will be created from the page to the action. +
+
+ Creating a Forward + Next, we are going to create a forward for the action. + + + On the left-hand side of the diagram in the colu= mn of icons, click on this + icon, again: + + +
+ Create New Connection Icon + + + + + +
+ + + Click on the + /greeting + action icon in the diagram and then cl= ick on the + pages/greeting.jsp= + icon + + + That's it. A link will be drawn from the actions= new greeting + forward to the greeting.jsp JSP page. Note that th= e forwards name will be + set based on the name of the target JSP file name.= If you don't + like it, you can easily change it + + + Select the + Tree + tab at the bottom of the editor window= (between Diagram and + Source) + + + Expand the + struts-config.xml/= action-mappings//greeting + node and then select the greeting forw= ard + + + In the Properties Editor to the right, change th= e text to + "sayHello" in the Name field + + + Select the + Diagram + tab at the bottom of the editor window= and see how the diagram + is also updated to reflect the change + + +
+
+ Creating a Global Forward + One last component that we need to create in the diagram= is a global forward. + + + Somewhere in the top-left corner of diagram, rig= ht-click and select + Add > Global F= orward... + + + + Enter + getName + in the Name field + + + Select the + Change... + button for Path + + + In the Edit Path window, switch to the + Pages + tab + + + Expand the + StrutsHello > W= EB-ROOT (WebContent) > pages + node and then select the inputname.jsp= page + + + Click + Ok + . + + + Leave the rest of the fields blank and click + OK + + + + A forward object now appears on the diagram and also in = the global-forwards folder + in the Outline view. + + + Tidy up the diagram, by clicking and dragging ar= ound each icon, so that + the diagram looks something like this: + + +
+ Diagram View + + + + + +
+
+
+ Creating a Form Bean + One last thing that we need to do is to create a form be= an. + + + Switch to the Tree viewer in the editor for the = struts-config.xml file, by + selecting the + Tree + tab at the bottom of the editor window= + + + Right-click + struts-config.xml = > form-beans + and select Create Form Bean + + + Enter + GetNameForm + in the name field and + sample.GetNameForm= + for type + + + Click + Finish + + + + To save your changes to struts-config.xml, selec= t + File > Save + from the menu bar + + + Note the disappearance of the asterisk next to the name,= struts-config.xml. +
+
+
+ = + + + + + JBoss Tools + Struts + Struts Application + + + = + Generating Stub Coding + We are done with designing the application through the diagr= am. Now we need to write + code for the action component. We also need to write an action= class for the + /greeting + mapping along with a FormBean. To aid in the codin= g phase, JBoss Developer + Studio can generate Java class stubs for all of the components= shown in the diagram. + + + Switch back to the diagram, by selecting the + Diagram + tab at the bottom of the editor window + + + Right-click a blank space in the diagram and select = + Generate Java Code + + + + Leave everything as is in the dialog box and click <= emphasis> + Generate + + + + You should see a screen that says: + Generated classes: 2 + Actions: 1 + Form beans: 1 + + + Click + Finish + + + + The Java files will be generated in a + JavaSource > sample + folder that you can see in the Package Explorer view u= nder the + "StrutsHello" node. One Action stub and one FormBean stub will= have + been generated. + + = + + + + + JBoss Tools + Struts + Struts Application + + + + Coding the Various Files + We will now code both the Java stub classes just generated, = the JSP files left in as + placeholders from previous steps, and a new start JSP page we = will have to create. +
+ Java Stub Classes + + + To finish the two Java classes, switch to the + Package Explorer + view and expand the + JavaSource > sa= mple + folder + + +
+ GetNameForm.java + + + Double-click + GetNameForm.ja= va + for editing + + + + You are looking at a Java stub class that wa= s generated by JBoss + Developer Studio. Now we are going to edit the= file + + + + Add the following attributes at the beginnin= g of the class: + + + + + + + Inside the reset method, delete the TO DO an= d throw lines and + add: + + + + + + + Inside the validate method, delete the TO DO= and throw lines and + add: + + + + + + + Right-click and select + Source > Ge= nerate Getters and Setters... + from the context menu + + + In the dialog box, check the check boxes for= name and greetName, + select First method for Insertion point, and c= lick on the + OK + button + + + The final GetNameForm.java file should look like thi= s: + + + + + Save the file + + +
+
+ GreetingAction.java + + + Open GreetingAction.java for editing + + + Inside the execute method, delete the TO DO = lines and add the + following: + + + + + + The final version of GreetingAction.java should look= like this: + + + + + Save the file + + + Close the editors for the two Java files + + + The last thing left to do is to code the JSP files w= hose editors should still + be open from having been created as placeholders. +
+
+
+ JSP Pages +
+ inputname.jsp + In this page, the user will enter any name and click= the + submit + button. Then, the greeting action will be = called through the form. + + + Click on the + inputname.jsp<= /property> + tab in the Editing area to bring i= ts editor forward + + + In the Web Projects view, expand + StrutsHello &g= t; Configuration > default + > struts-config.xml > action-map= pings + and select + /greeting + + + + Drag it and drop it between the quotes for t= he + "action" + attribute to the + <html:form&= gt; + element in the Source pane of the = editor + + + Then type this text on a new line just below= this line: + + + + + Select the + Visual + pane of the editor + + + Then, in the JBoss Tools Palette, expand the= + Struts Form + library, select + text + , and drag it onto the box + + Note: + By default there are only four group= s on the JBoss Tools + Palette. If you wish to make some grou= p visible click the + Show/H= ide + button on the top of palet= te and in the prompted + dialog check the group (or groups) you= want to be shown. + + + + +
+ JBoss Tools Palette + + + + + +
+ + + In the Insert Tag dialog box, type in name f= or property and select + Finish + + + + In the StrutsForm library in the JBoss Tools= Palette, select + submit + , and drag it to right after the te= xt box in the Visual pane + of the editor + + + Right-click the + submit + button and select + <html:submi= t> + Attributes from the context menu + + + In the Attributes dialog box, select the + value + field and type in "Say Hello!" for= its + value + + + After tidying the page source, the Editor window for= the file should look + something like this: +
+ Editor Window + + + + + +
+
+
+ greeting.jsp + Next, we will fill in the result page. + + + Click on the + greeting.jsp + tab in the Editing area to bring i= ts editor forward + + + Type in the following code: + + + + + Greeting + + +

+

+ + +]]>
+ + To complete editing of this file, we will use macros= from the JBoss Tools + Palette. This palette is a view that should be availab= le to the right of the + editing area. + + + Click on the + Struts Common<= /property> + folder in the JBoss Tools Palette = to open it + + + Position the cursor at the beginning of the = greeting.jsp file in the + Source pane and then click on bean taglib in t= he JBoss Tools + Palette + + + This will insert the following line at the top of th= e file: + +]]> + + + Click on the + Struts Bean + folder in the JBoss Tools Palette = to open it + + + Position the cursor inside the + <p> + element + + + Click on write in the JBoss Tools Palette + + + Type in "GetNameForm" for the + "name" + attribute and add a + "property" + attribute with "greetName" as its + value + + + The editor should now look like this: +
+ Editor Window + + + + + +
+
+
+ index.jsp + Finally, we will need to create and edit an index.js= p page. This page will use + a Struts forward to simply redirect us to the getName = global forward. + + + In the Web Projects view, right-click on + StrutsHello &g= t; WEB-ROOT(WebContent) + node and select + New > File = > JSP.. + + + + Type index for Name and click on the + Finish + button + + + On the JBoss Tools Palette, select the + Struts Common<= /property> + folder of macros by clicking on it= in the palette + + + Click on the logic taglib icon + + + Press the + Enter + key in the editor to go to the nex= t line + + + Back on the palette, select the + Struts Logic + folder of macros + + + Click on redirect + + + Delete the ending tag, put a forward slash i= n front of the closing + angle bracket, and type "forward=3DgetName" in= front of + the slash + + + The finished code for the page is shown below: + + +]]> + + + To save all the edits to files, select + File>Save A= ll + from the menu bar + + +
+
+
+ = + + + + + JBoss Tools + Struts + Struts Application + + + = + Compiling the Classes and Running the Application + = + As this is the Eclipse environment, no explicit compilation = step is required. By + default, Eclipse compiles as you go. + + Thus at this point everything is ready for running our appli= cation without having to leave JBoss + Developer Studio by using the JBoss Application Server engine = that comes with the JBoss + Developer Studio. For controlling JBoss AS within JBoss Develo= per Studio, there is JBoss + Server view. +
+ JBoss Server Panel + + + + + +
+ + + Start up JBoss AS by clicking on the icon in JBoss S= erver view. (If JBoss AS + is already running, stop it by clicking on the red ico= n and then start it again. + Remember, the Struts run-time requires restarting the = servlet engine when any + changes have been made.) + + + After the messages in the Console tabbed view stop s= crolling, JBoss AS is + available. At this point, right-click on the getName g= lobal forward in the + struts-config.xml diagram view and select Run on Serve= r. + + + The browser should appear with the application started. +
+ = + + + + + JBoss Developer Studio + Struts + Struts Validation + + +Struts Validation Examples + +Validation of input is an imp= ortant part of any Web application. All Apache Jakarta frameworks, = +including Struts, can use a common Jakarta Validation Framework for stream= lining this aspect of Web = +application development. The Validation Framework allows you to define val= idation rules and then apply these rules on the client-side or the server-s= ide. + + JBoss Developer Studio makes using the Validation Framework in Struts even easier with the help of a= specialized editor for the XML files that controls validation in a project= . In this document, we'll show you how this all works by creating some simp= le client-side validation and server-side validation examples. + +
+ +Starting Point +The example assumes that you have already created our sample "Struts= Hello" application from the = +Getting Started Guide for Creating a Struts Application. You should have t= he JBoss Developer Studio perspective + open on this StrutsHello project. +
+ +
+ +Defining the Validation Rule +In these steps you will set up the validation that can be used for e= ither client-side or = +server side validation. You need to enable validation as a part of the pro= ject, define an error message, and tie it into an appropriate part of the a= pplication. + + +Right-click on a "plug-ins" node under the StrutsHello > Configuration > default > str= uts-config.xml node in the Web Projects view and sele= ct Create Special Plugin > Validat= ors from the context menu +Further down in the Web Projects view, right-click on the = +StrutsHello > ResourceBundles node and select N= ew > Properties File...from the context menu +In the dialog box, click on the Browse...button next to the Folder field,= expand the + JavaSource folder in this next dialog box, select the sample subfolder= , and click on the OK button + Back in the first dialog box, type in "applResources" = for the Name field and click on the F= inish button +Right-click on a newly created file and select <= property moreinfo=3D"none">Add > Default Error Messages + from the context menu + +Drag up the sample.applResources icon until you can drop i= t on the resources folder under struts-config.xml +Select File > Sav= e All from the menu bar +Select validation.xml under the StrutsHello > Validation node and doub= le-click it to open it with the JBoss Tools XML Editor +Here you must create a Formset. +In the validation.xml file editor click the button Create Formset on the = panel Formsets<= /para> +In the dialog Add Fo= rmset fill the fields Language and Country or just leave them empty to create a def= ault formset. Click OK + +
+ Create Formset + = + + + + +
+ +Expand the "form-beans" node under the StrutsHello > Configuration > default > struts-= config.xml node. Then, drag the form bean "GetNameFor= m" and drop it onto a formset in the XML Editor +In the Validation Editor, expand the formset node, right-c= lick GetNameForm, and select Create F= ield... from the context menu +Enter a name for Property in the dialog box. A new propert= y will be created: + +
+ New Property Is Added + = + + + + +
+ +In the Properties view for the name field to the right of = the "tree" for the validation.xml file, click on the Change...button next to the Depends e= ntry field +In the displayed double list, select required from the left list and then= click Add + Click Ok +Right-click name and select Add Arg... from the context menu +In the Add Arg dialog box, click on the Change...button next to the Key field + In the Key dialog box that appears now, click on the <= emphasis>Add button + Enter "name.required" in the Name field, and enter a p= erson's name in the Value field + Click Finish, then Ok, and then Ok<= /emphasis> again +Select File > Sav= e All from the menu bar + +
+ +
+ +Client-Side Validation +Client-side validation uses a= scripting language (like JavaScript) running in the client browser to actu= ally do the validation. In a Struts = application using the Validation Fra= mework, however, you don't actually have to do any of the script coding. Th= e Validation Framework handles this.= +To see how this works in our application, you'll just need to make a= couple of modifications to one of the JSP files. + +Double-click inputname.jsp under StrutsHello > WEB-ROOT(WebContent) > pages to open it for editing +Find the tag near the top and hit Return to make a new li= ne under it + In the JBoss Tools Palette view to the right, open the= Struts HTML folder and click on the javascript tag +Back in the editor, just in front of the closing slash for= this inserted tag, hit Ctrl+Space and select "formName" from the prompting= menu +Over in the Web Projects view, select GetNameForm under th= e StrutsHello > Configuration >= default > struts-config.xml > form-beans node,= drag it, and drop it between the quotes in the editor +Modify the <html:form> tag by inserting this attribut= e: + + + +The file should now look like this: + + + + Input name + + + + validateGetNameForm(this)"> + + + + + + + +
Input name:
+ + +
+
+ +
+]]>
+ +Select File > Sav= e from the menu bar +Start JBoss Application Server by clicking on its icon (a = right-pointing arrow) in the toolbar +Click the Run icon or right click your project folder and = select Run As > Run on Server + +
+ Run Icon + = + + + + +
+In the browser window, click on the "Say Hel= lo!" button without having entered any name in the form +A JavaScript error message should be displayed in an alert box. +
+
+ +Server Side Validation +Server side validation does the validation inside the application on= the server. In a Struts application using the Validation Framework, you st= ill don't have to do any of the actual validation coding. The Validation Fr= amework handles this. You will though have to make a few changes to the JSP= file you modified for client-side validation along with a change to an act= ion and a few changes to the form bean class. +
+ +
+ +Editing the JSP File + +Reopen inputname.jsp for editing +Delete the "onsubmit" attribute in the <html:form> e= lement that you put in for client-side validation + +Add an <html:errors> tag after the <html:form> t= ag + +The JSP file should now look like this: + + + + + Input name + + + + + + + + + + + +
Input name:
+ + +
+
+ + +
+]]>
+ +
+ +
+ +Editing the Action + +In the Web Projects view, expand the node under the StrutsHello > Configuration > default= > struts-config.xml > action-mappings node, ri= ght-click the /greeting action, and then select Prop= erties...from the context menu + + In the Edit Properties window, insert the cursor into = the value column for the input property and click on the ... button + + In the dialog box, make sure the Pages tab is selected= , select StrutsHello > WEB-ROOT(We= bContent) > pages > inputname.jsp, click the Ok button, and th= en click on the Close button + +
+
+ + +Editing the Form Bean + +Right-click the /gre= eting action again and select Open Form-bean Source t= o open the GetNameForm.java file for editing +Change the class that it extends from: org.apache.struts.action.ActionForm to org.apache.struts.validator.Vali= datorForm +Comment out a validate method + +The file should now look like this: + + + +Select File > Sav= e All from the menu bar +Reload the application into JBoss AS by clicking on the "C= hange Time Stamp" icon (a finger pointing with a little star) in the toolba= r +Run the application +In the browser window, click on the "Say Hello!" button wi= thout having entered any name in the form + +The error message should appear in a refreshed version of the form.<= /para> +
+ +
+ = + + + + + JBoss Tools + Struts + Struts Application + + + + Other Relevant Resources on the topic + + Our reference: Struts Tools Reference Guide + Apache Struts: Struts T= echnology + Struts 2: Apache St= ruts 2 + Get Started: Struts Getting + Started + Struts on IBM: Struts - + An open-source MVC implementation + FAQ: = Struts FAQ + Download: Release of Apache + Struts + + Thus, this tutorial should help you to execute the hole developm= ent circle for building a + sample Struts-based Web application using JBoss Tools bundle of Eclipse + plugins starting from organizing a new Struts project and ending w= ith the running and + deploying it onto the JBoss Server. + + To find out all the features of JBos= s Tools for working with Struts + refer to our Struts Tools Reference Guide. If you + still have questions you are always welcome on JBoss + Tools Forum. + + + = + = +
--===============6639721800265897305==--