Matthias Reisner
Matthias Reisner

Reputation: 579

Load file on Tomcat Server (JSF,SPRING)

Somebody here who knows how to load a file on the tomcat server? We are using: Maven, JSF 2, Spring 3, Tomcat 7

I have the following Project Structure:

structure

I want to load the page.xml file!

I thought about setting a Environment Variable in the GlobalNamingResource in the server.xml of the server. But how can I access this variable in my WebApp?

Or I can use the ContextClassLoader, but if I want to load that file, I always get back null.

The problem is, that I can not use the FacesContext, because i get back null if I call FacesContext.getCurrentInstance();

Currently I had to hardcode the path to get it work. But I always have to change this path if I want to test it in eclipse or if I deploy it on the server.

@PostConstruct
public void loadMenu() {
             List<Page> pageCollection = new ArrayList<Page>();
    Page page = null;

    File xmlFile = new File(
            "C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\rainstar2-webapp\\WEB-INF\\classes\\at\\beko\\rainstar2\\page.xml");

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document xmlDoc = db.parse(xmlFile);
        xmlDoc.getDocumentElement().normalize();

        NodeList pages = xmlDoc.getElementsByTagName("page");
        int size = pages.getLength();
        Node pageNode = null;

        for (int i = 0; i < size; i++) {
            pageNode = pages.item(i);
            NamedNodeMap attr = pageNode.getAttributes();
            page = new Page();
            page.setLabel(attr.getNamedItem("key").getNodeValue()
                    .toString());
            page.setRedirect(Boolean.valueOf(attr.getNamedItem("redirect")
                    .getNodeValue().toString()));
            page.setNavigationName(attr.getNamedItem("url").getNodeValue()
                    .toString());
            page.addRole(attr.getNamedItem("role").getNodeValue()
                    .toString());
            pageCollection.add(page);
        }
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Upvotes: 1

Views: 1659

Answers (2)

Matthias Reisner
Matthias Reisner

Reputation: 579

Ok My solution was.

I put the the page.xml into the web-inf.

ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL url = loader.getResource("..\\page.xml");
    File xmlFile = null;
    try {
        xmlFile = new File(url.toURI());
    } catch (URISyntaxException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

enter image description here

Upvotes: 0

kostja
kostja

Reputation: 61578

With JSF you could use the ResourceHandler. It works nice for resources under /webapp/resources. I am not entirely sure about resources under /main

    FacesContext currentContext = FacesContext.getCurrentInstance();
    ResourceHandler resourceHandler = currentContext.getApplication()
            .getResourceHandler();
    Resource resource = resourceHandler.createResource(fileName, libraryName);

The libraryName is the name of the folder under /webapp/resources.

EDIT : Without relying on JSF you could use the good old ClassLoader.

     InputStream inputStream = this.getClass().getClassLoader()  
             .getResourceAsStream("/path/to/file/from/.war_as_root/fileName");  

Please note that the path is relative from the root of your packaged .war

Upvotes: 1

Related Questions