Spring MVC资源绑定视图解析器
ResourceBundleViewResolver使用属性文件中定义的视图bean来解析视图名称。 以下示例显示如何使用Spring Web MVC框架中的ResourceBundleViewResolver。
ResourceBundleViewResolver-servlet.xml 配置如下所示 -
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views" />
</bean>
这里basename是指携带视图的资源束的名称。资源束的默认名称是views.properties,可以使用basename属性重写(也就是将views修改为其它名称,对应的views.properties文件名称也要修改)。
views.properties 配置如下所示 -
hello.(class)=org.springframework.web.servlet.view.JstlView
hello.url=/WEB-INF/jsp/hello.jsp
例如,使用上面的配置,如果URI:
- 对于
/hello请求,DispatcherServlet会将请求转发到由views.properties中定义的hello对应的hello.jsp。 - 这里“
hello”是要匹配的视图名称。class指定视图类型,url是视图的位置。
首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:
- 创建一个名称为 ResourceBundleViewResolver 的动态WEB项目。
- 在
com.yiibai.springmvc包下创建一个Java类HelloController。 - 在
jsp子文件夹下创建一个视图文件:hello.jsp。 - 在
src文件夹下创建属性文件views.properties。 - 下载JSTL库jstl.jar。 把它放在
CLASSPATH中。 - 最后一步是创建所有源和配置文件的内容并运行应用程序,详细如下所述。
完整的项目文件目录结构如下所示 -

HelloController.java 的代码如下所示 -
package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello, Spring MVC资源绑定视图解析器!");
return "hello";
}
}
ResourceBundleViewResolver-servlet.xml 配置如下所示 -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.yiibai" />
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views" />
</bean>
</beans>
views.properties 配置如下所示 -
hello.(class)=org.springframework.web.servlet.view.JstlView
hello.url=/WEB-INF/jsp/hello.jsp
hello.jsp 的代码如下所示 -
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
完成创建源和配置文件后,发布应用程序到Tomcat服务器。
现在启动Tomcat服务器,当访问URL => http://localhost:8080/ResourceBundleViewResolver/hello , 如果Spring Web应用程序没有问题,应该看到以下结果:

Spring MVC资源绑定视图解析器的更多相关文章
- spring mvc: 资源绑定视图解析器(不推荐)
spring mvc: 资源绑定视图解析器(不推荐) 不适合单控制器多方法访问,有知道的兄弟能否告知. 访问地址: http://localhost:8080/guga2/hello/index 项目 ...
- spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器)
spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器) 资源绑定视图解析器 + 内部资源(普通模式)视图解析器 并存方式 内部资源视图解析器: http:// ...
- Spring MVC的多视图解析器配置及与Freemarker的集成
一.从freemarker谈起 Freemarker使用模板技术进行视图的渲染.自从看了Struts标签.Freemarker.JSTL的性能对比后,我毅然决定放弃Struts标签了!效率太差…… S ...
- spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping
spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...
- spring mvc: 属性方法名称解析器(多动作控制器)MultiActionController/ControllerClassNameHandlerMapping/PropertiesMethodNameResolver
spring mvc: 属性方法名称解析器(多动作控制器) 加入控制器是StudentContrller.java,里面有3个方法 index,add,remove 那么访问地址是: http://l ...
- Spring Web MVC 多viewResolver视图解析器解决方案
viewResolver的定义如下: public interface ViewResolver { View resolveViewName(String viewName, Locale loca ...
- Spring MVC-视图解析器(View Resolverr)-资源包视图解析器(Resource Bundle View Resolver)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_resourcebundleviewresolver.htm 说明:示例基于Spr ...
- Spring MVC参数方法名称解析器
以下示例显示如何使用Spring Web MVC框架来实现多动作控制器的参数方法名称解析器. MultiActionController类可在单个控制器中分别映射多个URL到对应的方法. 所下所示配置 ...
- Spring MVC属性方法名称解析器
以下示例显示如何使用Spring Web MVC框架来实现多动作控制器的属性方法名称解析器. MultiActionController类可在单个控制器中分别映射多个URL到对应的方法. 所下所示配置 ...
随机推荐
- oracle表空间操作语句
1.查看所有表空间及表空间大小: select tablespace_name ,sum(bytes) / 1024 / 1024 as MB from dba_data_files group by ...
- Android 多线程之HandlerThread 完全详解
关联文章: Android 多线程之HandlerThread 完全详解 Android 多线程之IntentService 完全详解 android多线程-AsyncTask之工作原理深入解析(上) ...
- iOS: 格式化新浪微博/QQ说说等等的发布时间
介绍:对于一些社交工具,我们可以发布一些说说或者心情什么的,如新浪微博,QQ,微信等,发布成功后,上面都会有一个发布的时间. 这个时间并不是具体的NSDate类型,而是经过格式化过的符合一般标准的模式 ...
- 算法导论-求x的n次方
目录 1.分治求x的n次方思路 2.c++代码实现 内容 1.分治求x的n次方思路T(n)=Θ(lgn) 为了计算乘方数a^n,传统的做法(所谓的Naive algorithm)就是循环相乘n次,算法 ...
- Shortest Path [3]
-----------应要求删除---------------
- TestNG+Maven+IDEA环境搭建
TestNG+Maven+IDEA环境搭建 前言: 主要进行TestNG测试环境的搭建 所需环境: 1.IDEA UItimate 2.JDK 3.Maven 一.创建工程 File –>new ...
- 在FASTBuild中使用Caching
上一篇:初识FASTBuild 在FASTBuild中使用缓存只需要注意三个环节: 一.设置编译选项 对于GCC\SNC\Clang编译器,没有特殊的要求 对于MSVC编译器,必须设置/Z7调试模式. ...
- NodeJS实战——创建基础应用并应用模板引擎
本次的目的是搭建一个最基础忽地可以实现功能的NodeJSserver,可以体现出NodeJS的工作流程以及开发的基本框架. 需求:已经安装了nodejs以及express. 一.构建基础的NodeJS ...
- ColorSchemer Studio 2 破解
软件介绍: ColorSchemer Studio 2 is a professional color matching application for anyone from hobbyists t ...
- 【Python3 爬虫】08_正则表达式(元字符与语法)
元字符表 符号 说明 示例 . 表示任意字符 'abc' >>>'a.c' >>>结果为:'abc' ^ 表示字符开头 'abc' >>> ...