向JSP提供服务器端属性的最佳方法是什么?

时间:2016-12-14 14:24:21

标签: java jsp servlets servlet-filters

我有一个system.properties文件,其中包含我希望在我的index.jsp页面上显示的系统版本和构建类型(我在一套仪表板中有几个)。向JSP提供这些属性的最佳方法是什么?

我目前正在直接从JSP中读取属性文件,但这很麻烦,因为它有几行代码,并且必须在我的所有JSP中重复。我确实将该代码抽象到自己的JSP中,然后将其包含在我的其他JSP中,但这仍然很麻烦。

理想情况下,我想在任何页面中执行以下操作:

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

这可能意味着我使用servlet或servlet-filter,但我不确定。

谢谢!

2 个答案:

答案 0 :(得分: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>

如果系统属性动态变化,
您可以直接在context属性(与system.properties文件并行)中更新它们

答案 1 :(得分: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