2016-12-14 9 views
1

system.properties私はindex.jspページに表示したいシステムバージョンとビルドタイプのファイルを持っています(ダッシュボードのスイートにはいくつかあります)。これらのプロパティをJSPに提供する最善の方法は何ですか?JSPにサーバーサイドのプロパティを提供する最も良い方法は何ですか?

私は現在、JSPから直接プロパティファイルを読んでいますが、これは数行のコードであり、すべてのJSPで複製されなければならないため、面倒です。そのコードを自分のJSPに抽象化して、私が他のJSPに組み込むことはできましたが、それでもまだ面倒です。

理想的には、私は任意のページから、次の操作を行いたいのですが:私は、サーブレットまたはサーブレット・フィルターを使用しますが、私はよく分からないことを意味します

<body data-build-type='${buildType}' data-system-version='${systemVersion}'>

ありがとうございます!

+0

まあ、JSP *は*サーブレットです。しかし、それは宣言的なマークアップで作られていて、それに軽微なコード文を追加するのは一般的に嫌われているので、サーブレットの形ですべてのJSPからその情報を取得するための集中型(ステートレスな)アクセスすることができます。私は個人的には、サーブレットフィルタがどのようにスコープされるのかを見ることはできません。 – Mena

+0

もう1つの方法はPropertiesTagを作成することです。これをJSPに組み込んでサーバープロパティをレンダリングすることができます。 – donlys

+0

@donlys - あなたは詳しく説明できますか? –

答えて

1

ServletContextListenerを実装するWebListenerを使用できます。
展開すると、システムのプロパティを読み取って属性として設定できます。
地図とは別に、または地図内にあります。

system.properties

はあなたがコンテンツを持つファイルsystem.propertiesを考えてみましょう:

buildType=myType 
systemVersion=v55 

WebListener

package testingThings.properties; 

import java.io.IOException; 
import java.io.InputStream; 
import java.util.HashMap; 
import java.util.Properties; 
import javax.servlet.ServletContextEvent; 
import javax.servlet.ServletContextListener; 
import javax.servlet.annotation.WebListener; 

@WebListener 
public class ContextListener implements ServletContextListener { 
    public ContextListener() {} 
    public void contextDestroyed(ServletContextEvent sce) {} 

    public void contextInitialized(ServletContextEvent sce) { 
     InputStream stream = Thread.currentThread().getContextClassLoader() 
       .getResourceAsStream("testingThings/properties/system.properties"); 
     Properties props = new Properties(); 

     try { 
      props.load(stream); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     HashMap<String, String> map = new HashMap<String, String>(); 

     for (final String name : props.stringPropertyNames()) { 
      map.put(name, props.getProperty(name)); 
     } 

     sce.getServletContext().setAttribute("system", map); 
    } 
} 

WebListenerのようなものかもしれません

JSP:

JSPで使用すると、このようなsystem属性を反復処理することができます。システムのプロパティを動的に変更する場合
あなたが直接、コンテキスト属性に、それらを更新することができ、

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<html> 
<head><title>access map of system properties</title></head> 
<body> 
    <h3>access map of system properties</h3> 
    <table> 
     <c:forEach items="${system}" var="property"> 
      <tr> 
       <td>${property.key}:</td> 
       <td>${property.value}</td> 
      </tr> 
     </c:forEach> 
     <tr> 
    </table> 

    <h3>directly access of map properties</h3> 
    <table> 
      <tr> 
       <td>buildType:</td> 
       <td>${system.buildType}</td> 
      </tr> 
      <tr> 
       <td>systemVersion:</td> 
       <td>${system.systemVersion}</td> 
      </tr> 
     <tr> 
    </table> 
</body> 

(system.propertiesファイルに平行)

+0

これはまさに私が探していたものです。私はMavenプロジェクトを持っています。 WebListener Javaファイルをどこに置くべきかについての私の説明を教えてください。 –

+0

この例では、リスナーとプロパティーファイルを同じパッケージ 'testingThings.properties'に入れます。しかし、あなたはそれらを分けることができます。リスナーはあなたの 'src'フォルダ' src/main/java'のどこかに行きます。アノテーションをクラス定義の前に '@WebListener'を追加すると、それはデプロイ/同期時に認識されます。 –

+0

私はうれしいです、私は助けることができます。答えを受け入れることとupvoteを忘れないでください –

0

JSPカスタムタグを作成します。たとえば、次のように

<html> 
    <body> 
    <hw:HelloWorld /> 
    </body> 
</html> 

のHello Worldは一例です - あなたはあなたのタグで定義されたあなたの属性(システムプロパティ)の全てを持っているとのそれらを取得することができます:あなたのJSPで使用する

public class HelloWorld extends TagSupport 
{ 
    public int doStartTag() throws JspException 
    { 
     try 
     { 
      JspWriter out = pageContext.getOut(); 
      HttpServletResponse response = (HttpServletResponse)pageContext.getResponse(); 
      out.write("Hello world!"); <!-- your property 
     } 
     catch(Exception e) 
     { 
      throw new JspException(e.getMessage()); 
     } 
     return EVAL_PAGE; 
    } 
} 
     <!-- a tab library descriptor --> 
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor"> 
    <tlib-version>1.0</tlib-version> 
    <jsp-version>1.2</jsp-version> 
    <short-name>Simple Tags</short-name> 

    <tag> 
    <name>HelloWorld</name> 
    <tag-class>HelloWorld</tag-class> 
    <body-content>empty</body-content> 
    </tag> 
</taglib> 

JSPこのように。参照:http://docs.oracle.com/javaee/5/tutorial/doc/bnalj.html

関連する問題