2016-12-14 74 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

相关问题