1

tomcat サーバーにファイルをロードする方法を知っている人はいますか? 使用している: Maven、JSF 2、Spring 3、Tomcat 7

次のプロジェクト構造があります。

構造

page.xmlファイルを読み込みたい!

サーバーのserver.xmlのGlobalNamingResourceに環境変数を設定することを考えました。しかし、WebApp でこの変数にアクセスするにはどうすればよいでしょうか?

または、ContextClassLoader を使用することもできますが、そのファイルをロードしたい場合は、常に null が返されます。

問題は、FacesContext.getCurrentInstance(); を呼び出すと null が返されるため、FacesContext を使用できないことです。

現在、パスを機能させるには、パスをハードコーディングする必要がありました。しかし、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();
    }
}
4

2 に答える 2

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 のルートからの相対パスであることに注意してください

于 2012-05-31T07:33:31.237 に答える
0

わかりました私の解決策はでした。

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

ここに画像の説明を入力

于 2012-05-31T08:22:22.217 に答える