Announcement Announcement Module
No announcement yet.
How to map views Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to map views


    How do I configure views via the xxx-servlet.xml file?

    For example, say I am making a web-app for inventory control of a store which sells "books", "magazines", and "music CDs". Each of these items has its own set of controllers and its own views (JSPs). I want the JSPs to be in a sub-directory structure under WEB-INF, with the "book" views, and the "magazine" views in separate sub-directories etc.

    But the only example "view resolver" configuration I have seen allows only for the JSPs to all sit in the same directory - and I can't work out how to configure resolving to different locations.

    Eg. I have seen this sort of view resolver configuration for InternalResourceViewResolver. But all the JSPs need to sit in "/WEB-INF/jsp/", and I can't see how to have some views in subdirectories of this.

    <property name="viewClass">
    <value>org.springframework.web.servlet.view.JstlVi ew</value>
    <property name="prefix">
    <property name="suffix">

    Thanks for any help,

  • #2
    You could use the ResourceBundleViewResolver, which allows you to specify the URL and a view class in a properties file instead of only providing a suffix and a prefix.

    Of course, you could also return "bla/bla.jsp" as the view name.



    • #3
      Thanks for the reply.

      I guess your second response is along the lines of having a controller return a ModelAndView with a view name like "books/bookEdit" or "books/showBook" etc?

      I would rather have the controller just return for example "bookEdit", and have configuration decide exactly what JSP that maps to. Is that only possible by using a ResourceBundleViewResolver? Are there examples of how to use this view-resolver?



      • #4
        You can use InternalResourceViewResolver - it'll take bookEdit (the logical view name you return) and resolve that to /${prefix}/bookEdit.${suffix}, for example /WEB-INF/foo/bar/bookEdit.jsp.



        • #5
          But using InternalResourceViewResolver and "prefix" and "suffix" format, doesn't that mean the all JSPs will be mapped to the same "prefix"?

          I want different prefixes depending on the JSP/controller.

          I can get it to work by returning a ModelAndView from my "Controller" class with for example
          return new ModelAndView("books/showBook", "data", data);
          return new ModelAndView("magazines/showMagazine", "data", data);

          but if I want to have an "edit form" I use SimpleFormController, and I get problems because I can't find out how to indicate the JSP with a path - I use formBackingObject to generate the data for the form, but this doesn't seem to allow me to indicate a path to the JSP.