javagenericsstruts2model-driven

The bounded wildcard gives a error using ModelDriven in Struts 2


I'm porting a Struts 1 application to Struts 2.

In the Struts 1 application, there's a CommonAction class that inherits from org.apache.struts.actions.DispatchAction.

public class CommonAction extends DispatchAction  

The CommonAction class is also a super class of all the other Action classes in the application like

public class ManageProfilesAction extends CommonAction

Both the subclasses like ManageProfilesAction and the CommonAction have action methods that receive requests from the web. In case of ManageProfilesAction, the associated form bean is ManageProfilesForm while CommonAction has form bean named CommonForm.

Now using Struts 2 approach, I declared CommonAction and ManageProfilesAction like

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

so that both CommonAction and its subclasses like ManageProfilesAction can have methods that receive web requests along with the associated model/form; CommonForm and ManageProfilesForm respectively.

However, I'm getting an compiler error:

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

Is it somehow workable with some Java generics magic or do I need to change the design thoroughly?


Solution

  • I managed to use both CommonForm and ManageProfilesForm as:

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

    Ofcourse, ManageProfilesForm inherits from CommonForm.