java.util.properties 的 Getters/Setters

发布于 2024-12-08 23:04:20 字数 171 浏览 0 评论 0原文

我想通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

物价感观 2024-12-15 23:04:20

您可以创建类来使用构造函数(如)来处理您的属性

private Properties props = null;

    private MyProperties() throws IOException {

        FileInputStream propFile = new FileInputStream(FULL_PATH);
        props = new Properties(System.getProperties());
        props.load(propFile);

        RegistryManager rm = RegistryManager.singleton();
        rm.addRegistry("MyProperty", this);
    }

public static MyProperties Singelton() {
        synchronized (MyProperties.class) {
            if (theInstance == null) {
                try {
                    theInstance = new MyProperties();
                } catch (IOException e) {
                    throw new MissingResourceException("Unable to load property file \"" + FULL_PATH + "\"", MyProperties.class.getName(),
                            PROPERTIES_FILENAME);
                }
            }
        }
        return theInstance;
    }

,而不是通过一种方法(如

public static String getProperty(String propertyName) {
        String value = Singelton().props.getProperty(propertyName);
        if (value == null) {
            LOGGER.warning("propertyName (" + propertyName + ") not found in property file (" + FULL_PATH + ")");
        }

        return value;
    }

代码中的finally)获取属性,您只能调用

String desiredProperty = MyProperties.getProperty("propertyKey");

一些代码,其中缺少某些代码,有些代码可能不需要,但您应该知道这是否是您想要做的...

you can create class to handle your properties with constructor like

private Properties props = null;

    private MyProperties() throws IOException {

        FileInputStream propFile = new FileInputStream(FULL_PATH);
        props = new Properties(System.getProperties());
        props.load(propFile);

        RegistryManager rm = RegistryManager.singleton();
        rm.addRegistry("MyProperty", this);
    }

public static MyProperties Singelton() {
        synchronized (MyProperties.class) {
            if (theInstance == null) {
                try {
                    theInstance = new MyProperties();
                } catch (IOException e) {
                    throw new MissingResourceException("Unable to load property file \"" + FULL_PATH + "\"", MyProperties.class.getName(),
                            PROPERTIES_FILENAME);
                }
            }
        }
        return theInstance;
    }

and than get properties by one method like

public static String getProperty(String propertyName) {
        String value = Singelton().props.getProperty(propertyName);
        if (value == null) {
            LOGGER.warning("propertyName (" + propertyName + ") not found in property file (" + FULL_PATH + ")");
        }

        return value;
    }

finally in code you can call only

String desiredProperty = MyProperties.getProperty("propertyKey");

some code is missing and some you maybe wont need but you should get the idea if this is what you wanted to do...

埖埖迣鎅 2024-12-15 23:04:20

您可以在 Properties 类中使用 store(Writer writer, String comments) 方法。将属性写入 StringWriter 并使用其中的 String 在 HTML 上打印。

You can use store(Writer writer, String comments) method in Properties class. Write your properties to a StringWriter and use String from it to print on the HTML.

短暂陪伴 2024-12-15 23:04:20

${yourObject.properties.propertyOne} 有效。 Properties 扩展了 Hashtable 实现 Map${map.key} 为您提供该键下的值。但是您不应该使用 JSP 中的 setter,因此这仅用于显示目的。

但您无法直接从静态字段访问它。您必须将其添加到某些上下文 - 请求或应用程序。例如,在 ServletContextListener.contextInitialized(..) 中:(

servletContext.addAttribute("yourProperties", MyClass.properties);

当然,您必须在 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(..):

servletContext.addAttribute("yourProperties", MyClass.properties);

(then you'd have to map the <listener> in web.xml, of course)

初见 2024-12-15 23:04:20

如果您扩展了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 (?) of Properties, ignore this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文