Learn Java for Web Development

(Tina Meador) #1
CHAPTER 4: Building a Web Application Using Struts 2 165

This action class is configured in the struts.xml file, as illustrated in Listing 4-2.


Listing 4-2. Configuring the Action Class in struts.xml


<package name="helloWorld" " extends="struts-default" namespace="/ >



/hello.jsp


The action mapping in Listing 4-2 uses the name attribute to define the name of the action you
can use to access this action class and uses the result tag to define which result page should be
returned to the user. Now you can access the action via an .action extension.


http://localhost:8080/helloWorldExample//hello.action


Even though you can use POJO actions, Struts 2 provides two action helpers that can be used: the
Action interface and the ActionSupport class.


Action Interface

Struts 2 comes with an optional Action interface, as illustrated in Listing 4-3.


Listing 4-3. Action Interface


package com.opensymphony.xwork2;


public interface Action {
public static final String ERROR = "error";
public static final String INPUT = "input";
public static final String LOGIN = "login";
public static final String NONE = "none";
public static final String SUCCESS = "success";


public String execute();
}


This interface provides the common string-based return values as constants and the default
execute() method that should be implemented by the implementing classes.


The action class that implements this interface can use the constant value directly, as illustrated
in Listing 4-4.

Free download pdf