ResourceBundleViewResolver使用属性文件中定义的视图bean来解析视图名称。 以下示例显示如何使用Spring Web MVC框架中的ResourceBundleViewResolver

ResourceBundleViewResolver-servlet.xml 配置如下所示 -

<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views" />
</bean>
XML

这里basename是指携带视图的资源束的名称。资源束的默认名称是views.properties,可以使用basename属性重写(也就是将views修改为其它名称,对应的views.properties文件名称也要修改)。

views.properties 配置如下所示 -

hello.(class)=org.springframework.web.servlet.view.JstlView
hello.url=/WEB-INF/jsp/hello.jsp
Shell

例如,使用上面的配置,如果URI

  • 对于/hello请求,DispatcherServlet会将请求转发到由views.properties中定义的hello对应的 hello.jsp 。
  • 这里“hello”是要匹配的视图名称。class指定视图类型,url是视图的位置。

首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:

  1. 创建一个名称为 ResourceBundleViewResolver 的动态WEB项目。
  2. 在 com.yiibai.springmvc 包下创建一个Java类HelloController
  3. jsp子文件夹下创建一个视图文件:hello.jsp
  4. src文件夹下创建属性文件views.properties
  5. 下载JSTL库jstl.jar。 把它放在 CLASSPATH 中。
  6. 最后一步是创建所有源和配置文件的内容并运行应用程序,详细如下所述。

完整的项目文件目录结构如下所示 -

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";
} }
Java

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>
XML

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>
HTML

完成创建源和配置文件后,发布应用程序到Tomcat服务器。

现在启动Tomcat服务器,当访问URL => http://localhost:8080/ResourceBundleViewResolver/hello , 如果Spring Web应用程序没有问题,应该看到以下结果:

Spring MVC资源绑定视图解析器的更多相关文章

  1. spring mvc: 资源绑定视图解析器(不推荐)

    spring mvc: 资源绑定视图解析器(不推荐) 不适合单控制器多方法访问,有知道的兄弟能否告知. 访问地址: http://localhost:8080/guga2/hello/index 项目 ...

  2. spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器)

    spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器) 资源绑定视图解析器 + 内部资源(普通模式)视图解析器 并存方式 内部资源视图解析器: http:// ...

  3. Spring MVC的多视图解析器配置及与Freemarker的集成

    一.从freemarker谈起 Freemarker使用模板技术进行视图的渲染.自从看了Struts标签.Freemarker.JSTL的性能对比后,我毅然决定放弃Struts标签了!效率太差…… S ...

  4. spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping

    spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...

  5. spring mvc: 属性方法名称解析器(多动作控制器)MultiActionController/ControllerClassNameHandlerMapping/PropertiesMethodNameResolver

    spring mvc: 属性方法名称解析器(多动作控制器) 加入控制器是StudentContrller.java,里面有3个方法 index,add,remove 那么访问地址是: http://l ...

  6. Spring Web MVC 多viewResolver视图解析器解决方案

    viewResolver的定义如下: public interface ViewResolver { View resolveViewName(String viewName, Locale loca ...

  7. Spring MVC-视图解析器(View Resolverr)-资源包视图解析器(Resource Bundle View Resolver)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_resourcebundleviewresolver.htm 说明:示例基于Spr ...

  8. Spring MVC参数方法名称解析器

    以下示例显示如何使用Spring Web MVC框架来实现多动作控制器的参数方法名称解析器. MultiActionController类可在单个控制器中分别映射多个URL到对应的方法. 所下所示配置 ...

  9. Spring MVC属性方法名称解析器

    以下示例显示如何使用Spring Web MVC框架来实现多动作控制器的属性方法名称解析器. MultiActionController类可在单个控制器中分别映射多个URL到对应的方法. 所下所示配置 ...

随机推荐

  1. C# 7 新特性-2

    在之前的C# 7 新特性博客中,我们谈到了Tuples,Record Type和Pattern Matching.这些都是C#新特性中最可能出现的.在本博客中,我们会提到更多的一些特性,虽然这些特性不 ...

  2. sqlserver 获取系统用户表结构信息

    SELECT (case when a.colorder=1 then d.name else null end) 表名, a.colorder 字段序号,a.name 字段名, (case when ...

  3. Java 图片添加水印效果

    package com.xiaowu.drawwater.demo; import java.awt.AlphaComposite; import java.awt.Graphics2D; impor ...

  4. 一起來玩鳥 Starling Framework(2)效能測試以及Image與Texture

    上一篇我們放了一個Quad與TextField在舞台上慢慢轉.眼尖的可能會發現轉起來邊緣有點鋸齒,這可以透過設定Starling的反鋸齒來解決,在Main.as裡,新增了_starling之後,可以加 ...

  5. scrapy-splash抓取动态数据例子十

    一.介绍 本例子用scrapy-splash抓取活动行网站给定关键字抓取活动信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...

  6. JsonFX 序列化反序列化

    /// <summary> /// Json 序列化工具类 /// </summary> public class SerializeJsonUtility { /// < ...

  7. 聚合数据全国天气预报API--ajax 通过城市名取数据

    聚合数据天气预报API:https://www.juhe.cn/docs/api/id/39 接口地址:http://v.juhe.cn/weather/index 支持格式:json/xml 请求方 ...

  8. python判断文件和文件夹是否存在、创建文件夹

    >>> import os >>> os.path.exists('d:/assist') True >>> os.path.exists('d: ...

  9. 通过 react-native-keyboard-aware-scroll-view 解决键盘遮盖输入框的问题

    1.安装 yarn add react-native-keyboard-aware-scroll-view 2.引入 import { KeyboardAwareScrollView } from ' ...

  10. js bind 绑定this指向

    1.示例代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UT ...