如何在jsp中迭代多个属性

我正在尝试遍历属性文件中的一组键,因此只输出“message.pX”。

a.property=foo message.p1=a message.p2=b message.p3=c some.other.property=bar 

我不知道在文件中有多少带前缀(message.p)的属性,所以我想显示任何存在的属性。 我已经有了一个使用ResourceBundle的bean类来处理它并为该语言环境提取正确的bundle,但是有一个标准的标签,比如可以处理这个吗?

没有标准的方法来处理这个问题。 由于您显然已经完全控制了资源捆绑创建,因此最好的办法是引入一个新的关键字/约定,例如以.list结尾的密钥:

  

${p}

..并创建一个自定义ResourceBundle其中您重写handleGetObject()以返回所需的值作为List ,如:

 protected Object handleGetObject(String key) { if (key.endsWith(".list")) { String listkey = key.substring(0, key.length() - 5); List list = new ArrayList(); for (int i = 1; containsKey(listkey + i); i++) { list.add(String.valueOf(getObject(listkey + i))); } if (!list.isEmpty()) { return list; } } return getObject(key); } 
 Properties properties = new Properties(); try { properties.load(new FileInputStream("filename.properties")); } catch (IOException e) { } Enumeration e = properties.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); //Edited answer if(key.indexOf("message.p") != -1 ){ System.out.println(key + " , " + properties.getProperty(key)); //Add key and value to a list } //Edited answer } 

我建议你在Servlet或Java类中执行此操作,并将属性列表存储在java.lang.List对象(如ArrayList或LinkedList)中,然后将结果发送到jsp。 避免在jsp中执行此操作。