Common Support class for Action classes in Struts 2
When you are about to create a complex application in struts 2, it is better to create a Base action class for all your actions and make this class extend ActionSupport and implement all necessary interfaces such as SessionAware, ParameterAware, HttpServletRequest, HttpServletResponse etc and implement all other common logic for your actions in this class. So now when you are about to create an action class, just extending this support class will suit all your need in action class.
** UPDATE: Struts 2 Complete tutorial now available here.
Following is a snippet of my ApplicationSupportAction class:
import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ParameterAware; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; public abstract class ApplicationSupportAction extends ActionSupport implements SessionAware, ServletRequestAware, ParameterAware, ServletResponseAware { private static final long serialVersionUID = 1L; // Field to store session context. private Map<String, Object> session; // Field to store request parameters. private Map<String, String[]> parameters; // Field to store HttpServletRequest. private HttpServletRequest servletRequest = null; // Field to store HttpServletResponse. private HttpServletResponse servletResponse = null; // Result code for session expired action. public final static String SESSION_EXPIRED = "SessionExpired"; // Result code for Logout action. public final static String LOGOUT = "logout"; // ..... // action to be triggered when session expires public String execute() throws Exception { HttpSession sessionObj = servletRequest.getSession(true); if (sessionObj.getAttribute("currentUser") == null) { return SESSION_EXPIRED; } return executeAction(); } public abstract String executeAction() throws Exception; @Override public void setSession(Map<String, Object> session) { this.session = session; } @Override public void setParameters(Map<String, String[]> parameters) { this.parameters = parameters; } @Override public void setServletResponse(HttpServletResponse servletResponse) { this.servletResponse = servletResponse; } @Override public void setServletRequest(HttpServletRequest servletRequest) { this.servletRequest = servletRequest; } public Map<String, Object> getSession() { return session; } public Map<String, String[]> getParameters() { return parameters; } public HttpServletRequest getServletRequest() { return servletRequest; } public HttpServletResponse getServletResponse() { return servletResponse; } public String getCurrentUser() { return (String) session.get("currentUser"); } public void setCurrentUser(String currentUser) { session.put("currentUser", currentUser); } }
You can handle following common concerns in the ApplicationSupportAction class:
1. Session
2. Parameters
3. Http Request and response
4. Validating session.
5. Common result codes etc.
ApplicationSupportAction class would reduce your action class code to a large extent by handling lot of common concerns in one centralized place.