• 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>
    
    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应用程序没有问题,应该看到以下结果:

  • 相关阅读:
    SEO分享:我为什么会有这么多的优质外链资源?
    执行shell脚本提示“syntax error near unexpected token for((i=0;i&lt;$length;i++))”
    Codeforces Round #254 (Div. 2)D(预计)
    自己写配置文件
    软件測试基本方法(二)之白盒測试
    hdu 4638 Group
    影视集结号--首页
    2015阿里巴巴秋招在线笔试题
    php 抓取天气情况 www.weather.com.cn
    C语言中的enum(枚举)使用方法
  • 原文地址:https://www.cnblogs.com/borter/p/9519682.html
Copyright © 2020-2023  润新知