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. Debian6 安装Kscope(也适用于Ubuntu)

    参考:http://soft.chinabyte.com/os/134/12307634.shtml kscope1.6.2在这里下载,下载后解压出kscope-1.6.2.tar.gz. 在ubun ...

  2. android多线程-AsyncTask之工作原理深入解析(下)

    关联文章: Android 多线程之HandlerThread 完全详解 Android 多线程之IntentService 完全详解 android多线程-AsyncTask之工作原理深入解析(上) ...

  3. GyoiThon:基于机器学习的渗透测试工具

    简介 GyoiThon是一款基于机器学习的渗透测试工具. GyoiThon根据学习数据识别安装在Web服务器上的软件(操作系统,中间件,框架,CMS等).之后,GyoiThon为已识别的软件执行有效的 ...

  4. 用coffeescript实现类java的Map类

    class Map constructor : -> @entry = {} @count = 0 size : -> return @count isEmpty : -> retu ...

  5. 安装Scala 找不到或无法加载主类 scala.tools.nsc.MainGenericRunner 错误

    对于安装Scala时 找不到或无法加载主类 scala.tools.nsc.MainGenericRunner 错误,不管是linux还是window系统,原因很大可能是scala的安装路径中出现空格 ...

  6. Jquery获取当前行的数据

    取表格当前行数据js代码: Java代码 $(function() { $(".myclass").each(function(){     var tmp=$(this).chi ...

  7. HTML5 Canvas 绘制库存变化折线 增加超储告罄线

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...

  8. 剑指Offer-正则表达式匹配(Python)

    1 题干内容 请实现一个函数用来匹配包括.和*的正则表达式.模式中的字符.表示任意一个字符,而*表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整个模式. 例如,字 ...

  9. lucene: IO/FileNotFoundException:(Too many open files) 查询异常解决

    http://stackoverflow.com/questions/6210348/too-many-open-files-error-on-lucene   baidu zone - 为什么Luc ...

  10. Scrapy:虎牙爬取,图片存储与数据分析

    第一次爬取虎牙主播数据,有点小激动 1.共批量爬取的101个主播的,包括 头像 主播名字 房间号 房间链接 2.数据规整部分,需要将json数据加载到pandas的Dataframe,只显示的前15个 ...