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>

运行的效果图


视图解析器工作流程:

首先,不管目标方法的返回值是 String, Map, ModelMap, Model, 还是 ModelAndView。 SpringMVC 都会在内部将他们装配成ModelAndView对象,
然后,借助视图解析器 ViewResolver,得到最终的逻辑视图对象View。最终的物理视图可以是jsp,excel,表单 等各种表现形式的视图。


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

原文地址:http://blog.csdn.net/qq_19558705/article/details/49930719




  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值