myeclipse struts quick start

Upload: thirumaj

Post on 10-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 MyEclipse Struts Quick Start

    1/25

    Struts Quickstart

    Last Revision:

    Outline

    1. Preface2. Introduction3. 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 Sun JDK 1.4.2, Eclipse 3.1 and MyEclipse 4.0. All screenshots are based upon thedefault user interface settings for Eclipse, MyEclipse Enterprise Workbench, and Windows XP. If you experiencedifficulty with the instruction of this document, please see the User Feedback section for how to provide feedback to thMyEclipse documentation team.

    Back to Top

    2. Introduction

    In this tutorial we will be walking through the development and testing of a simple Struts example application usingMyEclipse Enterprise Workbench. Previous knowledge 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 wfollow the same project goals and outline so you can later compare JSF with Struts, once you've gotten a betterunderstanding of how to use the Struts tools in MyEclipse.

    Back to Top

    3. Requirements

    Below is a list of software used by this guide:

    JDK 1.4+ (Sun or IBM)

    http://java.sun.com/j2se/downloads/index.html

    http://c/dev/workspaces/myeclipse3.1-production-build/com.genuitec.myeclipse.doc/html/quickstarts/jsf/index.htmlhttp://java.sun.com/j2se/downloads/index.htmlhttp://java.sun.com/j2se/downloads/index.htmlhttp://www.myeclipseide.com/index.phphttp://c/dev/workspaces/myeclipse3.1-production-build/com.genuitec.myeclipse.doc/html/quickstarts/jsf/index.htmlhttp://java.sun.com/j2se/downloads/index.html
  • 8/8/2019 MyEclipse Struts Quick Start

    2/25

    Eclipse 3.1 SDK

    http://www.eclipse.org/downloads/index.php

    MyEclipse 4.1

    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 Name is "myeclipse" and the Password is "myeclipse" as well.

    Note: After installing the JDK and restarting your computer, install Eclipse, MyEclipse, and Tomcat. For instructionson installing MyEclipse, please see the Installation Quickstart. After all the software has been installed, you'll need tosetup the Tomcat 5 connector in MyEclipse as detailed in theApplication Server Quickstartbefore you can deploy anexecute the example application.

    Back to Top

    4. New Project Setup & Structure

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

    Figure 1 -Web Project Wizard Dialog

    http://www.eclipse.org/downloads/index.phphttp://www.eclipse.org/downloads/index.phphttp://www.myeclipseide.com/ContentExpress-display-ceid-10.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.axint.net/apache/jakarta/tomcat-5/v5.0.28/bin/jakarta-tomcat-5.0.28.exehttp://c/dev/workspaces/myeclipse3.1-production-build/com.genuitec.myeclipse.doc/html/quickstarts/install/index.htmlhttp://c/dev/workspaces/myeclipse3.1-production-build/com.genuitec.myeclipse.doc/html/quickstarts/appservers/index.htmlhttp://www.eclipse.org/downloads/index.phphttp://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://c/dev/workspaces/myeclipse3.1-production-build/com.genuitec.myeclipse.doc/html/quickstarts/install/index.htmlhttp://c/dev/workspaces/myeclipse3.1-production-build/com.genuitec.myeclipse.doc/html/quickstarts/appservers/index.html
  • 8/8/2019 MyEclipse Struts Quick Start

    3/25

    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 ofour project in the Package Explorer View, and selecting MyEclipse > Add Struts Capabilities, as shown in Figure 3

  • 8/8/2019 MyEclipse Struts Quick Start

    4/25

    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 changethe Base package for new classes to reflect your desired location. In Figure 4 below, we will simply leave the defauvalues.

  • 8/8/2019 MyEclipse Struts Quick Start

    5/25

    Figure 4 - Configuring Struts Capabilities

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

  • 8/8/2019 MyEclipse Struts Quick Start

    6/25

    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.

    Back to Top

    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 logscreen. As a result, we will only need 2 JSP pages, one to prompt the user to login and the other to indicate that loginwas successful. We will call these pages userLogin.jsp and userLoginSuccess.jsp , respectively. For simplicity, if theis an authorization error during the login attempt, we will redirect the user back to the userLogin.jsp page and displayan 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 the pieces will fit together. The easiest way to stub out the flow is to graphicallycreate it using the Struts Editor's graphical design mode. The Struts Editor provides a pallet of drag-and-drop toolsthat enable you to begin building your application by quickly replicating the page flow from your design mockups. Theflow 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 toaccess it and use it in the next section..

  • 8/8/2019 MyEclipse Struts Quick Start

    7/25

    Figure 6 - Example Application Flow

    From this design layout, we can see that the application will start by displaying the userLogin.jsp page. The login pagwill call the userLogin action to perform the login operation. If we have any validation errors or problems, the userLogaction will forward the user back to the userLogin.jsp page. However, if the login was successful, the application willforward to the userLoginSuccess.jsp page.

    Back to Top

    5.1 Components of a Struts Project

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

    JSPs Actions

    ActionForwards*

    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 hascompleted. We are not referring to custom implementations of the ActionForward class, although these are possibleand 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 ActionFormimplementations to wrap page values.

  • 8/8/2019 MyEclipse Struts Quick Start

    8/25

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

    Method #1: Use theFile > New > Other... > J2EE > Struts 1.1 (or 1.0) menu by selecting a Struts wizard as seenbelow in Figure 7:

    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 ofthese wizards are more complex than others. For example, in the Struts Action wizard shown below in Figure 8, youcan see comprehensive coverage of all the features supported by a Struts Action:

  • 8/8/2019 MyEclipse Struts Quick Start

    9/25

    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 activediting context. From the Outline View, you can right-click on any of the root level nodes to activate a wizard to creanew components of that type, or to edit existing ones using the wizards. Figure 9 displays an example of using thesecontext wizards.

  • 8/8/2019 MyEclipse Struts Quick Start

    10/25

    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 allowyou to create an entire flow. The wizards are linked together to seamlessly reuse common values and minimizemanual re-entry.

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

    Figure 10 - Accessing the Struts Designer

  • 8/8/2019 MyEclipse Struts Quick Start

    11/25

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

    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 createthe different portions of our login demo application, as shown in the next section.

    Back to Top

    5.2 Building the Application

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

    We will start by creating ouruserLoginSuccess.jsp page first. This seems backwards by creating the last page first, buwe are doing it this way so we can use the New Form, Action and JSPwizard to create the first JSP page along withthe related Action and ActionForm.

    Let's create the userLoginSuccess.jsp JSP page from our designer view by using the "JSP" palette tool by clicking itfirst then clicking our canvas. Follow Figure 12 below for guidance:

  • 8/8/2019 MyEclipse Struts Quick Start

    12/25

    Figure 12 - Create a JSP Using the Designer

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

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

  • 8/8/2019 MyEclipse Struts Quick Start

    13/25

    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 screenshotbelow we have clicked back to the designer to show you what your application is starting to look like, don't getconfused if this is not the flow you see when you create the JSP page.

    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. Thecompleted source code for the page is shown in Figure 14a, below.

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

    userLoginSuccess.jsp

    My Struts 'userLoginSuccess.jsp' ending page

  • 8/8/2019 MyEclipse Struts Quick Start

    14/25

    Hello , you successfully logged in!

    Figure 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 variable userName that is stored in the request scope of our application. So, in our actionthat we will create later, we need to place an attribute by the name ofuserName in the request scope.

    Now we still need to create the userLogin.jsp page, the ActionForm and the Action. This may sound like a lot of workbut MyEclipse can streamline this considerably by way of the New Form and New Form, Action and JSPwizards.

    When creating the userLogin.jsp page we need to consider the fields we want to display in this page and map thosefields to the respective ActionForm. The form will store the values and ensure they get passed to the proper Action.Both of the MyEclipse wizards mentioned above, when generating the Form, offer the 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 fieldsalready in it and ready to use. In this situation since we also want to create an Action to process the login, we will usethe New Form, Action and JSPwizard instead of just the New Form wizard.

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

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

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

  • 8/8/2019 MyEclipse Struts Quick Start

    15/25

    Figure 16 - New Form Wizard

    Now we need to add two form properties: userName andpassword. When adding the password field, choosepassword for the JSP input type field as shown in Figure 17.

    Figure 17 - Add Properties to the Form

  • 8/8/2019 MyEclipse Struts Quick Start

    16/25

    Figure 18 - Form Properties

    Before selecting Next, be sure to click the JSPtab to tell the wizard that you wish MyEclipse to generate a skeletonJSP page having a form with these values in it. 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 thisdemo application we are going to place all the JSPs in the webroot.

    Figure 19 - Enable JSP Page Generation for Form

  • 8/8/2019 MyEclipse Struts Quick Start

    17/25

    Lastly we need to click on the Methods tab to be sure to uncheck all the methods that the wizard can auto-generate foyou in the new Form. Figure 20 shows this configuration.

    Note: For the purposes of keeping this demo simple we won't generate custom reset or validate methods, but it isgenerally a good idea to make use of these methods when coding your own application.

    Figure 20 - Disable Method Generation

    After you click Next, you will be directed to the New Action wizard where you will notice that most all of the values arealready filled in for you. Here is another place that MyEclipse saves you time, by tying together the Form you justcreated with the new Action. You are free to make any changes you want, but most likely the only thing you need to

    worry about now (certainly in this demo application) is to fill out the Forwards that this Action has available to it. Figure21 gives you a screenshot of the wizard.

  • 8/8/2019 MyEclipse Struts Quick Start

    18/25

    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 Forwards for this action, clicking finish will allow MyEclipse to create all the resourcesand update the struts-config.xmlfile (and Designer) with all the new information. Figure 23 displays the updated layouand structure of the application.

  • 8/8/2019 MyEclipse Struts Quick Start

    19/25

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

    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 quickly jump to any resource from the design by double clicking on it, as shown inFigure 24:

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

    When you open the UserLoginAction.java file for the first time, the generated code for ourexecute method will look lik

  • 8/8/2019 MyEclipse Struts Quick Start

    20/25

    Figure 24a.

    UserLoginAction.java

    public ActionForward execute(

    ActionMapping mapping,

    ActionForm form,

    HttpServletRequest request,

    HttpServletResponse response) {

    UserLoginForm userLoginForm = (UserLoginForm) form;

    throw new UnsupportedOperationException("Generated method 'execute(...)' not implemented.");

    }

    Figure 24a - Generated execute Method

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

    UserLoginAction.java

    public ActionForward execute(

    ActionMapping mapping,ActionForm form,

    HttpServletRequest request,

    HttpServletResponse response) {

    UserLoginForm userLoginForm = (UserLoginForm) form;

    if(userLoginForm.getUserName().equals("myeclipse") &&

    userLoginForm.getPassword().equals("myeclipse"))

    {

    request.setAttribute("userName", userLoginForm.getUserName());

    return mapping.findForward("success");

    }

    return mapping.findForward("failure");

    }

    Figure 24a - Corrected execute Method

    Here we are doing something very simple, just checking if the userName andpasswordvalues are both "myeclipse". they are, we store the userName in the request scope and return the success forward, so ouruserLoginSuccess.jsppage can display a personalized message. Otherwise, something went wrong and we return the failure forward. In areal application, you would typically add an ActionMessages or ActionErrors collection back to the request scopebefore returning a failure forward, in order to explain what happened.

    Back to Top

    6. Running the Demo

    Now that the application is complete, we can deploy it to Tomcat 5 by opening the Manage Deployments Dialog andsetting up a new deployment for this project. You can open the management dialog, shown in Figure 25, by either righclicking on your project in the Package Explorer view and selecting MyEclipse > Add and Remove ProjectDeployments, 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. Ifyou have not and need help doing that, please see theApplication Server Quickstart Guide.

    http://c/dev/workspaces/myeclipse3.1-production-build/com.genuitec.myeclipse.doc/html/quickstarts/appservers/index.htmlhttp://c/dev/workspaces/myeclipse3.1-production-build/com.genuitec.myeclipse.doc/html/quickstarts/appservers/index.html
  • 8/8/2019 MyEclipse Struts Quick Start

    21/25

    Figure 25 - Create a New Deployment

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

  • 8/8/2019 MyEclipse Struts Quick Start

    22/25

    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 Tomcatstartup so that we can check for two things:1) Tomcat deployed our web application correctly2) Tomcat started up correctly

  • 8/8/2019 MyEclipse Struts Quick Start

    23/25

    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 OpenView dialog that is accessed by selecting Window > Show View > Other... then selecting the MyEclipse Web BrowseView, as shown in Figure 29.

    Figure 29 -Opening the Web Browser View

    In the browser's address bar, enterhttp://localhost:8080/StrutsLoginDemo/userLogin.jspto activate the exampleapplication, as shown in Figure 30.

    http://localhost:8080/JSFLoginDemo/userLogin.faceshttp://localhost:8080/JSFLoginDemo/userLogin.faces
  • 8/8/2019 MyEclipse Struts Quick Start

    24/25

    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 showus ouruserLoginSuccess.jsp page that our Action correctly forwarded us to.

    Figure 31 - Successful Login

    Back to Top

    7. Summary

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

    This concludes your introduction to Struts. Additional Quickstart documents are available that introduce working withWeb Projects, editing, application server configuration, enterprise application projects and database development. Fomore information visit the MyEclipse Quickstart library .

    http://myeclipseide.com/ContentExpress-display-ceid-67.html#quickstartshttp://myeclipseide.com/ContentExpress-display-ceid-67.html#quickstarts
  • 8/8/2019 MyEclipse Struts Quick Start

    25/25

    Back to Top

    8. User Feedback

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

    Back to Top

    Copyright 2004 - 2005 by Genuitec L.L.C

    http://www.myeclipseide.com/PNphpBB2+file-viewforum-f-6.htmlhttp://www.myeclipseide.com/PNphpBB2+file-viewforum-f-6.htmlhttp://www.myeclipseide.com/PNphpBB2+file-viewforum-f-6.htmlhttp://www.myeclipseide.com/PNphpBB2+file-viewforum-f-6.html