October 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Developing a Struts Application with the NetBeans IDE

  • June 20, 2010
  • By Anghel Leonard
  • Send Email »
  • More Articles »

You know Struts is one of the most popular free, open-source frameworks for creating Java Web applications. You know it's based on the Model-View-Controller (MVC) architecture to separate concerns in an application. But did you know that Struts has been integrated into the NetBeans IDE since version 6.7? The current version (as of this writing) is version 6.9 and the example in this article uses NetBeans 6.8.

If you were unaware of the Struts/NetBeans IDE integration, this article will walk you through the NetBeans interfaces and wizards for developing a simple Struts login application. Before starting, you need to download NetBeans 6.8 or higher (try the Java Web and EE release). Remember that you do not need to explicitly download Struts, because Struts 1.3.8 is already bundled in the NetBeans IDE.

Creating the Struts Application Stub in NetBeans Style

After you start NetBeans, you can create a Struts application stub with a few simple steps as follows (NetBeans automatically generates this application, which you can consider the starting point for any Struts application):

  1. Choose File | New Project.
  2. Under Categories, select Web. Under Projects, select Web Application and click Next (see Figure 1).



    Click here for larger image

    Figure 1. Starting a New Web Project in NetBeans IDE

  3. On this page, under the Project Name section, type login and select the location of the project. Click Next.
  4. In the next panel, you can choose the server that will host this application, the Java EE version and the context path. For this example, choose GlassFish v3 server, Java EE 6 Web and do not modify the default context path. Click Next.
  5. In the Frameworks panel, select Struts 1.3.8 (see Figure 2). This is a major step because you indicate to NetBeans your intention to use Struts. Click Finish.



    Click here for larger image

    Figure 2. Adding Struts Framework to Your Web Project

Below the Frameworks panel, you will find the following set of configuration settings for Struts (do not modify any):

  • Action Servlet Name: indicates the name of the Struts action servlet (This is mapped in web.xml descriptor.)
  • Action URL Pattern: represents the incoming requests, which are mapped to the Struts action controller (By default, only the *.do pattern is mapped in the deployment descriptor.)
  • Application Resource: in this section, you may specify the resource bundle, which will be used in the struts-config.xml descriptor
  • Add Struts TLDs: lets you generate tag library descriptors for the Struts tag libraries

What the Generated Stub Contains?

After the stub it is generated, you should see the welcomeStruts.jsp page in the NetBeans editor. Before modifying this page, consider what else was added/generated in your Struts application stub. For starters, notice that the Struts libraries were added in the project under the Libraries folder of the login project (see this in the Projects tab). Going further, you should notice the content of the Configuration Files folder, which contains the Struts-specific descriptors (struts-config.xml, tiles-defs.xml, validation.xml, validator-rules.xml) and the application deployment descriptor (web.xml) -- all of them were generated with minimal information but enough to have a functional Struts application. Take the time to open these files and explore their contents. Finally, notice the two JSP pages: index.jsp and welcomeStruts.jsp. The former contains a simple JSP-style redirect to the latter, which displays a simple welcome message.

Creating the Login.jsp Page from WelcomeStruts.jsp

As stated in the introduction, you will develop a simple login application. Now, that you have the stub it is time to go further and start developing the login.jsp page, which will render the login form. For this, you can create a new JSP page and delete the welcomeStruts.jsp, or you can try to modify this existing page. First you will employ the second option, and in the next section you will develop a JSP page from scratch.

Here are the steps of transforming the welcomeStruts.jsp into login.jsp:

  1. Rename the welcomeStruts.jsp page by right clicking on its name in the Projects tab (under the Web Pages folder) and choose Rename. Type the new name without an extension and press OK.
  2. Go to index.jsp and modify the redirect to login.jsp like this:
    <jsp:forward page="Login.do"/>


  3. Open the struts-config.xml file and navigate to the Global Forwards section. Notice that you can navigate to any section from this descriptor by using the NetBeans Navigator, which automatically appears by default in the lower-lefthand corner, like in Figure 3.


    Figure 3. NetBeans IDE Navigator Panel

  4. Modify the single global forward like this:

    <global-forwards>
       <forward name="login" path="/Login.do"/>
    </global-forwards>


  5. Navigate to the Action Mappings section and modify the single action like this:

    <action-mappings>
       <action path="/Login" forward="/login.jsp"/>
     </action-mappings>


Now that you have completed the renaming and fixed the navigation to your login.jsp page, it's time to add some real content to it. In this case, you will render a simple login form mapped in a simple HTML table (note that you can speed up the development of this table by using the NetBeans Palette -- if it is not clear that you can activate it from the Window menu).

When you type in the Source Editor, NetBeans provides you with code completion for Struts tags, as well as the Struts Javadoc. You can also invoke code completion manually by pressing Ctrl-Space. Figure 4 presents a simple capture of inserting a Struts form into your page.



Click here for larger image

Figure 4. Sample of Struts Code Completion and Javadoc

Now, the login.jsp page code looks like this (typical Struts application code):

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

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

<html:html>
 <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>Login page</title>        
 </head>
 <body style="background-color: white">

     <html:form action="/login">
         <html:errors property="wrongemail" />
         <html:errors property="wrongpass" />
         <table border="1">
             <thead>
                 <tr>
                     <th>Login to your site</th>
                     <th></th>
                 </tr>
             </thead>
             <tbody>
                 <tr>
                     <td>Email:</td>
                     <td><html:text property="email" /></td>
                 </tr>
                 <tr>
                     <td>Password:</td>
                     <td><html:password property="pass" /></td>
                 </tr>
             </tbody>
         </table>
         <html:submit value="Login" />
     </html:form>

 </body>
</html:html>



Tags: Java EE, NetBeans, Struts



Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel