PDA

View Full Version : How to map views



pak
Sep 2nd, 2004, 03:02 AM
Hi,

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.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>


Thanks for any help,
Peter

Alef Arendsen
Sep 2nd, 2004, 07:37 AM
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.

Alef

pak
Sep 3rd, 2004, 02:14 AM
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?

Thanks,
Peter

Keith Donald
Sep 4th, 2004, 10:45 AM
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.

Keith

pak
Sep 8th, 2004, 02:26 AM
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);
or
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.