struts2 ModelDriven有界通配符给出了错误

时间:2017-12-26 12:15:16

标签: java generics struts2 model-driven

我将struts 1应用程序移植到struts 2。 在struts 1应用程序中,有一个继承自CommonAction的{​​{1}}类 org.apache.struts.actions.DispatchAction

public class CommonAction extends DispatchAction 类也是应用程序中所有其他Action类的超类,如
CommonAction

public class ManageProfilesAction extends CommonAction ManageProfilesAction这样的子类都有接收来自网络请求的操作方法。如果是CommonAction,则关联的表单bean为ManageProfilesAction,而ManageProfilesForm的表单bean为CommonAction
现在使用struts 2方法,我声明CommonFormCommonAction喜欢

ManageProfilesAction

public class CommonAction implements ServletRequestAware, ModelDriven<? extends CommonForm> {
  protected CommonForm form = new CommonForm();
  @Override
  public CommonForm getModel() {
    return form;
  }
}

public class ManageProfilesAction extends CommonAction {
  private ManageProfilesForm form = new ManageProfilesForm();
  @Override  
  public ManageProfilesForm getModel() {  
    return form;
  }
}

这样public class CommonAction implements ServletRequestAware, ModelDriven<? extends CommonForm> { protected CommonForm form = new CommonForm(); @Override public CommonForm getModel() { return form; } } public class ManageProfilesAction extends CommonAction { private ManageProfilesForm form = new ManageProfilesForm(); @Override public ManageProfilesForm getModel() { return form; } } 及其子类如都可以有接收Web请求的方法以及相关的模型/表单; CommonForm和ManageProfilesForm分别 但是,我收到编译错误:
CommonAction

它是否可以用某种Java泛型魔法来实现,还是我需要彻底改变设计?

2 个答案:

答案 0 :(得分:0)

接口ModelDriven需要类或接口声明。您不能通过实现接口来使用有界通配符。

您可以在Java tutorial.

中详细了解相关信息

答案 1 :(得分:0)

我设法将CommonForm和ManageProfilesForm用作:

public class CommonAction implements ServletRequestAware, ModelDriven<CommonForm> {
  protected CommonForm commonForm = new CommonForm();
  @Override
  public CommonForm getModel() {
    return commonForm;
  }
}

public class ManageProfilesAction extends CommonAction {
  private ManageProfilesForm profForm = new ManageProfilesForm();
  @Override  
  public ManageProfilesForm getModel() {  
    return profForm;
  }
}

当然,ManageProfilesForm继承自CommonForm。