如何覆盖ActionSupport和amp;的方法HttpServlet的

时间:2018-02-20 05:39:28

标签: java

我需要覆盖ActionSupport和amp;的方法。 HttpServlet给我 BaseSupport类。作为一个例子,我想覆盖我的基础支持类中的两个方法。这意味着我需要扩展ActionSupport和amp;我的BaseSupport类的HttpServlet类

//From ActionSupport class
public void addActionError(String anErrorMessage) {
    this.validationAware.addActionError(anErrorMessage);
}

//From HttpServlet class
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //method body
}

怎么做?

1 个答案:

答案 0 :(得分:0)

我认为您可以创建一个扩展HttpServlet的类和一个扩展ActionSupport的内部类,您可以在此类中使用ActionSupport的实例。因为java不支持多重继承。仅支持多重继承接口。

<!-- language: java -->
public class BaseSupportServlet extends HttpServlet{

    private class CustomActionSupport extends ActionSupport{
        public void addActionError(String anErrorMessage) {
            this.validationAware.addActionError(anErrorMessage);
        }
    }

    private ActionSupport myActionSupport;

    public BaseSupportServlet(){
        myActionSupport = new CustomActionSupport();
    }

    //From HttpServlet class
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //method body
    }

    public void addActionError(String anErrorMessage) {
        myActionSupport.addActionError(anErrorMessage);
    }
}

这可能是你的解决方案吗?