Results 1 to 5 of 5

Thread: RFE? Create fixed ModelAndView in XML

  1. #1
    Join Date
    Jul 2005
    Location
    UK
    Posts
    29

    Default RFE? Create fixed ModelAndView in XML

    Sometimes it is useful to be able to create a fixed ModelAndView instance in a bean context, with populated model data - after all if there is no code required to populate the model why not just do it in the context config?

    For example:

    Code:
        <bean name="view.pleasewait" class="org.springframework.somepackage.ModelAndView">
            <property name="viewName" value="pleasewait"/>
            <property name="model">
                <map>
                    <entry key="message"><value>Please wait, updating ...</value></entry>
                    <entry key="refreshURL"><value>/some/completion/page</value></entry>
                    <entry key="delay" value="3"/>
                </map>
            </property>
        </bean>
    However this is not possible as far as I can tell because ModelAndView protects its Model map, so it would need a ModelAndViewBean approach to act as a factory for the model. This would be very useful for passing complete static ModelAndView instances to controllers.

    An alternative, as we only need this currently for a fixed view that is returned by a fixed controller, I implemented a FixedViewController as below:


    Code:
        <bean name="controller.pleasewait" class="yourcorp.pkg.FixedViewController">
            <property name="viewName" value="pleasewait"/>
            <property name="model">
                <map>
                    <entry key="message"><value>Please wait, updating ...</value></entry>
                    <entry key="refreshURL"><value>/some/completion/page</value></entry>
                    <entry key="delay" value="3"/>
                </map>
            </property>
        </bean>



    Code:
    package yourcorp.pkg;
    
    import org.springframework.web.servlet.mvc.ParameterizableViewController;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.Map;
    
    /**
     * A controller that returns a ModelAndView that can
     * be set in the configuration to a specific fixed viewName
     * and a fixed model Map. Property modelName can specify a name for the
     * model used in the view.
     * @author mpalmer &#40;marc@anyware.co.uk&#41;
     */
    public class FixedViewController extends ParameterizableViewController
    &#123;
        private Map model;
    
        private String modelName;
    
        public Map getModel&#40;&#41;
        &#123;
            return model;
        &#125;
    
        public void setModel&#40;Map model&#41;
        &#123;
            this.model = model;
        &#125;
    
        public String getModelName&#40;&#41;
        &#123;
            return modelName;
        &#125;
    
        public void setModelName&#40;String modelName&#41;
        &#123;
            this.modelName = modelName;
        &#125;
    
        protected ModelAndView handleRequestInternal&#40;HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse&#41; throws Exception
        &#123;
            if &#40;modelName == null&#41;
            &#123;
                return new ModelAndView&#40; getViewName&#40;&#41;, model&#41;;
            &#125;
            else
            &#123;
                return new ModelAndView&#40; getViewName&#40;&#41;, modelName, model&#41;;
            &#125;
        &#125;
    &#125;
    It would of course be easy (and nicer) to adapt this so that it uses a ModelAndView as a property, and write a ModelAndViewFactoryBean.

    If anybody else thinks this is worthwhile I can lodge it as an RFE in Jira and contribute the code.

  2. #2
    Join Date
    Jul 2005
    Location
    UK
    Posts
    29

    Default Whooops

    Sorry meant this to go in the MVC forum.

    Can an admin move it over there?

  3. #3
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    You can specify static attributes in a view in the XML (or property file) definition if your View class inherits from AbstractView:
    Code:
    <bean id="staticView" class="some.View">
      <property name="staticAttributes">
        <value>
          message=Please wait, updating...,refreshURL=/some/completion/page,delay=3
        </value>
      </property>
    </bean>
    From your controller, just return the view with no associated model..
    Code:
    public ModelAndView handleRequestInternal&#40;...&#41; &#123;
      ...
      return new ModelAndView&#40;"staticView"&#41;;
    &#125;
    Would that not do what you wanted?
    Darren Davison.
    Public Key: 0xE855B3EA

  4. #4
    Join Date
    Jul 2005
    Location
    UK
    Posts
    29

    Default

    I don't think so - I want to create a full ModelAndView not just a View in the context.

    That way the ModelAndView can be assigned to any controllers that just return a fixed result that uses fixed model data.

  5. #5
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    you mean inject the MAV into a controller so it simply returns it when called? Why would you ever need to do that - if the model and view remain constant, you just need to create one controller that returns what you want and map multiple URL's to it.

    If you still really want to do it in config, use constructor args to set the name of the view and the model map.

    Am I missing something about what you're trying to do still?

    Regards,
    Darren Davison.
    Public Key: 0xE855B3EA

Similar Threads

  1. Replies: 4
    Last Post: Sep 27th, 2005, 11:31 PM
  2. simpleformcontroller & modelandview
    By yuer2084 in forum Web
    Replies: 2
    Last Post: Jul 27th, 2005, 04:07 AM
  3. How do you create unit tests for layered applications?
    By paul.barry in forum Architecture
    Replies: 2
    Last Post: May 6th, 2005, 08:31 AM
  4. How to create hierarchical context
    By Lock51 in forum Container
    Replies: 4
    Last Post: Mar 24th, 2005, 02:30 PM
  5. Strange Data Access Error
    By webifyit in forum Data
    Replies: 2
    Last Post: Dec 28th, 2004, 11:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •