Spring mvc: One step view or Two Step View?

gstackoverflow :

I am reading famous Fauler's book "Enterprise application patterns" And I can't understand semantic difference between One step view(top part) and Two Step View(bottom part)

enter image description here

I read all text but I understood only one thing: In case of two step view I have 1 additional intermediate logical view. And this is executed by some global "entity".

It is really not clear for me why do I need it. In my practice I use spring - mvc for web application. Could you explain which pattern is used inside this framework? Could I choose/change it ?

Aleksandr Semyannikov :

Stages responsibility

  1. The first stage translates a model to a logical view - you may think about it as a model described in terms of some universal form (Fowler uses XML for this purpose).

  2. The second stage is able to read logical view and is responsible for rendering this to rich interface with styles, formatting, and all stuff we love.

The way we get a view

You need to make some HTML having an object of next class:

public class Customer {
    private String name; //value: John Snow
    private String address; //Winterfell
}

On the first stage you translate that customer data into logical view:

<screen>
<title>Customer data</title>
<name>John Snow</name>
<address>Winterfell</address>
</screen>

XML is a good form to be transformed by XSLT, so our second stage is presented by some XSLT that transform our XML to some HTML code and surround it by footer, header, some menu items, etc.

Now we have a scheme: model -> fist stage -> xml -> second stage(XSLT) -> html -> user

What we can do with this

That scheme will let you:

  1. To create some themes for your application. In this example you can use XSLT script as a theme. You change the theme, your application appearance changes as well.
  2. To make some global changes in your application appearance by changing only one XSLT script.

If you use one-step view, you will have to change every view this case.

It's how Fowler describe this. You might find this interesting because in your edition of the book it might be described in a different way:

Two Step View deals with this problem by splitting the transformation into two stages. The first transforms the model data into a logical presentation without any specific formatting; the second converts that logical presentation with the actual formatting needed. This way you can make a global change by altering the second stage, or you can support multiple output looks and feels with one second stage each.

How it is related to Spring MVC

Spring MVC implements another pattern - model-view-controller. Our pattern is a view part and we are free to decide what exactltly view technology to use with Spring MVC. If you will find some view framework that implements this pattern, you are free to plug it to Spring MVC.

If you don't know why you need to use two-stage view pattern, probably, you don't. In my expierence I've never seen this implemented in any project I've worked at.

What if we use one step view instead

In this case if we need to make global changes in our application appearance, we have to change every view that generates html for user.

Conclusion

Remember that the book is written about 20 years ago, and nowadays we have a lot of great technologies that didn't exist 20 years ago.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=305928&siteId=1