Java – independent model and UI view model in spring (MVVM in spring)

I want to start discussing the separation of pure model and UI Model in spring 3

Pure model I mean the main object / object retrieved from the database, assuming that there are some "user accounts" It contains enough information to display it in an HTML view or pass it to a web service

Through the UI model, I mean all the auxiliary tools I need in the UI to process the object For example If the user account has status, I need to get all status from the database, such as combo boxes These ideas are tricky. In some cases, they need more information and in others, they need less It's also good to be able to change some lists by adding items such as "select all", which is pure UI content (and inconvenient from view templates)

I've heard of the so-called model view ViewModel model, which seems to solve these problems, but I've never tried to implement it

The solution I'm using now is to decompose the logic into two services - one for the pure model and one for the UI model It looks like this:

@RequestMapping(value="app/user_accounts/{id}")
public String getUserAccount(@PathVariable("id") String id) {
    service.getUserAccount(id); // Gets main object and puts it into model
    presenter.formUserAccount(); // Gets all classifier for main object's properties
    return "user_account";
}

What I don't like is that views and their so-called view models are not related to each other I can call presenter Formuseraccount() and returns a completely unrelated view name

The other approach I see is similar to spring controller annotation We have classes annotated @ viewmodels and methods mapped to view names The interceptor finds and executes these methods before rendering a view These look elegant, but they require a lot of coding

How would you solve this problem?

Solution

I've been thinking about this in the Grails environment based on spring MVC My approach is to use the command object as the view model because it provides you with the data binding and validation functions you need in the view model Using the command object, you can use the command object to map view attributes to domain objects, abstracting the "pure model" (or domain model) from the view

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>