Ten things a Struts developer should know

1.Starting up

  To create  a new project  for the application  the following objects are considered.

  • Go to the menu bar and select FileNewStruts Project
  • Next enter “StrutsHello” as the project name
  • Leave everything else as it is, and click Next
  • If you have server runtime already defined, just pass to next point. Otherwise in the Runtime section click the New button and target at needed server runtime environment. Click Finish.
  • Click Next
  • Make sure that struts-bean.tld , struts-html.tld , and struts-logic.tld are checked in the list of included tag libraries and then hit Finish

In the package explorer view a StrutsHello node should appear

  •  Click the plus sign next to StrutsHello to reveal the child nodes
  • Click the plus sign next to WebContent under StrutsHello
  • Click the plus sign next to WEB-INF under WebContent
  • Then, double-click on the struts-config.xml node to display a diagram of the Struts application configuration file in the editing area

Background lines are empty at this point.

2. Creating the application components

                     By creating the individual components we will design the application as a placeholder first.

3. Creating JS page placeholders

Let’s create and place two JSP pages. We will not write anything more than basic template code for the files; they will serve as placeholders. So we can create links to them in the diagram. We will write some custom code a little bit later.

Creating the page holders

  • Bring the Web Projects view to the front of the Package Explorer view by selecting the Web Projects tab next to that tab.
  • Right-click the StrutsHelloWEB-ROOT (WebContent) folder in the Web Projects view and select NewFolder…
  • Enter pages for a folder name and click Finish
  • We will keep our presentation files in this folder
  • Right-click the pages folder and select NewFileJSP
  • For Name type in input name (the JSP extension will be automatically added to the file), and then click on the Next button
  • Untick the use JSP Template checkbox, and then click on the Finish button
  • Right-click the pages folder again and select NewFileJSP
  • For Name type in greeting , and then click on the Next button
  • Untick the use JSP Template checkbox, and then click on the Finish button

4. Adding template code to the input name JSP file

                 The input name, JSP file is populated with some template code. In web pages view click on the input name JSP. Then modify the file so it looks like this.

<%@ taglib uri="/WEB-INF/struts-html" prefix="html" %>
<html:html>
<strong>&lt;head&gt;</strong>
 <title></title>
</head>
<body>
 <html:form action="">
 </html:form>
</body>
</html:html>

 

Place the two pages created on the diagram.

  • Click on the struts-config.xml tab in the editing area to bring the diagram to the front
  • Click on the inputname.jsp page in the Web Projects view, drag it onto the diagram, and drop it
  • Click on the greeting.jsp page in the Web Projects view, drag it onto the diagram, and drop it to the right of the /pages/inputname.jsp icon with some extra space

Now you should have two JSP pages in the diagram.

6. Creating an action mappings 

We are next going to create an action mapping using a context menu on the diagram.

  •      Right-click between the two icons and select NewAction
  • Enter the following values:

Table 2.1. Action values

path /greeting
  • On the left
  • Hand side of the diagram in the column of icons, click on the Create New Connection icon( l_> ).
scope request
type sample.GreetingAction
validate <leave blank>

 

(“GetNameForm” is the name for a form bean that we will create later.)

  • Click Finish

7. Creating a link

Now create a link from the input name, JSP page to the action.

  •    Connection icon(  l_> ).
    • In the connect-the-components mode you are in now, click on the /pages/inputname.jsp icon in the diagram and then click on the /greeting action

To the action a link will be created from the page.

8. Creating a foreward

Next we are going to create a forward for the action.

  •     On the left-hand side of the diagram in the column of icons, click on the Create New Connection icon( l_>), again.
    • Click on the /greeting action icon in the diagram and then click on the pages/greeting.jsp icon
    • That’s it. A link will be drawn from the actions new greeting forward to the greeting.jsp JSP page. Note that the forwards name will be set based on the name of the target JSP file name. If you don’t like it, you can easily change it
    • Select the Tree tab at the bottom of the editor window (between Diagram and Source)
    • Expand the struts-config.xml/action-mappings/ /greeting node and then select the greeting forward
    • In the Properties Editor to the right, change the text to “sayHello” in the Name field
    • Select the Diagram tab at the bottom of the editor window and see how the diagram is also updated to reflect the change

9. Creating a global forward

Global forward is the last component that we need to create in the diagram.

  • Somewhere in the top-left corner of diagram, right-click and select NewGlobal Forward
  • Enter getName in the Name field
  • Select the Change… button for Path
  • In the Edit Path window, switch to the Pages tab
  • Expand the StrutsHelloWEB-ROOT (WebContent)pages node and then select the inputname.jsp page
  • Click Ok .
  • Leave the rest of the fields blank and click OK

In the outline view a foreward  object now appears on the diagram and also in the global foreward folder. The diagram looks something like this by checking and dragging around each icon.

Diagram View
Diagram View

10. Creating a Form Bean

                          Create a form bean is the last thing we need to do.

  • Switch to the Tree viewer in the editor for the struts-config.xml file, by selecting the Tree tab at the bottom of the editor window
  • Right-click struts-config.xmlform-beans and select Create Form Bean
  • Enter GetNameForm in the name field and sample.GetNameForm for type
  • Click Finish
  • To save your changes to struts-config.xml, select FileSave from the menu bar