Spring实战之ServletContextResource访问资源文件示例
本文实例讲述了Spring实战之ServletContextResource访问资源文件。分享给大家供大家参考,具体如下:
一视图
<%@pagecontentType="text/html;charset=GBK"language="java"errorPage=""%> <%@pageimport="org.springframework.web.context.support.ServletContextResource"%> <%@pageimport="org.dom4j.*,org.dom4j.io.*,java.util.*"%>测试ServletContextResource 测试ServletContextResource
<% //从WebContext下的WEB-INF路径下读取book.xml资源 ServletContextResourcesrc=newServletContextResource (application,"WEB-INF/book.xml"); //获取该资源的简单信息 System.out.println(src.getFilename()); System.out.println(src.getDescription()); //创建基于SAX的dom4j解析器 SAXReaderreader=newSAXReader(); Documentdoc=reader.read(src.getFile()); //获取根元素 Elementel=doc.getRootElement(); Listl=el.elements(); //遍历根元素的全部子元素 for(Iteratorit=l.iterator();it.hasNext();) { //每个节点都是<书>节点 Elementbook=(Element)it.next(); Listll=book.elements(); //遍历<书>节点的全部子节点 for(Iteratorit2=ll.iterator();it2.hasNext();) { Elementeee=(Element)it2.next(); out.println(eee.getText()); out.println("
"); } } %>