java.util.properties 的 Getters/Setters
我想通过 jsp 显示 HTML/XML 中的属性。就像这样:
${MyClass.properties.propertieOne}
我创建了属性的扩展类 MProperties,但是如何在该类中为我的属性创建 getter?
BR 科莱萨尔
I want display properties in HTML/XML via jsp. Like as:
${MyClass.properties.propertieOne}
I created extended class of properties, MProperties, but how can I create getters in that class for my properties?
BR
Kolesar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以创建类来使用构造函数(如)来处理您的属性
,而不是通过一种方法(如
代码中的finally)获取属性,您只能调用
一些代码,其中缺少某些代码,有些代码可能不需要,但您应该知道这是否是您想要做的...
you can create class to handle your properties with constructor like
and than get properties by one method like
finally in code you can call only
some code is missing and some you maybe wont need but you should get the idea if this is what you wanted to do...
您可以在
Properties
类中使用store(Writer writer, String comments)
方法。将属性写入 StringWriter 并使用其中的 String 在 HTML 上打印。You can use
store(Writer writer, String comments)
method inProperties
class. Write your properties to a StringWriter and use String from it to print on the HTML.${yourObject.properties.propertyOne}
有效。Properties 扩展了 Hashtable 实现 Map
和${map.key}
为您提供该键下的值。但是您不应该使用 JSP 中的 setter,因此这仅用于显示目的。但您无法直接从静态字段访问它。您必须将其添加到某些上下文 - 请求或应用程序。例如,在
ServletContextListener.contextInitialized(..)
中:(当然,您必须在 web.xml 中映射
)${yourObject.properties.propertyOne}
works.Properties extends Hashtable implement Map
and${map.key}
gives you the value under that key. But you are not supposed to use a setter from a JSP, so this is only for display purposes.But you can't access it directly from a static field. You have to add it to some context - request or application. For example, in a
ServletContextListener.contextInitialized(..)
:(then you'd have to map the
<listener>
in web.xml, of course)如果您扩展了
Properties
,您可能确切地知道您想要拥有哪些字段。在这种情况下,最好使用这些字段和适当的 getter(以及您希望的 setter 和/或构造函数)创建一个 POJO(简单对象)。如果您以某种方式需要属性的动态性(?),请忽略此答案。If you extended
Properties
, you probably know exactly which fields you want to have. In that case, it would seem to be better to just a create a POJO (simple object) with those fields and the appropriate getters (and setters and/or constructor as you wish). If you somehow need the dynamic-ness (?) ofProperties
, ignore this answer.