Announcement Announcement Module
No announcement yet.
Spring MVC Component-like Controllers Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC Component-like Controllers

    I'm currently using Spring MVC for my web applications. The problem is that the MVC pattern of Spring is based on pages. Each page has its command object, its controller and its view. What I am looking for is the possibility to have more than 1 controller per page. The page should be composed of several Spring MVC Controllers.

    I've been looking at SiteMesh and Tiles so far and came to the following conclusions:

    SiteMesh: SiteMesh does not seem to be capable to solve my problem. SiteMesh is used to assemble a web page according to a decorator. After the DispatcherServlet has received the request, the page will be rendered according to the registered decorator. The single parts that are used to assemble the resulting page do not contain Spring MVC Controllers. A SiteMesh site still has just 1 controller as far as I understood.

    Tiles: Tiles really seems to be a possiblity to have more than 1 controller on 1 web site. Every part of the template could be a page with its own controller. But there seems to be another problem with Tiles. The controllers used with Tiles are not configured as beans in the spring application context. The Tiles definition file contains the name of the controller class (controllerClass="...") and not a reference to a bean. Therefore the controllers cannot be configured as spring beans. Furthermore the controllers do not seem to be able to profit by command objects or validators.

    I'm not certain that the statements I wrote are true. In fact I hope that I misunderstood the whole thing and that there is an easy way to use more than 1 Spring MVC Controllers as components of a page without loosing the convenience of command objects/validators and so on.

    If you have a solution to my problem, please write it here or paste a link.

    Thanks a lot for your effort.

  • #2
    Can you explain why you want more than one controller per page?

    If you want to have lots of different "actions" chained together to produce a single output, then you can do this with one controller and using (for example) a CompositeCommand.

    If you literally want different parts of your page to come from different resources, then you can also do this either by using jsp imports.

    What problem are you trying to solve?


    • #3
      > Why do you want to have more than one controller per page?
      My pages consist of more than one dynamic elements. I'd like to separate the code of each of these elements. One controller that controlls more than one element isn't that beautifull. Imagine an element E that is in a corner of a 'user list page' and also at the bottom of 'project list page'. If I can't separate the logic of that reused element E the UserListFormController and the ProjectListFormController both would have to know about the logic of that element. UserListFormController and ProjectListFormController shouldn't even know that there is an additional element E on the site. They logic should be separated.

      So, I don't want to have one single controller with a composite command, if that is possible.

      > jsp imports
      I these resources which I import with jsp:include (or whatever) aren't just simple includes but real requests that pass the DispatcherServlet, it might work. This really might be the solution to my problem. Thanks for that hint, I guess I've been looking too far and didn't realize the answer was so close. I'm going to check that and will post what came out.

      > what problem are you trying to solve?
      I want to have Components. I'd like to be able to assemble my page. Each of these components that are going to be an element of that resulting page should be written according to the Spring MVC framework. Each of these components is totally separated and due to its separation able to be reused in more than one page.


      • #4
        I could be way off base here, but it seems like you should look at the Portlet spec. A link you might find useful:


        • #5

          I can't say much about SiteMesh as I haven't used it but I can recommend using Tiles as a way of 'componentising' your pages. As a fan of Tiles and, now, of Spring I spent a bit of time on my current project integrating the two and hopefully my experience will be of some help.

          The principle of integrating Tiles with Spring is very similar to that of Struts Actions with Spring. That is, all tile definitions use the same controllerClass which 'delegates' to a spring-managed instance of ControllerSupport.

          - Write your subclasses of ControllerSupport and configure them as spring-managed beans.

          - Create a controller class called, for example, DelegatingTileController. In execute (), obtain a reference* to the bean (e.g. via a WebApplicationContext) and invoke the bean's execute () method of the bean passing in tileContext, request, etc.

          - And that's it. You can now inject your DAOs into your tile controller. Nice

          * The first challenge is working out how to get the id of the bean because there is no way a tile controller knows the 'name' of it definition. I presume the Tiles developer thought we would never need to know!

          The easiest way would be to set the name of the bean as a tile-context attribute (e.g. <put name="beanName" direct="true" value="myTileControllerBean">) which DelegatingTileController would pick up.

          However, if you're looking for a bit more of a challenge it is possible for it to be bit more seemless. By subclassing I18nFactorySet and overriding the getDefinition () method you can get the 'name' of the definition, add it as a definition attribute which is then passed to the tile controller in the tileContext. explains how to install your own 'factoryClass' via the tilesConfigurer bean.

          Hope that helps,