Home > Articles > Programming > Java

Java Reference Guide

Hosted by

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Struts Development with MyEclipse

Last updated Mar 14, 2003.

I felt a little guilty writing an article about using NetBeans to build a Struts application without building a counterpart for our Eclipse readers. Because Eclipse has on open plug-in architecture, many open source and commercial vendors have built plug-ins that extend the capabilities of Eclipse. A great resource to find these plug-ins is Eclipse Plug-in Central. The top rated plug-in at the time of this writing is the MyEclipse Enterprise Workbench, which boasts features such as UML Modeling and code generation, IDE integration with Struts, Spring, and Tapestry, deep Hibernate integration, enhanced Web Services support, a visual HTML designer, a JavaServer Faces (JSF) designer, and for the purposes of this article, a Struts Designer. It was the Struts Designer that strongly attracted me to MyEclipse, so I took it out for a test spin and you are reading the results.

MyEclipse Enterprise Workbench 5.0 is not free, but annual subscriptions are (after tax and at the time of this writing) $31.75 for the standard edition and $52.95 for the professional edition, with the professional edition providing UML support, extended database support, a JavaScript debugger, and the Matissee WYSIWYG Swing GUI builder. The price is reasonable, and they offer a 30-day trial so you can preview what you are getting for your money.

In order to follow along with this example you will need to download and install the trial version (or make the purchase if you like what you see.) With MyEclipse installed, the first thing you need to do is create a new Web Project:

Choose File->New->Other and navigate to "MyEclipse" and choose "J2EE Projects" and then "Web Project," shown in Figure 1.

Figure 129

Figure 1. Creating a new MyEclipse Web Project

Enter in a project name, change the context root if you so desire, and press "Finished." In this example I named the project "EclipseStrutsForums," shown in Figure 2.

Figure 130

Figure 2. Creating a new MyEclipse Web Project, page 2

After you have created your project, the package explorer should resemble Figure 3.

Figure 131

Figure 3. New Web Project: package explorer

Right-click on your project name (EclipseStrutsForum in my example) and choose "My Eclipse" and "Add Struts Capabilities," see Figure 4.

Figure 132

Figure 4. Adding Struts capabilities to your web application

This will show the dialog box in Figure 5.

Figure 133

Figure 5. Adding Struts capabilities dialog

Choose Struts Specification "1.2," enter your own package name, and be sure to leave "Install Struts jars" and "Install Struts TLDs" checked. Click "Finish" and the Struts dialog will transform your Package explorer into Figure 6.

Figure 134

Figure 6. New project explorer with Struts capabilities.

There are several ways to configure and build a Struts application using MyEclipse, but because we are focused on productivity, and of course "cool factor," we'll focus on the Struts Designer. Double-click on struts-config.xml to open it. The default mode is "Design," but you can also see the raw XML file by clicking on the "Source" tab on the bottom left of the file. For now, leave it in "Design" mode.

The workflow that the MyEclipse documentation suggests is to create the destination page first, and then create the action for and action bean. So let's create a JSP page: click on the "Add JSP Page" icon (the page icon with a "J" on it on the left of the workspace) and then click on the workspace. This shows the "Create a new JSP page" dialog box, shown in Figure 7.

Figure 135

Figure 7. Create a new JSP page dialog

For this example I'll name the JSP page "loginSuccess.jsp" and use the "Standard JSP using Struts 1.2" template. This creates the JSP shown in Listing 1.

Listing 1. loginSuccess.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
 <head>
  <html:base />
  
  <title>loginSuccess.jsp</title>

  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">  
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->

 </head>
 
 <body>
  This a struts page. <br>
 </body>
</html:html>

In the Struts Designer, you'll notice a new "loginSuccess.jsp" node, shown in Figure 8.

Figure 1360

Figure 8. Struts Designer

Right-click on the design page and choose "New" and then "Form, Action and JSP," shown in Figure 9.

Figure 137

Figure 9. Adding a new Form, Action, and JSP in the Struts Designer

This will display the Struts Form Declaration dialog box, shown in Figure 10.

Figure 138

Figure 10. Struts Form Declaration dialog

Enter a use case name for this action, in my example I named it "login." This will cause the rest of the dialog to be populated, for example, it names the FormBean "LoginForm" to match the use case name "login."

In the form properties tab, click "Add" to add the form properties that you want in your JSP page and in your FormBean. For this example we'll add two: username and password. This is shown in Figure 11.

Figure 139

Figure 11. Building a Struts form

Next, click on the "JSP" tab, enable the "Create JSP form" checkbox, and then either accept the name of the input JSP as "login.jsp" or change it to something more appropriate. I put the JSP page in the root of the web application and left it with the name "/login.jsp," shown in Figure 12.

Figure 140

Figure 12. Configuring an input JSP

Finally, because we're not going to perform any validation in this example, click on the "Methods" tab and disable all FormBean methods. Click "Next."

The next page that is displayed is the Struts Action Declaration page. The default values are almost complete, but you need to define two forwards in the "Forwards" tab:

  • success: forwards to the loginSuccess.jsp page
  • failure: forwards back to the login.jsp page

This is shown in Figure 13.

Figure 141

Figure 13. Configuring forwards

When you are finished, press "Finish" and the Struts Designer will show your new page flows. I rearranged my page for readability (you can drag-and-drop the nodes on the workspace), it is displayed in Figure 14.

Figure 142

Figure 14. Struts Designer application flow

Double-click on the action (the login node in the center of figure 14) and it will take you to the action source code. Add in some business logic to the Action's execute() method; following with the MyEclipse Struts tutorial, I checked for a hard coded set of strings. The action is shown in Listing 2.

Listing 2. LoginAction.java

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.javasrc.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.javasrc.struts.form.LoginForm;

/** 
 * MyEclipse Struts
 * Creation date: 10-30-2006
 * 
 * XDoclet definition:
 * @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request" validate="true"
 * @struts.action-forward name="success" path="/loginSuccess.jsp"
 * @struts.action-forward name="failure" path="/login.jsp"
 */
public class LoginAction extends Action {
  /*
   * Generated Methods
   */

  /** 
   * Method execute
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return ActionForward
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) {
    LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
    if( loginForm.getUsername().equalsIgnoreCase( "informit" ) &&
      loginForm.getPassword().equalsIgnoreCase( "informit" ) )
    {
      return mapping.findForward( "success" );
    }
    return mapping.findForward( "failure" );
  }
}

Now you're ready to build and deploy your Struts application. Was that the easiest Struts application you have ever built?

Right-click on your project and choose "MyEclipse" and then "Add and Remove Project Deployments..." This is shown in Figure 15.

Figure 143

Figure 15. Adding a Project Deployments dialog

This will display the "Manage Deployments" dialog box. Click on "Add" to add a new deployment; this shows the "New Deployment" dialog box. Click on "Edit server connectors..." to configure a deployment (such as telling MyEclipse where the application server is located, startup scripts, etc.) In this example we'll configure a Tomcat 5.x deployment (see Figure 16).

Figure 144

Figure 16. Adding a Tomcat 5.x server deployment

If you find the home directory, it will populate the rest of the dialog box for you. Be sure to choose the "Enabled" radio button to make it available to you as a deployment target. If you successfully complete this process, you will see the successful deployment of your application to Tomcat, shown in Figure 17.

Figure 145

Figure 17. Successful deployment

On the bottom of your Eclipse IDE you will see a set of tabs, including "Problems," "Tasks," "Web Browser," "Console," and "Servers." Click on the "Servers" tab, right-click on your application server, and choose "Run Server." The application server startup information will be displayed in the "Console" tab and your application will be automatically deployed to your application server. And if you want to test your application without leaving your IDE, you can do so by clicking on the "Web Browser" tab and entering the URL:

http://localhost:8080/EclipseStrutsForums/login.jsp

Summary

While this example has been simple and has not exercised many of the features of Struts such as validations, this example has demonstrated how easy it can be to develop Struts applications using MyEclipse. The Struts Designer makes building Struts applications easy, but more importantly it makes managing Struts applications simple. This is but one of dozens of features of MyEclipse, so if productivity can come with a price tag, $50 is a drop in the bucket compared to the time that MyEclipse will save you in building your applications. I give it two thumbs up!