+ All Categories
Home > Documents > MyEclipse Struts 1.x Tutorial

MyEclipse Struts 1.x Tutorial

Date post: 04-Jun-2018
Category:
Upload: venu-kongara
View: 227 times
Download: 0 times
Share this document with a friend

of 19

Transcript
  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    1/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    MyEclipse Struts 1.x Tutorial

    Outline

    1. Preface2. Introduction

    3. Requirements4. New Project Setup & Structure5. Starting a Struts Project

    1. Components of a Struts Project2. Building our Application

    6. Running the Demo7. Summary8. User Feedback

    1. Preface

    This document was written using MyEclipse 6.0 GA. All screenshots are based upon the default user interface settings for Eclipse, MyEclipse EnterpriseWorkbench, and Windows XP. If you experience difficulty with the instruction of this document, please see the User Feedbacksection for how to providefeedback to the MyEclipse documentation team.

    2. Introduction

    In this tutorial we will be walking through the development and testing of a simple Struts example application using MyEclipse Enterprise Workbench. Previoknowledge of Struts and/or MyEclipse is not necessary.

    This tutorial, because of the framework's similarity and feature coverage, will be very similar to the JSF tutorial. We will follow the same project goals andoutline so you can later compare JSF with Struts, once you've gotten a better understanding of how to use the Struts tools in MyEclipse.

    3. Requirements

    Below is a list of software used by this guide:

    Java SE 5 (Sun or IBM)http://java.sun.com/j2se/downloads/index.html

    MyEclipse 6.0 GA (All-In-One Installer)http://www.myeclipseide.com/ContentExpress-display-ceid-10.html

    Tomcat 5.0.x (5.0.28 Preferred, or other compliant Servlet/EJB container)http://www.axint.net/apache/jakarta/tomcat-5/v5.0.28/bin/jakarta-tomcat-5.0.28.exe

    For this demo the User Nameis "myeclipse" and the Passwordis "myeclipse" as well.

    Note: After installing the JDK and restarting your computer, install Eclipse, MyEclipse, and Tomcat. For instructions on installing MyEclipse, please see thenstallation Tutorial. After all the software has been installed, you'll need to setup the Tomcat 5 connector in MyEclipse as detailed in theApplication Server

    Tutorialbefore you can deploy and execute the example application.

    4. New Project Setup & Structure

    To organize our development artifacts, we will need to create a new Web Module Projectin MyEclipse that has Struts Capabilitiesadded to it. You can creatweb project using the wizard located at File > New > Other > MyEclipse > Java Enterprise Projects > Web Project , as shown in Figure 1, below.

    http://www.myeclipseide.com/documentation/quickstarts/jsf/index.htmlhttp://java.sun.com/j2se/downloads/index.htmlhttp://www.myeclipseide.com/ContentExpress-display-ceid-10.htmlhttp://www.axint.net/apache/jakarta/tomcat-5/v5.0.28/bin/jakarta-tomcat-5.0.28.exehttp://www.myeclipseide.com/documentation/quickstarts/install/index.htmlhttp://www.myeclipseide.com/documentation/quickstarts/appservers/index.htmlhttp://www.myeclipseide.com/documentation/quickstarts/appservers/index.htmlhttp://www.myeclipseide.com/documentation/quickstarts/appservers/index.htmlhttp://www.myeclipseide.com/documentation/quickstarts/appservers/index.htmlhttp://www.myeclipseide.com/documentation/quickstarts/install/index.htmlhttp://www.axint.net/apache/jakarta/tomcat-5/v5.0.28/bin/jakarta-tomcat-5.0.28.exehttp://www.myeclipseide.com/ContentExpress-display-ceid-10.htmlhttp://java.sun.com/j2se/downloads/index.htmlhttp://www.myeclipseide.com/documentation/quickstarts/jsf/index.html
  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    2/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 1 - Web Project Wizard Dialog

    Now enter in all the new project information, as shown in Figure 2:

    Figure 2 - Web Project Settings

    Once the Web Project is created, we need to add Struts Capabilities to it. This is done by right clicking on the root of our project in the Package Explorer Viand selecting MyEclipse > Add Struts Capabilities, as shown in Figure 3.

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    3/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 3 - Adding Struts Capabilities to a Web Project

    The default values for the Struts dialog are fine for the purposes of this tutorial. However, you might want to change the Base package for new classesreflect your desired location. In Figure 4 below, we will simply leave the default values.

    Figure 4 - Configuring Struts Capabilities

    After the wizard completes, the project structure will look like that shown in Figure 5.

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    4/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 5 - Project Layout After Configuration

    Now that we are done creating our project, we can begin building our web content, as illustrated in the next section.

    5. Starting a Struts Project

    In this section we are going to focus on creating our example Struts application, which will mimic a simple website login screen. As a result, we will only neJSP pages, one to prompt the user to login and the other to indicate that login was successful. We will call these pages userLogin.jspand userLoginSucces, respectively. For simplicity, if there is an authorization error during the login attempt, we will redirect the user back to the userLogin.jsppage and display error message.

    When beginning a Struts project, it's generally useful to lay out the flow of your application so that the development team will have a better idea how all thpieces will fit together. The easiest way to stub out the flow is to graphically create it using the Struts Editor's graphical design mode. The Struts Editorprovides a pallet of drag-and-drop tools that enable you to begin building your application by quickly replicating the page flow from your design mockups. flow of the the example login application looks like Figure 6 below.

    Note: The screenshot of the application flow was built using the Struts Designer. We will go into detail on how to access it and use it in the next section..

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    5/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 6 - Example Application Flow

    From this design layout, we can see that the application will start by displaying the userLogin.jsppage. The login page will call the userLoginaction to perfthe login operation. If we have any validation errors or problems, the userLoginaction will forward the user back to the userLogin.jsppage. However, if thelogin was successful, the application will forward to the userLoginSuccess.jsppage.

    5.1 Components of a Struts Project

    A typical Struts project will consist of the following categories of development artifacts:

    JSPsActionsActionForwards*ActionForms**

    Struts deployment descriptor: struts-config.xml

    *ActionForwards are entries in the struts-config.xml file defining what path an Action will take when it has completed. We are not referring tocustom implementations of the ActionForward class, although these are possible and supported by the tools, for the advanced user.

    **ActionForms can be replaced by the use of DynaForms if the user wishes not to create concrete ActionForm implementations to wrap page values.

    In MyEclipse creating any or all of these components (except the struts-config.xml file) can be done via 3 different methods:

    Method #1: Use the File > New > Other... > J2EE > Struts 1.1 (or 1.2 or 1.3) menu by selecting a Struts wizard as seen below in Figure 7:

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    6/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 7 - All Available Struts Wizards

    The wizards are straight forward and will prompt you for all values that the given Struts component supports. Some of these wizards are more complex thaothers. For example, in the Struts Action wizard shown below in Figure 8, you can see comprehensive coverage of all the features supported by a Struts Act

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    7/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 8 - New Struts Action Wizard

    Method #2: Use the custom Outline View, available when displaying the Struts Editor's source view panel is the active editing context. From the Outline Vyou can right-click on any of the root level nodes to activate a wizard to create new components of that type, or to edit existing ones using the wizards. F9 displays an example of using these context wizards.

    Figure 9 - Launching Struts Wizards from the Outline View

    It is worth noting from this screenshot that some of actions are actually a series of logically related wizards that allow you to create an entire flow. The wizare linked together to seamlessly reuse common values and minimize manual re-entry.

    Method #3: The Struts Editor's design page, shown in Figure 10, is also a very convenient way of creating Struts artifacts. The designer is accessed byopening the struts-config.xmlfile. At the bottom of the editor you can click the Designtab to bring up the designer.

    Figure 10 - Accessing the Struts Designer

    After switching to the designer you should see a view that looks like Figure 11 below:

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    8/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 11 - Overview of Struts Designer

    Now that we have covered how you go about creating the different components of a Struts project, we can now create the different portions of our login depplication, as shown in the next section.

    5.2 Building the Application

    We will begin the construction of the demo application by first focusing on creating the JSP pages. Since our demo application is going to mimic a login screon a website, we will only need 2 JSP pages: userLogin.jspand userLoginSuccess.jsp. As with most Struts apps, if something goes wrong during the login, will send the user back to the userLogin.jsppage and display an error (which is why we don't need to make a loginUserFailure.jsppage).

    We will start by creating our userLoginSuccess.jsppage first. This seems backwards by creating the last page first, but we are doing it this way so we can uthe New Form, Action and JSPwizard to create the first JSP page along with the related Action and ActionForm.

    Let's create the userLoginSuccess.jspJSP page from our designer view by using the "JSP" palette tool by clicking it first then clicking our canvas. Follow Fig12 below for guidance:

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    9/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 12 - Create a JSP Using the Designer

    After clicking on the canvas, we will be prompted with the familiar New JSPdialog as shown in Figure 13.

    Note: Be sure to select the "Standard JSP using Struts 1.1" template

    Figure 13 - Configure the JSP Wizard

    After clicking Finish, the design view, in Figure 14, will show the newly created page.

    Note:After adding the new JSP page, MyEclipse will open the new JSP page in the JSP editor, in the screenshot below we have clicked back to the designeshow you what your application is starting to look like, don't get confused if this is not the flow you see when you create the JSP page.

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    10/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 14 - Struts Designer showing our JSP page

    All that remains to complete the JSP page is to output a message to tell the user that the login was successful. The completed source code for the page isshown in Figure 14a, below.

    Note: For the purposes of making this guide easier to follow (and the code snippets shorter) the JSP page below does not resemble the default JSP templayou will have once you open the file for the first time, you are free to exactly copy the code we have here or adopt it to the default JSP template code thathave after creating the new JSP file.

    userLoginSuccess.jsp

    My Str ut s ' userLogi nSuccess. j sp' endi ng page

    Hel l o , you successf ul l y l ogged i n!

    igure 14a - Code for userLoginSuccess.jsp

    Notice that this page is very simple. The only thing important that we are doing here is the contents of the tag, printing out the value of the variauserNamethat is stored in the request scope of our application. So, in our action that we will create later, we need to place an attribute by the name ofuserNamein the request scope.

    Now we still need to create the userLogin.jsppage, the ActionForm and the Action. This may sound like a lot of work, but MyEclipse can streamline thisconsiderably by way of the New Formand New Form, Action and JSPwizards.

    When creating the userLogin.jsppage we need to consider the fields we want to display in this page and map those fields to the respective ActionForm. Tform will store the values and ensure they get passed to the proper Action. Both of the MyEclipse wizards mentioned above, when generating the Form, offthe ability to create a JSP page along with the form. This will take all the properties of the Form and generate a JSP page with all those form fields alreadyand ready to use. In this situation since we also want to create an Action to process the login, we will use the New Form, Action and JSPwizard instead of the New Formwizard.

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    11/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    To continue building the application, right click on the white canvas area of the Struts designer, select new and then the New Form, Action and JSPwizard Figure 15:

    Figure 15 - Launch New Form, Action, JSP Wizard from Designer

    You will first be presented with the New Formwizard, since it is the first of a three step wizard. Be sure to enter a use case name so the wizard can fill in gdefault values for you. Figure 16 shows how values will be filled in for you as you enter a use case:

    Figure 16 - New Form Wizard

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    12/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Now we need to add two form properties: userNameand password. When adding the password field, choose password for the JSP input typefield as shin Figure 17.

    Figure 17 - Add Properties to the Form

    Figure 18 - Form Properties

    Before selecting Next, be sure to click the JSPtab to tell the wizard that you wish MyEclipse to generate a skeleton JSP page having a form with these valuit. Figure 19 shows this below:

    Note: The default behavior of the wizard is to place generated JSPs into a "/form" subdirectory, for the purpose of this demo application we are going to pall the JSPs in the webroot.

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    13/19

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    14/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 21 - Struts Action Wizard

    You can specify the ActionForwards by clicking the Forwards tab as shown in Figure 22.

    Figure 22 - Setting up the Action Forwards

    After you are done adding the Forwardsfor this action, clicking finish will allow MyEclipse to create all the resources and update the struts-config.xmlfile (aDesigner) with all the new information. Figure 23 displays the updated layout and structure of the application.

    Note: Some manual layout was done to be able to show all the elements of the diagram clearly in a small screenshot. When you manually layout a diagrayour changes will be preserved for future edits.

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    15/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 23 - Overview of Struts Designer and our Application

    Now that the application flow has been defined, we need to add logic to the Action to handle the "login" procedure for this demo application. We can quickjump to any resource from the design by double clicking on it, as shown in Figure 24:

    Figure 24 - Double-click on a Designer Resource to Open an Editor

    When you open the UserLoginAction.javafile for the first time, the generated code for our executemethod will look like Figure 24a.

    UserLoginAction.java

    publ i c Act i onFor ward execut e(Act i onMappi ng mappi ng,

    Acti onFor m f orm,Ht t pServl etRequest request ,Ht t pServl etResponse response) {UserLogi nForm user Logi nFor m = ( UserLogi nFor m) f orm;t hrow new Unsuppor t edOper at i onExcept i on("Gener ated met hod ' execut e(. . . ) ' not i mpl ement ed. ") ;

    }

    igure 24a - Generated execute Method

    We want to remove the default implementation, which simply throws an exception, and replace it with some simple login logic like that shown in Figure 24b

    UserLoginAction.java

    publ i c Act i onFor ward execut e(Act i onMappi ng mappi ng,Acti onFor m f orm,

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    16/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Ht t pServl etRequest r equest ,Ht t pServl etResponse response) {UserLogi nForm user Logi nFor m = ( UserLogi nFor m) f orm;

    i f ( userLogi nForm. getUserName() . equal s( "myecl i pse") && user Logi nForm. get Password( ) . equal s( "myecl i pse") ){r equest . setAt t r i bute( "userName", userLogi nFor m. getUserName() ) ;r eturn mappi ng. f i ndForward( "success" ) ;

    }

    ret urn mappi ng. f i ndForward(" f ai l ur e") ;}

    igure 24a - Corrected execute Method

    Here we are doing something very simple, just checking if the userNameand passwordvalues are both "myeclipse". If they are, we store the userNamein trequest scope and return the successforward, so our userLoginSuccess.jsppage can display a personalized message. Otherwise, something went wrong anreturn the failureforward. In a real application, you would typically add an ActionMessages or ActionErrors collection back to the request scope before retura failureforward, in order to explain what happened.

    6. Running the Demo

    Now that the application is complete, we can deploy it to Tomcat 5 by opening the Manage Deployments Dialog and setting up a new deployment for thisproject. You can open the management dialog, shown in Figure 25, by either right clicking on your project in the Package Explorer view and selectingMyEclipse > Add and Remove Project Deployments,or by clicking the Deploy J2EE Project to Serverbutton in the toolbar:

    Note: It is assumed that you have already setup the Tomcat 5 (or other) application server connector in MyEclipse. If you have not and need help doing thplease see theApplication Server Tutorial.

    Figure 25 - Create a New Deployment

    Its always a good idea to check the deployment status after deployment has completed to make sure no errors occurred. You can do this by following theinstructions in Figure 26:

    http://www.myeclipseide.com/documentation/quickstarts/appservers/index.htmlhttp://www.myeclipseide.com/documentation/quickstarts/appservers/index.htmlhttp://www.myeclipseide.com/documentation/quickstarts/appservers/index.html
  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    17/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 26 - Confirm Deployment Succeeded

    Finally, we will launch Tomcat using the application server launch button shown in Figure 27.

    Figure 27 - Start Application Server

    When the server starts, its output will be directed into the Console View in Eclipse. Figure 28 shows a typical Tomcat startup so that we can check for two

    things:1)Tomcat deployed our web application correctly2)Tomcat started up correctly

    Figure 28 - Confirm Application and Server started correctly

    Once Tomcat 5 is running, we can test it by opening the MyEclipse Browser View. This can be done from the Open View dialog that is accessed by selectinWindow > Show View > Other... then selecting the MyEclipse Web Browser View, as shown in Figure 29.

  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    18/19

    pse Struts 1.x Tutorial

    /www.myeclipseide.com/documentation/quickstarts/struts/[8/5/2009 12:49:50 PM]

    Figure 29 - Opening the Web Browser View

    In the browser's address bar, enter http://localhost:8080/StrutsLoginDemo/userLogin.jspto activate the example application, as shown in Figure 30.

    Figure 30 - Login to Demo Application

    Here we see that the application is available. We can now enter our userName and password as shown in Figure 31.

    Note: The userName and password for this demo are 'myeclipse' and 'myeclipse' respectively.

    After logging in, the form was validated and the application forwarded us to the successful login page. Figure 31 shows us our userLoginSuccess.jsppage tour Action correctly forwarded us to.

    http://localhost:8080/StrutsLoginDemo/userLogin.jsphttp://localhost:8080/StrutsLoginDemo/userLogin.jsp
  • 8/13/2019 MyEclipse Struts 1.x Tutorial

    19/19

    pse Struts 1.x Tutorial

    igure 31 - Successful Login

    7. Summary

    In this demo we developed a simple Struts application using the Struts tools available in MyEclipse Enterprise Workbench.

    This concludes your introduction to Struts. Additional Tutorial documents are available that introduce working with Web Projects, editing, application serverconfiguration, enterprise application projects and database development. For more information visit the MyEclipse Tutorial library.

    8. User Feedback

    If you have comments or suggestions regarding this document please submit them to the MyEclipse Documentation Forum.

    http://myeclipseide.com/ContentExpress-display-ceid-67.html#quickstartshttp://myeclipseide.com/ContentExpress-display-ceid-67.html#quickstartshttp://www.myeclipseide.com/PNphpBB2+file-viewforum-f-6.htmlhttp://www.myeclipseide.com/PNphpBB2+file-viewforum-f-6.htmlhttp://myeclipseide.com/ContentExpress-display-ceid-67.html#quickstarts

Recommended