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操作。
SpringMVC 视图解析器的更多相关文章
- SpringMVC视图解析器
SpringMVC视图解析器 前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视 图解析器.当我们对SpringMVC控制的资源发起 ...
- SpringMVC视图解析器(转)
前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视图解析器.当我们对SpringMVC控制的资源发起请求时,这些请求都会被Sprin ...
- springmvc 之 SpringMVC视图解析器
当我们对SpringMVC控制的资源发起请求时,这些请求都会被SpringMVC的DispatcherServlet处理,接着Spring会分析看哪一个HandlerMapping定义的所有请求映射中 ...
- SpringMVC视图解析器概述
不论控制器返回一个String,ModelAndView,View都会转换为ModelAndView对象,由视图解析器解析视图,然后,进行页面的跳转. 控制器处理方法---->ModelAndV ...
- springMVC视图解析器——InternalResourceViewResolver(转)
springmvc在处理器方法中通常返回的是逻辑视图,如何定位到真正的页面,就需要通过视图解析器. springmvc里提供了多个视图解析器,InternalResourceViewResolver就 ...
- 深入SpringMVC视图解析器
ViewResolver的主要职责是根据Controller所返回的ModelAndView中的逻辑视图名,为DispatcherServlet返回一个可用的View实例.SpringMVC中用于把V ...
- SpringMVC 视图解析器 InternalResourceViewResolver
我们在使用SpringMVC的时候,想必都知道,为了安全性考虑,我们的JSP文件都会放在WEB-INF下, 但是我们在外部是不可以直接访问/WEB-INF/目录下的资源对吧, 只能通过内部服务器进行转 ...
- SpringMvc 视图解析器常见功能、类型转换、格式化
springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- SSM-SpringMVC-05:SpringMVC视图解析器InternalResourceViewResolver配置
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 视图解析器------默认就有配置,但是默认的在实际使用过程中有很多不方便的地方,所以我们配置一道视图解析器 ...
随机推荐
- 【特效】手机端仿美团下拉菜单带遮罩层html+css+jquery
写了一个手机端的下拉菜单,类似美团,用相对单位rem写的. 效果截图: 代码很简单,原理有点类似嵌套的选项卡,其中的难点在于弹出下拉菜单后,出现黑色半透明遮罩层,而且下层列表页面禁止滚动了.关键就是给 ...
- win10 UWP 你写我读
想要电脑读出我们写的内容,在win10,很简单 其实这个技术在windows7就有了,但是现在win10让写出一个你写我读的软件很简单. 我们需要一个类MediaElement来播放,因为window ...
- 博客发在oschina
国内git.oschina做的很好,看到oschina还有博客 直接导入csdn博客 http://my.oschina.net/lindexi/blog
- 在centos6编译配置httpd2.4的N种方法
前言 我们使用linux的过程中,一定会用到httpd这个服务,在centos7上,默认安装的httpd就是2.4版本,大家都知道,2.4版本相对之前的版本已经做了改进,用起来更加方便,但是我们的ce ...
- HTTP 错误 500.19 Internal Server Error的解决方法
第一种可能,能解决一部分问题 http://wenku.baidu.com/view/c5cb4a08bb68a98271fefa3f.html 第二种可能,解决另外一部分问题 经过检查发现是由于先安 ...
- MySQL锁类型以及子查询锁表问题、解锁
MySQL中select * for update锁表的范围 MySQL中select * for update锁表的问题 由于InnoDB预设是Row-Level Lock,所以只有「明确」的指定主 ...
- 通过 ODBC 访问数据库获取数据集
Step1:(window 中完成): 控制面板/管理工具/ODBC 数据源/用户 Step2:(window 中完成): 添加/SQL Server Step3:(window 中完成): 自己定义 ...
- 深度揭秘ES6代理Proxy
最近在博客上看到关于ES6代理的文章都是一些关于如何使用Proxy的例子,很少有说明Proxy原理的文章,要知道只有真正掌握了一项技术的原理,才能够写出精妙绝伦的代码,所以我觉得有必要写一篇关于深刻揭 ...
- 如何用php写app接口[原创]
人生就如一列永不停止的列车,no one knows when or where to stop.总有那些美好,值得永远怀念.也总有那些希望,值得你无怨无悔的付出,追逐.去年年底带着女儿一起坐火车会湖 ...
- 1031: [JSOI2007]字符加密Cipher
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7338 Solved: 3182[Submit ...