views:

137

answers:

0

Hello,

I have a Model-Driven Struts2 action that provides correct JSON response. When I re-structure the action I get an empty JSON response back. Has anyone got inheritance working with Struts2 Model-Driven actions?

Ive tried explicitly setting include properties in struts config:

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

Code for all actions below - not actual code in use - I have edited and stripped down for clarity.

Thanks in advance.

Action providing correct response:

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

Re-structured Actions providing incorrect response:

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

}