JBoss.orgCommunity Documentation

Chapter 4. Coding the Various Files

4.1. Java Stub Classes
4.1.1. GetNameForm.java
4.1.2. GreetingAction.java
4.2. JSP Pages
4.2.1. inputname.jsp
4.2.2. greeting.jsp
4.2.3. index.jsp

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.

private String name = "";

     private String greetName = "";
this.name = "";

     this.greetName = "";
ActionErrors errors = new ActionErrors();

     return errors;

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 = "";
    private String greetName = "";
    
    public String getName() 
    {
        return name;
    }
    public void setName(String name) 
    {
        this.name = name;
    }
    
    public String getGreetName() 
    {
        return greetName;
    }
    
    public void setGreetName(String greetName) 
    {
        this.greetName = greetName;
    }
    
    public GetNameForm() 
    {
    }
    
    public void reset(ActionMapping actionMapping, HttpServletRequest request) 
    {
        this.name = "";
        this.greetName = "";
    }
    
    public ActionErrors validate(ActionMapping actionMapping, 
            HttpServletRequest request) 
    {
        ActionErrors errors = new ActionErrors();
        return errors;
    }
}
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);
    }
}

The last thing left to do is to code the JSP files whose editors should still be open from having been created as placeholders.