Announcement Announcement Module
Collapse
No announcement yet.
RFE? Create fixed ModelAndView in XML Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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;[email protected]&#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
    Whooops

    Sorry meant this to go in the MVC forum.

    Can an admin move it over there?

    Comment


    • #3
      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?

      Comment


      • #4
        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.

        Comment


        • #5
          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,

          Comment

          Working...
          X