- Overview
-
Table of Contents
- J2SE: Standard Java
- Java Windows NT Services
- Apache Velocity
- Advanced J2SE
- Bytecode Instrumentation
- Dynamic Languages and the JVM
- J2SE 1.5.0: "Tiger"
- Java SE 6
- Java 7
- Core Computer Science Principles in Java (Data Structures)
- Annotations
- Java Generics
- Java New I/O
- Java Sound
- Java Applets
- JavaFX
- Java SE Threading
- Resource Management Using Semaphores
- Java Atomic Operations
- JavaTemplate Pages
- Executing Templates with the JtpExecutor
- Java Cryptography Extensions (JCE)
- Java Database Connectivity (JDBC) API
- Jakarta Commons - Net Class Library
- Jakarta Commons HttpClient
- Apache POI
- Regular Expressions
- JavaMail
- Cool Tools
- Building an Really Simple Syndication (RSS) Java App
- Embedding JavaScript in Java with Rhino
- Logging with Log4J
- Inside Swing
- Swing Components
- SwingX
- Swing Styled Documents
- Web Rendering in Java Swing Applications
- Java Look-and-Feel Graphics Repository
- Java Media Framework
- Quicktime for Java
- Media in Java Review 2008
- External Multimedia in Java
- Graphs and Charts
- Holiday Special: Electronic Greeting Card
- Media Framework: Presenter Application
- Standard Widget Toolkit
- JFace
- Java Performance Tuning
- J2EE Performance Tuning
- Caches and Pools
- Java Caching System
- EHCache
- Java Compression and Decompression
- Obfuscating Java Applications
- Continuous Integration
- Load Testing
- Tomcat Clustering
- High Scalability with Terracotta
- Troubleshooting Production Performance Issues
- Enterprise Java Testing
- Automated Unit Testing with JUnit and Ant
- Unit Testing: Tips From The Trenches
- Custom Ant Tasks
- Extensible Markup Language (XML)
- Java Web Technologies
-
Web Frameworks
- Performance Analysis Plan
- The Players
- The Evolution of Dynamic Content Generation
- The Model-View-Controller Pattern
- Apache Struts
- Building a Simple Apache Struts Example
- Server-side Form Validation
- Client-side Form Validation
- The Struts Validator
- The Struts Validator – Part II
- Custom Validators
- Tiles
- NetBeans
- Struts Development with MyEclipse
- Struts Development with BEA Workshop for Struts
- Struts Development with BEA Workshop for Struts, Part II
- Summary
- Struts 2
- Wicket
- JavaServer Faces
- Distributed Programming / RMI
- Behavior Tracking Servlet Filter
- Servlet Filters
- Building a Robust Java Server
- J2EE: Enterprise Java
- Spring
- Spring 3
- Java Design Patterns
- Model-Driven Architecture
- Enterprise Messaging with ActiveMQ
- Event-Driven Architecture
- XDoclet
- Hibernate
- Developing Standalone Database Applications with Hypersonic DB
- Project Backup
- J2EE Project: Hands-On
- Enterprise Java Beans (EJB) 3.0
- Disaster Recovery
- Java Management Extensions (JMX)
- Service-Oriented Architecture
- Web Services
- RESTful Web Services
- Web Services with Apache CXF
- Atom Syndication
- Project: Building a Web Photo Gallery
- J2ME: Micro Java
- Specialized J2ME
- Optional Packages
- Other Java Technologies
- Derivatives and Competitors
- Java, Engineered for Integration
-
Additional Resources
- The World of Java Tools
- Building Java Applications with Ant
- Managing Java Build Lifecycles with Maven
- Acceptance Testing with FitNesse
- Source Control with Subversion
- Inversion of Control and Dependency Injection
- Certification
- Roadmap: Becoming an Enterprise Java Developer
- Roadmap: Becoming an Enterprise Java Developer in 2007
- The Business of Enterprise Software
- JavaOne 2006
- JavaOne 2007
- JavaOne 2008 Wrap-Up
- JavaOne 2009 Wrap-Up
- JavaOne 2010
- JavaOne 2011
- How to Survive in a Turbulent Job Market
- How to Hire the Best Talent
- Unified Modeling Language (UML)
- Cloud Computing
- Amazon EC2 and Java
- MongoDB
- Enterprise Java in 2008 and Beyond
- Predictions for 2018
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 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 2. Creating a new MyEclipse Web Project, page 2
After you have created your project, the package explorer should resemble Figure 3.
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 4. Adding Struts capabilities to your web application
This will show the dialog box in Figure 5.
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 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 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 8. Struts Designer
Right-click on the design page and choose "New" and then "Form, Action and JSP," shown in Figure 9.
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 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 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 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 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 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 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 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 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!
