将Servlet转换为Struts2 Action类

时间:2010-09-23 05:54:00

标签: java java-ee struts2

我正在向Struts2移植一个动态Web项目,我必须将许多servlet转换为Struts2操作。我想使用ServletRequestAware,ServletResponseAware和SessionAware接口,并保持大部分代码不变。请查看我从GOOGLin'找到的BaseAction.java类。我想使用它,以便我可以做其他操作来简单地扩展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;
     }

}

现在假设我有一个如下的servlet: 的 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");
       }
}

有人可以编写一个扩展BaseAction等效于Servlet的动作类吗? 提前谢谢。

2 个答案:

答案 0 :(得分:1)

由于您使用的是Struts2,我建议您利用框架提供的所有功能,而不是尝试破解现有的servlet代码。获取和设置请求参数和会话的http内容由struts2框架轻松处理:

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

您的struts.xml应包含操作的条目:

<action name="NewAction" class="yourapp.NewAction">
  <result type="redirect">login.jsp</result>
</action>

答案 1 :(得分:0)

我希望以下示例能帮到你..

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