Spring Dynamically View MVC + Handle Non-Existing Views

I am going to implement a Spring MVC controller that uses a dynamic view passed as a parameter for viewing:

@Controller @RequestMapping("/page") public class PageController { @RequestMapping(value = "/{page}", method = {RequestMethod.GET}) public ModelAndView page(@PathVariable("page")String page) { System.out.println("page = " + page); return new ModelAndView(page); } } 

Solutions allowed by UrlBasedViewResolver:

 <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> <property name="cache" value="false"/> </bean> 

Is there a way to return the default view in the absence of a nonexistent view? Perhaps check if there is a view, then return new ModelAndView('requested_page_not_found') ?

+1
source share
1 answer

Not with UrlBasedViewResolver and friends. Once the chain has reached this, you will complete - if the view does not exist , you will be directed to all 404 pages configured by your container.

Note. When linking ViewResolvers, UrlBasedViewResolver should always be the last, as it will try to resolve any view name, regardless of whether the main resource exists.

+1
source

All Articles