views:

129

answers:

2

I'm porting a dynamic web project to Struts2 and I gotta convert a many servlets to Struts2 actions. I want to use the ServletRequestAware, ServletResponseAware and SessionAware interfaces and keep most of the code unchanged. Please take a look at BaseAction.java class which I found by GOOGLin'. I want to use this so that i can make other actions to simply extend the BaseAction.

BaseAction.java

public abstract class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware,ServletResponseAware, ServletContextAware{

 private static final long serialVersionUID = 1L;
  protected Map session;
     protected HttpServletRequest request;
     protected HttpServletResponse response;

     public String execute() throws Exception {
         return doExecute();
     }

     protected abstract String doExecute() throws Exception;

  public Map getSession() {
         return session;
     }

     public void setSession(Map sess) {
         this.session = sess;
     }

     public HttpServletRequest getServletRequest() {
         return request;
     }

     public void setServletRequest(HttpServletRequest req){
         this.request = req;
     }

     public HttpServletResponse getServletResponse() {
         return response;
     }

     public void setServletResponse(HttpServletResponse resp) {
         this.response = resp;
     }

}

Now Suppose I have a servlet like following: Servlet.java

public class Servlet extends HttpServlet
{
 private static Logger log = Logger.getLogger(Servlet.class);

 public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException
 {
  HttpSession session=request.getSession();
 Bean bean= new Bean();
 bean.setName(request.getParameter("xxx"));
        session.setAttribute("bean");
        response.sendRedirect("login.jsp");
       }
}

Can someone write an action class extending the BaseAction equivalent to the Servlet? Thanks in advance.

A: 

i hope below sample will help u..

public class YourAction extends BaseAction {

    private static final long serialVersionUID = -6113083300296539328L;

    private static final Logger logger = Logger.getLogger(YourAction .class);

    private Bean bean;

    public String get() throws ApplicationException {
    try {
        this.setBean(getService().fetchBeanById(
                getHttpRequest().getParameter(
                "xxxx")));
    } catch (Exception e) {
        logger.error(new String[] { " Exception listing Comment ",
                e.toString() });
        throw new ApplicationException(e);
    }
        return SUCCESS;
    }

    public Bean getBean() {
        return bean;
    }

    public void setBean(Bean bean) {
        this.bean= bean;
    }
}
Jothi
ThanQ, but 1 thing is still not clear for me. I can get a form field value by getServletRequest.getParameter("xxxx"); Now, how do i set the bean into session using SessionAware interface. Do i have to put the bean into a map and is it that then struts takes care of the rest?? Or, can i use HttpSession ses=new HttpSession();
Dhora
((HttpServletRequest) ActionContext.getContext().get( StrutsStatics.HTTP_REQUEST)).getSession(); will gives you session object.
Jothi
A: 

Since you're using Struts2, I recommend taking advantage of everything the framework offers instead of trying to hack existing servlet code. The http stuff of getting and setting request parameters and sessions is handled easily by the struts2 framework:

public class NewAction implements SessionAware {

  private Map theSession;
  private XXX xxx;

  public String execute() {
    Bean b = new Bean();
    b.setXXX(xxx);
    theSession.put(b);

    return "success";
  }

  public XXX getXXX() {
    return xxx;
  }

  public void setXXX(XXX xxx) {
    this.xxx = xxx;
  }

  public Map getSession() {
    return theSession;
  }

  public void setSession(Map session) {
    theSession = session;
  }
}

Your struts.xml should contain an entry for the action:

<action name="NewAction" class="yourapp.NewAction">
  <result type="redirect">login.jsp</result>
</action>
gmallett
And remember that Struts2 instantiates a new Action for each request. A servlet instance (and also Struts 1 action) is instead used by (potentially) many request -even perhaps concurrent requests.
leonbloy
Thanks a lot, thats really a gud suggestion.
Dhora
setXXX method neednt return anything.
Dhora
Can u explain how to get a form field value into action class.
Dhora
@Dhora You don't need to do anything, it will be there because of the "magic" of the struts2 framework. If you have a form like: <form action="/app/NewAction"><input name="XXX" type="hidden"/></form> it will just work. Behind the scenes, the framework uses an "interceptor" class which gets the Http parameter and calls your action's setXXX method. As @leonbloy mentioned, an action is instantiated on each request. All of this is online in the (very good imo) struts2 documentation http://struts.apache.org/2.2.1/docs/guides.html.
gmallett