有界通配符在 Struts 2 中使用 ModelDriven 时出错

The bounded wildcard gives a error using ModelDriven in Struts 2

我正在将 Struts 1 应用程序移植到 Struts 2.

在Struts1应用中,有一个CommonActionclass继承自org.apache.struts.actions.DispatchAction

public class CommonAction extends DispatchAction  

CommonAction class 也是应用程序中所有其他操作 class 的超级 class,例如

public class ManageProfilesAction extends CommonAction

ManageProfilesActionCommonAction 这样的子class 都有从网络接收请求的操作方法。在 ManageProfilesAction 的情况下,关联的表单 bean 是 ManageProfilesFormCommonAction 具有名为 CommonForm.

的表单 bean

现在使用 Struts 2 方法,我声明 CommonActionManageProfilesAction 喜欢

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

这样 CommonAction 和它的子 class 像 ManageProfilesAction 都可以有方法来接收网络请求以及相关的 model/form; CommonFormManageProfilesForm 分别。

但是,我遇到编译器错误:

public class CommonAction implements ServletRequestAware, ModelDriven<? extends CommonForm> {
required: class or interface without bounds
found:    ? extends CommonForm

使用某些 Java 泛型魔术是否可行,或者我是否需要彻底更改设计?

接口 ModelDriven 需要 class 或接口声明。

public class CommonAction implements >ServletRequestAware, ModelDriven<? extends >CommonForm> {

required: class or interface >without bounds found: ? extends CommonForm

您不能通过实现接口来使用有界通配符。

接口 ModelDriven 是一个通用接口,它带有一个 类型参数 ,它应该是一个具体类型。

您可以在 Java tutorial.

中阅读更多相关信息

我设法同时使用 CommonFormManageProfilesForm 作为:

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