码迷,mamicode.com
首页 > 编程语言 > 详细

Spring MVC 基础篇 6

时间:2016-06-30 01:03:36      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

Spring MVC 视图解析器

1.请求直接进入页面,不经过Controller

<!-- 配置直接转发的页面,请求直接进入页面,而无需再经过Controller方法 -->
<mvc:view-controller path="/success" view-name="success"/>
<!-- 在实际开发中通常都需要配置 mvc:annotation-driven 标签 -->
<mvc:annotation-driven></mvc:annotation-driven>

2.访问页面静态资源

<!--  配置访问静态资源 start -->
<mvc:resources mapping="/js/**" location="/resources/js/" />
<mvc:resources mapping="/css/**" location="/resources/css/" />
<mvc:resources mapping="/images/**" location="/resources/images/" />
<mvc:resources mapping="/upload/**" location="/upload/" />
<mvc:resources mapping="/**/*.html" location="/"/>
<!--<mvc:default-servlet-handler /> -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置访问静态资源 end -->

3.自定义视图

/**
 * 1.自定义视图实现view接口,重写对于的方法 ,然后将自定义view加入到Spring IoC容器中
 * 2.Controller中返回 自定义view在Spring容器中的name
 * 3.剩下的操作由render和getContentType实现
 *
 */
@Component
public class SelfView implements View{

    public String getContentType() {
        return "text/html";
    }

    public void render(Map<String, ?> model, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        response.getWriter().print("hello view ,time:" + new Date());
    }

}
    @RequestMapping("testView")
    public String testView() {
        System.out.println("Hello this is testView");
        return "selfView";
    }
    
    <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 配置BeanName对应的视图解析器  order为该视图优先级  随便给定一个值就行   都比InternalResourceViewResolver优先级要低 -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
         <property name="order" value="100"></property>
    </bean>

 

Spring MVC 基础篇 6

标签:

原文地址:http://www.cnblogs.com/booth-sun/p/5628678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!