zoukankan      html  css  js  c++  java
  • SpringMVC 视图解析器

    SpringMVC 视图解析器

    还记得SpringMVC 快速入门中,dispatcher-servlet.xml 配置的视图解析器么。它是SpringMVC 的核心知识点。本章节比较简单,明白视图解析器的工作原理,然后配置自定义的视图解析器和使用重点向跳转页面。

    SpringMVC的配置文件,dispatcher-servlet.xml。这里配置了直接跳转的页面 mvc:view-controller 即不经过Controller层,直接根据视图解析器跳转页面。还配置了视图解析器 BeanNameViewResolver 优先级为666。其中jsp最为常见的解析器是 InternalResourceViewResolver 器优先级是int类型的最大值。也就是说优先级最低。

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
      
        <!-- 配置自定扫描的包 -->  
        <context:component-scan base-package="com.itdragon.springmvc" />  
      
        <!-- 配置视图解析器 -->  
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
            <property name="prefix" value="/WEB-INF/views/"></property>  
            <property name="suffix" value=".jsp"></property>  
        </bean>  
          
        <!-- 配置注解驱动 -->  
        <mvc:annotation-driven />  
      
        <!-- 配置视图  BeanNameViewResolver 解析器  
            使用视图的名字来解析视图   
            通过 order 属性来定义视图解析器的优先级, order 值越小优先级越高  
        -->  
        <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">  
            <property name="order" value="666"></property>  
        </bean>  
          
        <!-- 配置直接跳转的页面,无需经过Controller层    
            http://localhost:8080/springmvc/index   
            然后会跳转到 WEB-INF/views/index.jsp 页面  
        -->  
        <mvc:view-controller path="/index" view-name="index"/>  
    </beans>  
    

    自定义CustomViewResolver java类 实现 View 接口。

    import java.util.Map;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
    import org.springframework.stereotype.Component;  
    import org.springframework.web.servlet.View;  
      
    @Component  
    public class CustomViewResolver implements View{  
      
        public String getContentType() {  
            return "text/html";  
        }  
      
        public void render(Map<String, ?> model, HttpServletRequest request,  
                HttpServletResponse response) throws Exception {  
            response.setCharacterEncoding("utf-8");  
            response.setContentType("text/html;charset=utf-8");  
            response.getWriter().print("CustomViewResolver order 越小优先级越高! 所以优先于 InternalResourceViewResolver");  
        }  
      
    }  
    

    语法知识点说明类 ViewResolverController

    import org.springframework.stereotype.Controller;  
    import org.springframework.web.bind.annotation.RequestMapping;  
      
    @Controller  
    public class ViewResolverController {  
          
        @RequestMapping("/testRedirect")  
        public String testRedirect() {  
            System.out.println("^^^^^^^^^^^^^^^^^^^^^重定向到apistudy.jsp页面,地址栏URL改变");  
            return "redirect:apiStudy/testModelAndView";  
        }  
          
        @RequestMapping("/testForward")  
        public String testForward() {  
            System.out.println("^^^^^^^^^^^^^^^^^^^^^转发到apistudy.jsp页面,地址栏URL不变");  
            return "forward:apiStudy/testModelAndView";  
        }  
          
        @RequestMapping("/testCustomViewResolver")  
        public String testCustomViewResolver() {  
            System.out.println("^^^^^^^^^^^^^^^^^^^^^进入到自定义的视图解析器中,返回值必须是类名首字母小写");  
            return "customViewResolver";  
        }  
      
    }  
    

    WEB-INF/views/index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"  
        pageEncoding="UTF-8"%>  
          
    <!DOCTYPE>  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>SpringMVC 快速入门</title>  
    </head>  
    <body>  
          
        <h2>视图解析器</h2>  
        <a href="testRedirect">Test Redirect</a>  
        <br/><br/>  
        <a href="testForward">Test Forward</a>  
        <br/><br/>  
        <a href="testCustomViewResolver">Test Custom View Resolver</a>  
        <br/><br/>  
          
    </body>  
    </html>  
    

    运行的效果图

    们装配成ModelAndView对象,
    然后,借助视图解析器 ViewResolver,得到最终的逻辑视图对象View。最终的物理视图可以是jsp,excel,表单 等各种表现形式的视图。

    到这里SpringMVC 的视图解析器就介绍完了,下一章是重难点,SpringMVC Form表单的crud操作。

  • 相关阅读:
    Qt进程间通信
    reinterpret
    vs调试技巧
    利用QSystemSemaphore和QSharedMemory实现进程间通讯
    QLocalSocket
    QShareMemory
    qt动态库实现无边框窗体的消息处理 nativeEvent的使用
    BCB6常用快捷键
    1219个人总结
    冲刺二 12.6
  • 原文地址:https://www.cnblogs.com/itdragon/p/7718143.html
Copyright © 2011-2022 走看看