【问题标题】:The bounded wildcard gives a error using ModelDriven in Struts 2有界通配符在 Struts 2 中使用 ModelDriven 时会出错
【发布时间】:2018-06-07 07:01:00
【问题描述】:

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

在 Struts 1 应用程序中,有一个继承自 org.apache.struts.actions.DispatchActionCommonAction 类。

public class CommonAction extends DispatchAction  

CommonAction 类也是应用程序中所有其他 Action 类的超类,例如

public class ManageProfilesAction extends CommonAction

ManageProfilesActionCommonAction 等子类都有接收来自网络的请求的操作方法。对于ManageProfilesAction,关联的表单bean 是ManageProfilesForm,而CommonAction 的表单bean 名为CommonForm

现在使用 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 及其子类(如ManageProfilesAction)都可以具有接收Web 请求以及相关模型/表单的方法;分别为CommonFormManageProfilesForm

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

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

它是否可以使用一些 Java 泛型魔法或者我需要彻底改变设计?

【问题讨论】:

    标签: java generics struts2 model-driven


    【解决方案1】:

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

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

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

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

    您可以在Java tutorial. 中阅读更多相关信息

    【讨论】:

      【解决方案2】:

      我设法将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

      【讨论】:

        猜你喜欢
        • 2012-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-23
        • 1970-01-01
        • 2014-02-12
        相关资源
        最近更新 更多