Example of accessing resource file by servletcontextresource in spring


In this paper, an example is given to show how spring‘s servletcontextresource can access resource files. The details are as follows:

One view

<%@ 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">
  < title > test servletcontextresource < / Title >
<h3>Testing servletcontextresource</h3>
//Read from WEB-INF path under Web context book.xml resources
ServletContextResource src = new ServletContextResource
  (application , "WEB-INF/book.xml");
//Get simple information about the resource
//Creating DOM 4J parser based on Sax
SAXReader reader = new SAXReader();
Document doc = reader.read(src.getFile());
//Get root element
Element el = doc.getRootElement();
List l = el.elements();
//Traverses all child elements of the root element
for (Iterator it = l.iterator();it.hasNext() ; )
  //Each node is a Book node
  Element book = (Element)it.next();
  List ll = book.elements();
  //Traverse all the child nodes of the < book > node
  for (Iterator it2 = ll.iterator();it2.hasNext() ; )
    Element eee = (Element)it2.next();

2. Configuration file

<?xml version="1.0" encoding="GBK"?>
Computer book list
  The book
    < book title > crazy Java handout < / book title >
    < author > Li Gang < / author >
  < / book >
  The book
    < book title > lightweight Java EE enterprise application practice < / book title >
    < author > Li Gang < / author >
  < / book >
< / computer book list >

3. Test results

More about Java related content, interested readers can see our special topics: “spring framework introduction and advanced tutorial”, “Java data structure and algorithm tutorial”, “Java operation DOM node skills summary”, “java file and directory operation skills summary” and “Java cache operation skills summary”

I hope this article will be helpful to you in Java programming.

Recommended Today

Go carbon version 1.2.0 has been released to improve and optimize the multi scenario support for orm

Carbon is a lightweight, semantic and developer friendly golang time processing library, which supports chain call, lunar calendar, Gorm, xorm and other mainstream orm If you think it’s good, please give it to a star github:github.com/golang-module/carbon gitee:gitee.com/go-package/carbon Installation and use //Using GitHub Library go get -u github.com/golang-module/carbon import ( “github.com/golang-module/carbon”) //Using gitee Library go get […]