SpringMVC学习笔记——视图解析器

流程

SpringMVC会将目标方法的返回值(StringModelAndViewView)转换成ModelAndView,再通过ViewResolver解析成视图对象(JSP、JSTL、PDF)

JSTL

  • 若项目使用了JSTL,则SpringMVC会自动把视图由InternalResourceView转为JstlView
  • 若使用JSTL的fmt标签则需要在SpringMVC的配置文件中配置国际化资源文件
    在项目没有配置JSTL和standard的环境下,view会被解析成InternalResourceView,当Maven加入如下配置后,SpringMVC会将视图解析成JstlView类型。

         <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>
    
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

    原先的View类型:
    这里写图片描述
    配置Jstl后的View类型:
    这里写图片描述

配置国际化资源文件

  1. 首先在classpath下,创建两个国际化资源文件。
    i18n_en_US.properties
i18n.username=Username
i18n.password=Password

i18n_zh_CN.properties
properties
i18n.username=用户名
i18n.password=密码

2. 在dispatcherServlet-servlet.xml中配置一个bean,去加载资源文件

<?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.shen.springmvc"></context:component-scan>

    <!-- 配置视图解析器:如何把 handler 方法返回值解析为实际的物理视图 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 配置国际化资源文件 -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n">
        </property>
    </bean>
</beans>

最后一个bean负责去加载国际化资源文件,class为ResourceBundleMessageSource

前端显示

编写国际化代码:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h4>Page success !</h4>
    time : ${requestScope.time}
    <br></br>

    names:${requestScope.names }
    <br></br>

    request user:${requestScope.user }
    <br></br>

    session user:${sessionScope.user }
    <br></br>

    request schoole:${requestScope.schoole }
    <br></br>

    session schoole:${sessionScope.schoole }
    <br></br>

    user user:${requestScope.user}
    <br></br>
    <!-- 国际化代码 -->
    <fmt:message key="i18n.username"></fmt:message>
    <br></br>
    <fmt:message key="i18n.password"></fmt:message>
    <br></br>
</body>
</html>

直接响应转发界面(mvc:view-controller)

在dispatcherServlet-servlet.xml增加如下配置:

    <!-- 配置直接转发的页面 -->
    <!-- 可以直接响应转发的页面,而无需再经过 Handler处理 -->
    <mvc:view-controller path="/success" view-name="success" />

这样就可以绕过Handler直接响应目标页面,如http://localhost:8080/springMVC1/success

mvc:annotation-driven

但此时@RequestMapping注解的URL,全部报404错误。
解决方案,在dispatcherServlet-servlet.xml增加如下配置:

    <!-- 在实际开发中,通常都需要配置 mvc:annotation-driven 标签 -->
    <mvc:annotation-driven></mvc:annotation-driven>

增加后,直接响应转发界面和经过Handler方法转发的视图都可以响应。

自定义视图

若要整合Excel等其它功能的视图,可以通过自定义视图完成。比如Excel,SpringMVC为我们提供了AbstractView或AbstractJExcel View的抽象类,实现buildExcelDocument方法,就可以轻松展示Excel数据。
1. 编写自定义的View类,该类实现了View接口,示例代码如下:

@Component
public class HelloView implements View {
    @Override
    public String getContentType() {
        return "text/html";
    }

    @Override
    public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.getWriter().print("hello view , time :" + new Date());
    }
}
  1. 配置视图解析器(BeanNameViewResolver)
    dispatcherServlet-servlet.xml中增加如下配置:
    <!-- 配置视图 BeanNameViewResolver 解析器: 使用视图的名字来解析视图 -->
    <!-- 通过 order 属性来定义视图解析器的优先级,order 值越小优先级越高,默认为Interger.MAX_VALUE -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
        <property name="order" value="100"></property>
    </bean>

这里写图片描述
(1)BeanNameViewResolver将会执行getBean方法,所以要将定义的View标识为一个Bean,这里使用@Component
(2)定义ViewResolver的优先级,通过属性order,值越小优先级越高,默认为Integer.MAX_VALUE。

重定向

之前做的所有操作都是转发操作,如何进行重定向?
在使用@RequestMapping的目标方法中,之前都是使用字符串进行返回,在这里这个字符串可以增加前缀redirect:,forward:,可以实现进行重定向转发
重定向示例代码如下:

    @RequestMapping("/testRedirect")
    public String testRedirect() {
        System.out.println("testRedirect");
        return "redirect:/index.jsp";
    }

猜你喜欢

转载自blog.csdn.net/u012525096/article/details/81325876