将 Servlet 转换为 Struts2 操作类

发布于 2024-09-25 05:15:01 字数 1811 浏览 3 评论 0原文

我正在将一个动态 Web 项目移植到 Struts2,并且必须将许多 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");
       }
}

有人可以编写一个相当于 Servlet 的扩展 BaseAction 的操作类吗? 提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送舟行 2024-10-02 05:15:01

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

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>

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>
烈酒灼喉 2024-10-02 05:15:01

我希望下面的示例能帮助你..

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文