Spring实战之ServletContextResource访问资源文件示例

本文实例讲述了Spring实战之ServletContextResource访问资源文件。分享给大家供大家参考,具体如下:

一 视图

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ page import="org.springframework.web.context.support.ServletContextResource"%>
<%@ page import="org.dom4j.*,org.dom4j.io.*,java.util.*"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>测试ServletContextResource</title>
</head>
<body>
<h3>测试ServletContextResource</h3>
<%
// 从Web Context下的WEB-INF路径下读取book.xml资源
ServletContextResource src = new ServletContextResource
  (application , "WEB-INF/book.xml");
// 获取该资源的简单信息
System.out.println(src.getFilename());
System.out.println(src.getDescription());
// 创建基于SAX的dom4j解析器
SAXReader reader = new SAXReader();
Document doc = reader.read(src.getFile());
// 获取根元素
Element el = doc.getRootElement();
List l = el.elements();
// 遍历根元素的全部子元素
for (Iterator it = l.iterator();it.hasNext() ; )
{
  // 每个节点都是<书>节点
  Element book = (Element)it.next();
  List ll = book.elements();
  // 遍历<书>节点的全部子节点
  for (Iterator it2 = ll.iterator();it2.hasNext() ; )
  {
    Element eee = (Element)it2.next();
    out.println(eee.getText());
    out.println("<br/>");
  }
}
%>
</body>
</html>

二 配置文件

<?xml version="1.0" encoding="GBK"?>
<计算机书籍列表>
  <书>
    <书名>疯狂Java讲义</书名>
    <作者>李刚</作者>
  </书>
  <书>
    <书名>轻量级Java EE企业应用实战</书名>
    <作者>李刚</作者>
  </书>
</计算机书籍列表>

三 测试结果

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

(0)

相关推荐

  • springboot如何获取相对路径文件夹下静态资源的方法

    今日遇到一个问题:springboot需要获取到一个自定义名称文件夹下的静态资源(图片等),并且文件夹的路径不在classPath下面,而是一个相对路径. 一开始使用修改配置文件的方式: # 配置静态资源访问前缀 spring.mvc.static-path-pattern=*/** # 配置静态资源路径,默认配置失效 spring.resources.static-locations=../upload 发现并不行,无法解析出相对路径. 后面通过自定义静态资源映射配置类实现了: @Config

  • springboot操作静态资源文件的方法

    默认静态资源供 SpringBoot有几个默认的静态资源目录,当然也可配置,默认配置的/**映射到/static(或/public ,/resources,/META-INF/resources),自定义配置方式如下: spring.mvc.static-path-pattern=/** # Path pattern used for static resources. 前端如果需要访问默认的静态资源,下面有点要注意,考虑下面的目录结构: └─resources │ application.ym

  • spring mvc 读取xml文件数据库配置参数的方法

    本文主要介绍怎么通过属性注入与构造器注入实现把我们项目中要用到的数据库参数放到xml文件里面去,方便部署. spring mvc 4.2.6项目 SQL Server 2008数据库 本文介绍的主要使用ApplicationContext以及其实现类实现.主要用到的是ClassPathXmlApplicationContext. ClassPathXmlApplicationContext:从类路径ClassPath中寻找指定的XML配置文件,找到并装载 完成ApplicationContext

  • Spring实战之FileSystemResource加载资源文件示例

    本文实例讲述了Spring实战之FileSystemResource加载资源文件.分享给大家供大家参考,具体如下: 一 代码 package lee; import org.springframework.core.io.FileSystemResource; import org.dom4j.*; import org.dom4j.io.*; import java.util.*; import java.util.*; public class FileSystemResourceTest

  • Spring Boot 读取静态资源文件的方法

    一.需求场景 有时候我们需要在项目中使用一些静态资源文件,比如城市信息文件 countries.xml,在项目启动后读取其中的数据并初始化写进数据库中. 二.实现 静态资源文件 countries.xml 放在 src/main/resources 目录下 使用 Spring 的 ClassPathResource来实现 : Resource resource = new ClassPathResource("countries.xml"); File file = resource.

  • 读取spring配置文件的方法(spring读取资源文件)

    1.spring配置文件 复制代码 代码如下: <bean id="configproperties"          class="org.springframework.beans.factory.config.PropertiesFactoryBean">          <property name="location" value="classpath:jdbc.properties"/>

  • Springboot 使用 JSR 303 对 Controller 控制层校验及 Service 服务层 AOP 校验 使用消息资源文件对消息国际化

    导包和配置 导入 JSR 303 的包.hibernate valid 的包 <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.5.Final</version> </dependency> <dependency> <

  • Spring如何在xml文件中配置Bean

    Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 <beans> 根元素,可包含多个<bean>元素,一个<bean>即一个Bean的配置. <bean> 一个<bean>即一个Bean对象.原来是new出来该类的一个对象,Spring中是一个<bean>创建一个对象. <bean na

  • Spring标准的xml文件头实例分析

    这篇文章主要介绍了Spring标准的xml文件头实例分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2

  • 防止SpringMVC拦截器拦截js等静态资源文件的解决方法

    SpringMVC提供<mvc:resources>来设置静态资源,但是增加该设置如果采用通配符的方式增加拦截器的话仍然会被拦截器拦截,可采用如下方案进行解决: 方案一.拦截器中增加针对静态资源不进行过滤(涉及spring-mvc.xml) <mvc:resources location="/" mapping="/**/*.js"/> <mvc:resources location="/" mapping=&quo

  • Spring实战之使用ClassPathResource加载xml资源示例

    本文实例讲述了Spring使用ClassPathResource加载xml资源.分享给大家供大家参考,具体如下: 一 代码 package lee; import org.springframework.core.io.ClassPathResource; import org.dom4j.*; import org.dom4j.io.*; import java.util.*; import java.util.*; public class ClassPathResourceTest { p

随机推荐