【问题标题】:What is the best way to provide a server-side property to a JSP?向 JSP 提供服务器端属性的最佳方式是什么?
【发布时间】:2017-04-29 22:09:15
【问题描述】:

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

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

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

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

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

谢谢!

【问题讨论】:

  • 嗯,JSP 一个 servlet。但是由于它是由声明性标记组成的,并且向其中添加重要的代码语句是一种通常不受欢迎的做法,因此您确实可以添加一个集中式(无状态)服务,以它们的 servlet 形式从所有 JSP 中检索该信息可以访问。但是,我个人看不到 servlet 过滤器的范围。
  • 另一种选择是创建一个 PropertiesTag - 然后您可以将其包含在您的 JSP 中以呈现服务器属性。
  • @donlys - 你能详细说明一下吗?

标签: java jsp servlets servlet-filters


【解决方案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 文件平行)

【讨论】:

  • 这正是我想要的。我有一个 Maven 项目。你能解释一下我们的观点吗?关于在哪里放置 WebListener java 文件的解释?
  • 对于这个例子,我把监听器和属性文件放在同一个包testingThings.properties。您可以根据需要将它们分开。侦听器位于您的src 文件夹src/main/java 中的某个位置。如果在类定义前添加注解@WebListener,在部署/同步时会被识别。
  • 我很高兴,我能帮上忙。不要忘记接受答案并点赞
【解决方案2】:

创建一个 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 2010-10-13
    • 2011-06-15
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 2011-05-17
    相关资源
    最近更新 更多