从jsp属性文件填充选择 [英] Populate a select from a jsp properties file

查看:55
本文介绍了从jsp属性文件填充选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是修改一个jsp项目(不是Spring).更改非常简单: 在页面上获取静态链接列表,然后将其替换为包含所述链接的下拉列表.另一个要求是,下拉列表的数据源必须是属性文件.他们希望这样做,以便站点的最终用户(内部项目)可以修改/删除链接,而不必重新部署应用程序.我是一个Java语言的人(AngularJS,EmberJS,jQuery等),他在10或12年前对jsp进行了简要介绍,所以我对此非常了解.为了弄湿我,我下载了IntelliJ,设置了一个Tomcat项目,并使其读取并在属性文件中显示几个值.呜呜!仅花了几个小时便进行了谷歌搜索,以实现这一目标.尽管我找到了可以使用jsp构建选择的代码,但是我没有找到任何东西可以显示如何布局属性文件并读取键/值对,这些键/值对可以用作选择项的选项/值

I've been tasked with a modifying a jsp project (not Spring). The change is quite simple: Take a static list of links on a page and replace them with a dropdown consisting of said links. The other requirement is that the datasource for the dropdown be a properties file. They want this so that the end users of the site (internal project) can modify/remove links without having to redeploy the app. I'm a javascript guy (AngularJS, EmberJS, jQuery, etc) whose had a brief introduction to jsp about 10 or 12 years ago, so I'm quite green about it. To get my feet wet, I downloaded IntelliJ, setup a Tomcat project and got it to read and display a couple of values in a properties file. WooHoo!. It took a couple of hours of googling just to get this far. Although I've found code that will build a select using jsp, I've not found anything that shows me how to layout a properties file and read in the key/value pairs that I can use as the option/value for the select items.

这是我的属性文件:

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>

它正确显示了好奇的乔治.

And it correctly displays Curious George.

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

Can someone provide some guidance as to how to proceed with creating a select using a properties file? Thanks

推荐答案

我会这样进行:

  1. 加载属性(就像您一样)
  2. 填写地图(因为使用JSP-EL可以轻松访问键和值)
  3. 在页面属性中设置地图(可从JSP-EL访问)
  4. 使用Core JSTL遍历地图
  5. 使用keyvalue
  6. 填充选项标签
  1. load the properties (like you did)
  2. fill a map (because the keys and value are easy to access with JSP-EL)
  3. set the map in a page attribute (accessible from JSP-EL)
  4. iterate through the map (with the Core JSTL)
  5. populate the option tags using key and value

赞:

<%@ 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内,然后转发到JPS.

It is bad practice to use scriptlets in JSP.
You should consider moving the code from <% ... %> inside a servlet and forwarding to the JPS.

JSP仅应用于呈现信息.准备,计算,数据库操作等应在Servlet中完成.
您可以在此处了解更多信息:如何避免JSP文件中的Java代码? /a>

JSP should be used only to present the information. The preparation, calculation, database actions, etc should be done in Servlets.
You can read more here: How to avoid Java code in JSP files?

在您的情况下: 创建一个servlet,将其命名为PrepareLinkList,然后从上方将脚本代码移动到此处:

In your case: You create a servlet, lets name it PrepareLinkList, and move the scriplet code from above there:

@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中仅保留演示文稿:

And in the JSP stay only the presentation:

<%@ 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.它减少了代码重复,并且易于维护.

As you see the know you can run once the PrepareLinkList Servlet, and access the linkMap in all other following requests of JSP/Servlet. It reduces code repetition and it is easy to maintain.

在您的情况下,您可以在执行一个让我们说UpdateLinksProperties -Servlet

In your case, you can run/forward/include the PrepareLinkList after the execution of one lets say UpdateLinksProperties-Servlet

这篇关于从jsp属性文件填充选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆