SpringMVC 表单绑定到作为接口的命令对象

发布于 2024-09-18 05:08:13 字数 1457 浏览 2 评论 0原文

我正在学习 SpringMVC 2,我有一个需要绑定到对象(命令)的表单。但是,如果我需要这个命令对象作为一个接口,以便我可以为该对象使用不同的实现(当然所有实现都将具有相同的字段),该怎么办?

为了绑定代表帐户的表单,我有这个控制器。 我是否可以将表单绑定到帐户接口,以便之后我可以像业务 bean 一样使用它?

或者只是告诉我对于以下流程的最佳 Spring 实践是什么: 表格->做业务逻辑->保存到数据库

public  class OpenAccountControllerSpring2
extends SimpleFormController {

private ClientDao clientDao;
private Account account;

public OpenAccountControllerSpring2() {
    setCommandClass( // dont know what to write here);
    setCommandName("newAccount");


}


protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
        Object command, BindException bindExp)
    throws Exception {
    try {
                    //here i just want to create a new Account, add it to a Client (Interface), then transform the Client into a database-bean and save it.

        int client_id = Integer.parseInt(request.getParameter("clientId"));
        Account account = (Account) command;
        Client client = Transformer.toBusinessHeavy(clientDao.getClient(client_id));
        client.addAccount(account);
        clientDao.updateClient(Transformer.toEntity(client));
    } catch (Exception err) {
        return new ModelAndView(this.getFormView());
    }

    return new ModelAndView(this.getSuccessView());
}

public void setClientDao(ClientDao dao) {
    this.clientDao = dao;
}


public void setAccount(Account account) {
    this.account = account;
}

}

I'm learning SpringMVC 2 and i have a form that i need to bind to an object (the command). But what if i need this command object to be an interface so i can use different implementations for the object (of course all the implementations will have the same fields).

For binding a form that represents an Account i have this controller.
Is it possible that i could bind the form to the Account Interface, so i can use it like a business bean after that ?

Or just tell me what are the best spring practices for a flow like:
FORM -> do Business Logic -> Save to DB

public  class OpenAccountControllerSpring2
extends SimpleFormController {

private ClientDao clientDao;
private Account account;

public OpenAccountControllerSpring2() {
    setCommandClass( // dont know what to write here);
    setCommandName("newAccount");


}


protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
        Object command, BindException bindExp)
    throws Exception {
    try {
                    //here i just want to create a new Account, add it to a Client (Interface), then transform the Client into a database-bean and save it.

        int client_id = Integer.parseInt(request.getParameter("clientId"));
        Account account = (Account) command;
        Client client = Transformer.toBusinessHeavy(clientDao.getClient(client_id));
        client.addAccount(account);
        clientDao.updateClient(Transformer.toEntity(client));
    } catch (Exception err) {
        return new ModelAndView(this.getFormView());
    }

    return new ModelAndView(this.getSuccessView());
}

public void setClientDao(ClientDao dao) {
    this.clientDao = dao;
}


public void setAccount(Account account) {
    this.account = account;
}

}

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

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

发布评论

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

评论(3

一身软味 2024-09-25 05:08:13

您绝对应该跳过 Spring 2 MVC 并直接进入 Spring 3 MVC。这是一次重大的进化飞跃,将为您省去很多麻烦。然而,当你尝试做你想做的事情时,你仍然会遇到同样的问题。 Spring 必须能够实例化表单命令对象,就像您收到的异常中所说的那样。正如它所说,它无法实例化接口。您必须尝试不同的方法,这可能需要您做更多的工作,而来自 Spring MVC 的帮助会更少。您可以直接使用请求参数值,而不使用命令对象。您必须手动将参数绑定到正确的子类实​​例。或者,您也许可以尝试为您的子类使用非抽象基类。这样,您可以将该类指定为命令对象,Spring 可以实例化它并将值绑定到它。您可以通过使用一个单独的类来对表单进行建模并且不在现有的类层次结构中来执行类似的操作。不过,你必须将其转化为你的课程。您可能还想检查您的对象模型并确保您确实正确地建模。一般来说,如果你正在做一些标准工具不支持的时髦事情,你可能会陷入糟糕的境地。

You should definitely skip Spring 2 MVC and go straight to Spring 3 MVC. It is a major evolutionary leap forward and will save you a lot of trouble. However, you will still run into the same problem trying to do what you want to do. Spring has to be able to instantiate the form command object, like it says in the exception you're getting. It cannot instantiate an interface, like it says. You'll have to try a different approach, which will likely involve more work on your part and less help from Spring MVC. You can just work with request parameter values directly and not use a command object. You will have to manually bind the parameters to your correct subclass instance. Or instead you could perhaps try to use a non-abstract base class for your subclasses. That way you could specify that class as the command object and Spring could instantiate it and bind values to it. You could do something similar by having a separate class that models your forms and is not in your existing class hierarchy. You'll have to translate that into your classes, though. You may also want to examine your object model and make sure you're really modeling things correctly. Generally if you are doing something funky that the standard tools don't support you are probably wandering off into bad territoy.

如此安好 2024-09-25 05:08:13

您为什么要学习 Spring MVC 2(其中大部分现已弃用),而不是 Spring MVC 3?新的注释驱动控制器更容易使用,并且在编写控制器的方式上提供了更大的灵活性。

如果您对 Spring MVC 3 感兴趣,这里是我编写的一系列示例这可能会有所帮助。

Why are you learning Spring MVC 2 (much of which is now deprecated), rather than Spring MVC 3? The new annotation-driven controllers are much easier to work with, and allow a lot more flexibility in the way you write your controllers.

If you're interested in Spring MVC 3, here is a series of examples I wrote which might be helpful.

雨后彩虹 2024-09-25 05:08:13

我通过使用接口、注入它的实现并实现 formBackingObject() 来完成此操作,在其中我从请求中获取信息并调用接口上的相应设置器。
这样我可以保留界面,但我必须做一点绑定工作。

I have done it by using an interface, injecting it's implementation and implementing formBackingObject() where i get the information from the request and call the respective setters on the interface.
This way i can keep the interface but i have to do just a little bit of binding work.

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