JBoss.orgCommunity Documentation
We will now code both the Java stub classes just generated, the JSP files left in as placeholders from previous steps, and a new start JSP page we will have to create.
To finish the two Java classes, switch to the Package Explorer view and expand the JavaSource > sample folder
Double-click GetNameForm.java for editing
You are looking at a Java stub class that was generated by JBoss Tools. Now we are going to edit the file
Add the following attributes at the beginning of the class:
private String name = "";
Inside the reset method, delete the TO DO and throw lines and add:
this.name = "";
Inside the validate method, delete the TO DO and throw lines and add:
ActionErrors errors = new ActionErrors();
return errors;
Right-click and select Source > Generate Getters and Setters from the context menu
In the dialog box, check the check box for name, select First method for Insertion point, and click on the OK button
The final GetNameForm.java file should look like this:
package sample;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
public class GetNameForm extends org.apache.struts.action.ActionForm
{
private String name = "";
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public GetNameForm()
{
}
public void reset(ActionMapping actionMapping, HttpServletRequest request)
{
this.name = "";
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest request)
{
ActionErrors errors = new ActionErrors();
return errors;
}
}
Save the file
Open GreetingAction.java for editing
Inside the execute method, delete the TO DO lines and add the following:
String name = ((GetNameForm)form).getName();
String greeting = "Hello, "+name+"!";
((GetNameForm)form).setName(greeting);
return mapping.findForward(FORWARD_sayHello);
The final version of GreetingAction.java should look like this:
package sample;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class GreetingAction extends org.apache.struts.action.Action
{
// Global Forwards
public static final String GLOBAL_FORWARD_getName = "getName";
// Local Forwards
public static final String FORWARD_sayHello = "sayHello";
public GreetingAction()
{
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception
{
String name = ((GetNameForm)form).getName();
String greeting = "Hello, "+name+"!";
((GetNameForm)form).setName(greeting);
return mapping.findForward(FORWARD_sayHello);
}
}
Save the file
Close the editors for the two Java files
The last thing left to do is to code the JSP files whose editors should still be open from having been created as placeholders.
In this page, the user will enter any name and click the submit button. Then, the greeting action will be called through the form.
Click on the inputname.jsp tab in the Editing area to bring its editor forward
In the Web Projects view, expand StrutsHello > Configuration > default > struts-config.xml > action-mappings and select /greeting
Drag it and drop it between the quotes for the
"action"
attribute to the
<html:form>
element in the Source pane of the editor
Then type this text on a new line just below this line:
Input name:
Select the Visual pane of the editor
Then, in the JBoss Tools Palette, expand the Struts Form library, select text , and drag it onto the box
By default there are only four groups on the JBoss Tools Palette. If you wish to make some group visible click the Show/Hide button on the top of palette and in the prompted dialog check the group (or groups) you want to be shown.
In the Insert Tag dialog box, type in name for property and select Finish
In the StrutsForm library in the JBoss Tools Palette, select submit , and drag it to right after the text box in the Visual pane of the editor
Right-click the
submit
button and select
<html:submit>
Attributes from the context menu
In the Attributes dialog box, select the value field and type in "Say Hello!" for its value
After tidying the page source, the Editor window for the file should look something like this:
Next, we will fill in the result page.
Click on the greeting.jsp tab in the Editing area to bring its editor forward
Type in the following code:
<html>
<head>
<title>Greeting</title>
</head>
<body>
<p>
</p>
</body>
</html>
To complete editing of this file, we will use macros from the JBoss Tools Palette. This palette is a view that should be available to the right of the editing area.
Click on the Struts Common folder in the JBoss Tools Palette to open it
Position the cursor at the beginning of the greeting.jsp file in the Source pane and then click on bean taglib in the JBoss Tools Palette
This will insert the following line at the top of the file:
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
Click on the Struts Bean folder in the JBoss Tools Palette to open it
Position the cursor inside the
<p>
element
Click on write in the JBoss Tools Palette
Type in "GetNameForm" for the name attribute and add a property attribute with "name" as its value
The editor should now look like this:
Finally, we will need to create and edit an index.jsp page. This page will use a Struts forward to simply redirect us to the getName global forward.
In the Web Projects view, right-click on StrutsHello > WEB-ROOT(WebContent) node and select New > File > JSP
Type index for Name and click on the Finish button
On the JBoss Tools Palette, select the Struts Common folder of macros by clicking on it in the palette
Click on the logic taglib icon
Press the Enter key in the editor to go to the next line
Back on the palette, select the Struts Logic folder of macros
Click on redirect
Delete the ending tag, put a forward slash in front of the closing angle bracket, and type "forward=getName" in front of the slash
The finished code for the page is shown below:
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<logic:redirect forward="getName"/>
To save all the edits to files, select File>Save All from the menu bar