【问题标题】:Load file on Tomcat Server (JSF,SPRING)在 Tomcat 服务器上加载文件 (JSF,SPRING)
【发布时间】:2012-05-31 07:23:48
【问题描述】:

这里有人知道如何在 tomcat 服务器上加载文件吗? 我们正在使用: Maven、JSF 2、Spring 3、Tomcat 7

我有以下项目结构:

我要加载 page.xml 文件!

我想过在服务器的 server.xml 中的 GlobalNamingResource 中设置一个环境变量。但是如何在我的 WebApp 中访问这个变量?

或者我可以使用 ContextClassLoader,但是如果我想加载那个文件,我总是会返回 null。

问题是,我不能使用 FacesContext,因为如果我调用 FacesContext.getCurrentInstance(); 会返回 null;

目前我必须对路径进行硬编码才能使其工作。但是如果我想在 eclipse 中测试或者在服务器上部署它,我总是必须改变这个路径。

@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();
    }
}

【问题讨论】:

  • 您最好发布用于加载 xml 的代码供我们调试。

标签: java file tomcat maven load


【解决方案1】:

使用 JSF,您可以使用 ResourceHandler。它适用于/webapp/resources 下的资源。我不太确定/main下的资源

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

libraryName/webapp/resources 下文件夹的名称。

编辑:不依赖 JSF,您可以使用旧的 ClassLoader

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

请注意,路径是相对于您打包的 .war 的根目录

【讨论】:

  • 好吧,听起来很不错!唯一的问题是我在 JavaBean 中有一个用 @PostConstruct 注释的方法,如果我想在该方法中使用 FacesContext.getCurrentInstance() 获取 FacesContext 的当前实例,我只能返回 null。不使用FacesContext就没有办法了吗?
  • @Matthias 编辑了答案,添加了基于类加载器的替代方案
  • 谢谢!我有几乎相同的解决方案!
【解决方案2】:

好的,我的解决方案是。

我把page.xml放到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();
    }

【讨论】:

    猜你喜欢
    • 2017-10-31
    • 2012-04-23
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多