(csdn内转载)spring学习笔记:配置视图解析器ViewResolver

我们在controller里面经常这样return一个ModelAndView: return new ModelAndView('user', 'model', model);

DispatcherServlet靠ViewResolver把user解析为/WEB-INF/jsp/user.jsp:

常用的ViewResolver:

InternalResourceViewResolver:

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
     <property name="prefix" value="/WEB-INF/jsp/" />  
     <property name="suffix" value=".jsp" />  
    </bean>  


其实InternalResourceViewResolver的工作很简单: 在视图逻辑名前面加上prefix,后面加上suffix;

ResourceBundleViewResolver:把视图逻辑名和真实文件的映射关系放在配置文件中.

    <bean id="myViewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">  
        <property name="order" value="1"/>  
        <property name="basename" value="views"/>  
    </bean>  


因为可以配置多个viewResolver,所以上面的resolver还有一个选项order, 标明优先级。

views.properties放在classpath下面,内容大约是这样的:

userDetail.class=org.springframework.web.servlet.view.JstlView
userDetail.url=/WEB-INF/jsp/userDetail.jsp

userList.class=org.springframework.web.servlet.view.JstlView
userList.url=/WEB-INF/jsp/userList.jsp
......

我们可以在controller里面这样返回一个ModelAndView:
return new ModelAndView("userDetail","model", model);


该博文转载自http://blog.csdn.net/sunxing007/article/details/4584979

你可能感兴趣的:((csdn内转载)spring学习笔记:配置视图解析器ViewResolver)