【问题标题】:Populate a select from a jsp properties file从 jsp 属性文件中填充选择
【发布时间】:2016-12-04 04:29:19
【问题描述】:

我的任务是修改一个 jsp 项目(不是 Spring)。更改非常简单: 获取页面上的静态链接列表,并用包含所述链接的下拉列表替换它们。另一个要求是下拉列表的数据源是属性文件。他们希望这样做,以便站点的最终用户(内部项目)可以修改/删除链接,而无需重新部署应用程序。我是一个 javascript 人(AngularJS、EmberJS、jQuery 等),大约 10 或 12 年前对 jsp 进行了简要介绍,所以我对它很感兴趣。为了让我的脚湿透,我下载了 IntelliJ,设置了一个 Tomcat 项目并让它读取并显示属性文件中的几个值。呜呼!花了几个小时的谷歌搜索才走到这一步。尽管我找到了将使用 jsp 构建选择的代码,但我还没有找到任何可以告诉我如何布局属性文件和读取可以用作选择项的选项/值的键/值对的东西.

这是我的属性文件:

fname=Courious
lname=George

这是显示值的标记:

<%@page import="java.util.Properties" %>

<%
 InputStream stream = application.getResourceAsStream("foo.properties");
 Properties props = new Properties();
 props.load(stream);
 String fname = props.getProperty("fname");
 String lname = props.getProperty("lname");
%>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
out.println(fname);
out.println(lname);
%>
</body>

它正确显示好奇的乔治。

有人可以就如何使用属性文件继续创建选择提供一些指导吗? 谢谢

【问题讨论】:

    标签: jsp properties


    【解决方案1】:

    我会这样做:

    1. 加载属性(就像你做的那样)
    2. 填充映射(因为键和值很容易使用 JSP-EL 访问)
    3. 在页面属性中设置地图(可从 JSP-EL 访问)
    4. 遍历地图(使用核心 JSTL)
    5. 使用keyvalue 填充选项标签

    像这样:

    <%@ page contentType="text/html; charset=UTF-8" 
      import="java.io.InputStream, java.util.HashMap, java.util.Properties" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
    <%
    // 1. load the properties
    InputStream stream = application.getResourceAsStream("foo.properties");
    Properties props = new Properties();
    props.load(stream);
    
    // 2. fill a map
    HashMap<String, String> linkMap = new HashMap<String, String>();
    for (final String name: props.stringPropertyNames()) {
         linkMap.put(name, props.getProperty(name));
    }
    
    // 3. set the map in a page attribute
    pageContext.setAttribute("linkMap", linkMap);
    %>
    <html>
    <head>
    <title>$Title$</title>
    </head>
    <body>
    <h3>select field with map</h3>
    <select name="link">
    
    <!-- 4. iterate through the map -->
    <c:forEach items="${linkMap}" var="link">
        <!-- 5. populate the option tags -->
        <option value="${link.key}">${link.value}</option>
    </c:forEach>
    </select>
    </body>
    

    在 JSP 中使用 scriptlet 是不好的做法。
    您应该考虑将代码从 servlet 中的 &lt;% ... %&gt; 移动并转发到 JPS。

    编辑:

    JSP 应该只用于呈现信息。准备、计算、数据库操作等都应该在 Servlet 中完成。
    你可以在这里阅读更多:How to avoid Java code in JSP files?

    在您的情况下: 您创建一个 servlet,将其命名为 PrepareLinkList,然后将上面的脚本代码移到那里:

    @WebServlet("/PrepareLinkList")
    public class PrepareLinkList extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
             InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("testingThings/properties/foo.properties");
             Properties props = new Properties();
             props.load(stream);
    
             HashMap<String, String> map = new HashMap<String, String>();
    
             for (final String name: props.stringPropertyNames()) {
                 map.put(name, props.getProperty(name));
             }
    
             // make the linkMap attribute available accross the application
             getServletContext().setAttribute("linkMap", map);
             // response.sendRedirect("dropdown.jsp");
             // or
             // request.getRequestDispatcher("dropdown.jsp").forward(request, response);
        }
    
    }
    

    而在 JSP 中只保留演示文稿:

    <%@ page contentType="text/html; charset=UTF-8" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
    <head>
    <title>$Title$</title>
    </head>
    <body>
    
    <h3>with map</h3>
    
    <select name="link">
    <c:forEach items="${linkMap}" var="link">
        <option value="${link.key}">${link.value}</option>
    </c:forEach>
    </select>
    
    </body>
    

    如您所见,您可以运行一次 PrepareLinkList Servlet,然后在 JSP/Servlet 的所有其他后续请求中访问 linkMap。它减少了代码重复并且易于维护。

    在您的情况下,您可以在执行一个假设UpdateLinksProperties-Servlet 后运行/转发/包含PrepareLinkList

    【讨论】:

    • 谢谢,我知道了。但我不知道这意味着什么:“您应该考虑将代码从 移动到 servlet 中并转发到 JPS”
    • 好的,我现在明白了。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多