Struts2 模型驱动操作中的继承可能吗?

发布于 2024-08-30 08:31:30 字数 3773 浏览 11 评论 0原文

我有一个模型驱动的 Struts2 操作,可以提供正确的 JSON 响应。当我重新构建操作时,我收到一个空的 JSON 响应。有人使用 Struts2 模型驱动操作进行继承吗?

我尝试在 struts 配置中显式设置包含属性:

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

下面所有操作的代码 - 不是实际使用的代码 - 为了清楚起见,我已进行编辑和剥离。

提前致谢。

提供正确响应的操作:

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

提供错误响应的重组操作:

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万水千山粽是情ミ 2024-09-06 08:31:30

将特定于类型的代码移至子类,并修复 getModel() 以返回正确的域对象:

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

Moved type-specific code to child classes, and fixed getModel() to return the correct domain object:

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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文