1

WEB-INF 内のプロパティ ファイルを読み込もうとしていますが、得られるのは null だけです。私はJava Web開発にまったく慣れていません。

私の初期化方法:

 @Override
    public void init() throws ServletException {
        try {
            String path = "";
            path = "/WEB-INF/" + getServletContext().getInitParameter("monpesapropertiesfile");
            String realPath;
            realPath = getServletContext().getRealPath(path);
            if(realPath != null){
                 InputStream fis = new FileInputStream(realPath);
                 prop.load(fis);
            }
        } catch (Exception asd) {
            System.out.println(asd.getMessage());
        }
    }
    public String getDatabaseUrl() {
        String url = prop.getProperty("database.url");
        return url;
    }

私の Web.xml:

<context-param>
        <param-name>monpesapropertiesfile</param-name>
        <param-value>monpesaproperties.properties</param-value>
    </context-param>
    <servlet>
        <servlet-name>ReadProperty</servlet-name>
        <servlet-class>com.monpesa.properties.ReadProperty</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

String getDataBaseUrl が null を返すため、何かが足りないことはわかっています。助けてください。

4

2 に答える 2

3

私はどちらかだろう

1)プロパティを入れて、それをロードするためにWEB-INF/classes使用getClass().getResourceAsStream()します。

また

2) 完全なファイル パスを web.xml に配置します (そして、ファイルを Web アプリケーションの外部にあるユーザーが編集可能な構成領域に配置します)。

于 2013-09-19T12:09:00.510 に答える