2016-12-14 73 views
1

我有一個system.properties文件,其中包含系統版本和構建類型,我想在我的index.jsp頁面上顯示(我在一組儀表板中有幾個)。將這些屬性提供給我的JSP的最佳方式是什麼?向JSP提供服務器端屬性的最佳方式是什麼?

我目前直接從JSP讀取屬性文件,但這很麻煩,因爲它有幾行代碼,它必須在所有JSP中重複。我確實將該代碼抽象到自己的JSP中,然後將其包含在其他JSP中,但仍然感覺很麻煩。

理想情況下,我願意做任何網頁以下內容:

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

這可能意味着我使用servlet或Servlet過濾器,但我不知道。

謝謝!

+0

那麼一個JSP *就是一個各種各樣的servlet。但是由於它是由聲明性標記構成的,併爲其添加不重要的代碼語句是一種慣例,所以您確實可以添加一個集中式(無狀態)服務,以便以servlet的形式從所有JSP中檢索該信息可以訪問。但是我個人無法看到servlet過濾器如何在這個範圍內被限制。 – Mena

+0

另一種選擇是創建一個PropertiesTag - 然後可以在JSP中包含該屬性標記以呈現服務器屬性。 – donlys

+0

@donlys - 你能詳細說明嗎? –

回答

1

您可以使用實現ServletContextListener的WebListener。
所以在部署中,您可以讀取系統屬性並將它們設置爲屬性。
單獨或在地圖中。

system.properties

比方說你有一個文件system.properties的內容:

buildType=myType 
systemVersion=v55 

WebListener

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

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

對於這個例子,我把listener和properties文件放在同一個包'testingThings.properties'中。但是你可以將它們分開。監聽器會在你的'src'文件夾'src/main/java'的某處。如果在類定義之前添加註釋'@ WebListener',則會在部署/同步中識別它。 –

+0

我很高興,我可以幫忙。不要忘記接受答案,並樂觀向上 –

0

創建一個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中使用它:

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

的Hello World只是一個例子 - 你可以有一切都在你的標籤定義你的屬性(系統屬性),並得到那些在JSP這種方式。請參閱:http://docs.oracle.com/javaee/5/tutorial/doc/bnalj.html

相關問題