2010-04-29 3 views
2

J'ai une action Struts2 pilotée par le modèle qui fournit une réponse JSON correcte. Lorsque je restructure l'action, j'obtiens une réponse JSON vide. Est-ce que quelqu'un a hérité de travailler avec des actions Struts2 Model-Driven?L'héritage dans Struts2 est-il possible?

Ive a essayé définissant explicitement inclure des propriétés dans config Struts:

 
<result name="json" type="json"> 
    <param name="includeProperties"> 
     jsonResponse 
    </param> 
</result> 

code pour toutes les actions ci-après - code réel utilisé - je l'ai édité et dépouillé pour plus de clarté.

Merci d'avance.

action fournissant une réponse correcte:

 
public class Bike extends ActionSupport implements ModelDriven, Preparable { 

    @Autowired private Service bikeService; 
    private JsonResponse jsonResponse; 
    private com.ets.model.Vehicle bike; 
    private int id; 

    public Bike() { 
     jsonResponse = new JsonResponse("Bike"); 
    } 

    @Override 
    public void prepare() throws Exception { 
     if (id == 0) { 
      bike = new com.ets.model.Bike(); 
     } else { 
      bike = bikeService.find(id); 
     } 
    } 

    @Override 
    public Object getModel() { 
     return bike; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public void setBikeService(@Qualifier("bikeService") Service bikeService) { 
     this.bikeService = bikeService; 
    } 

    public JsonResponse getJsonResponse() { 
     return jsonResponse; 
    } 

    public String delete() { 
     try { 
      bike.setDeleted(new Date(System.currentTimeMillis())); 
      bikeService.updateOrSave(bike); 
      jsonResponse.addActionedId(id); 
      jsonResponse.setAction("delete"); 
      jsonResponse.setValid(true); 
     } catch (Exception exception) { 
      jsonResponse.setMessage(exception.toString()); 
     } 
     return "json"; 
    } 
} 

Actions restructurées fournissant une réponse incorrecte:

 
public abstract class Vehicle extends ActionSupport implements ModelDriven { 

    @Autowired protected Service bikeService; 
    @Autowired protected Service carService; 
    protected JsonResponse jsonResponse; 
    protected com.ets.model.Vehicle vehicle; 
    protected int id; 

    protected abstract Service service(); 

    @Override 
    public Object getModel() { 
     return bike; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public void setBikeService(@Qualifier("bikeService") Service bikeService) { 
     this.bikeService = bikeService; 
    } 

    public void setCarService(@Qualifier("carService") Service carService) { 
     this.carService = carService; 
    } 

    public JsonResponse getJsonResponse() { 
     return jsonResponse; 
    } 

    public String delete() { 
     try { 
      vehicle.setDeleted(new Date(System.currentTimeMillis())); 
      service().updateOrSave(vehicle); 
      jsonResponse.addActionedId(id); 
      jsonResponse.setAction("delete"); 
      jsonResponse.setValid(true); 
     } catch (Exception exception) { 
      jsonResponse.setMessage(exception.toString()); 
     } 
     return "json"; 
    } 

} 

public class Bike extends Vehicle implements Preparable { 

    public Bike() { 
     jsonResponse = new JsonResponse("Bike"); 
    } 

    @Override 
    public void prepare() throws Exception { 
     if (id == 0) { 
      vehicle = new com.ets.model.Bike(); 
     } else { 
      vehicle = bikeService.find(id); 
     } 
    } 

    @Override 
    protected Service service() { 
     return bikeService; 
    } 

} 

public class Car extends Vehicle implements Preparable { 

    public Car() { 
     jsonResponse = new JsonResponse("Car"); 
    } 

    @Override 
    public void prepare() throws Exception { 
     if (id == 0) { 
      vehicle = new com.ets.model.Car(); 
     } else { 
      vehicle = carService.find(id); 
     } 
    } 

    @Override 
    protected Service service() { 
     return carService; 
    } 

} 
+1

Il semble qu'il y ait une certaine confusion entre les champs de bicyclette et de véhicule dans vos exemples, pouvez-vous clarifier votre exemple et vérifier qu'il compile? – Geoff

Répondre

0

Déplacé code spécifique de type à des classes d'enfants et getModel() fixée à renvoie l'objet de domaine approprié:

public abstract class Vehicle extends ActionSupport 
     implements ModelDriven, Preparable { 

    protected int id; 

    protected abstract Service service(); 
    public abstract void setService(Service service); 
    public abstract JsonResponse getJsonResponse(); 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String delete() { 
     JsonResponse jsonResponse = getJsonResponse(); 
     try { 
      getModel().setDeleted(new Date(System.currentTimeMillis())); 
      service().updateOrSave(getModel()); 
      jsonResponse.addActionedId(id); 
      jsonResponse.setAction("delete"); 
      jsonResponse.setValid(true); 
     } catch (Exception exception) { 
      jsonResponse.setMessage(exception.toString()); 
     } 
     return "json"; 
    } 
} 

public class Bike extends Vehicle { 

    @Autowired protected Service bikeService; 
    private com.ets.model.Bike model; 

    @Override 
    public void prepare() throws Exception { 
     if (id == 0) { 
      model = new com.ets.model.Bike(); 
     } else { 
      model = bikeService.find(id); 
     } 
    } 

    @Override 
    public Object getModel() { 
     return model; 
    } 

    @Override 
    protected Service service() { 
     return bikeService; 
    } 

    @Override 
    public void setService(@Qualifier("bikeService") Service bikeService) { 
     this.bikeService = bikeService; 
    } 

    @Override 
    public JsonResponse getJsonResponse() { 
     return new JsonResponse("Bike"); 
    } 
} 

public class Car extends Vehicle { 

    @Autowired protected Service carService; 
    private com.ets.model.Car model; 

    @Override 
    public void prepare() throws Exception { 
     if (id == 0) { 
      model = new com.ets.model.Car(); 
     } else { 
      model = carService.find(id); 
     } 
    } 

    @Override 
    public Object getModel() { 
     return model; 
    } 

    @Override 
    protected Service service() { 
     return carService; 
    } 

    @Override 
    public void setService(@Qualifier("carService") Service carService) { 
     this.carService = carService; 
    } 

    @Override 
    public JsonResponse getJsonResponse() { 
     return new JsonResponse("Car"); 
    } 
}