开发者

Converting a Servlet to Struts2 Action Class

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;
     protec开发者_运维技巧ted 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.


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>


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜