【问题标题】:Change properties in spring:message in JSP files in Spring mvc更改spring中的属性:Spring mvc中JSP文件中的消息
【发布时间】:2015-01-30 04:11:39
【问题描述】:

我在 spring 中有一个属性文件,如下所示。

label.company.name = ABC Company
label.company.address = 123, west street
label.company.message = Welcome to ABC Company

我面临的问题是该公司名称可能会有所不同,并且该公司名称在文件中引用的位置很多。所以如果我更改公司名称,属性文件中所有引用公司名称的地方也应该更改。不应使用字符串搜索和替换(按照给我的说明)。我该如何进行上述任务。

jsp文件中属性调用如下

<spring:message code="label.company.name"></spring:message>

请帮助我。谢谢

【问题讨论】:

  • 你可以在jsp中使用java代码来实现
  • 如果有办法的话,我需要一种方法在它自己的属性文件中进行处理
  • 我的意思是你可以使用 Java 代码更改属性值,我没有看到从属性文件中更改属性值的方法。
  • 您是想说可以存在超过 1 家公司还是直接替换??

标签: jsp spring-mvc properties-file


【解决方案1】:

您可以在 Java 类中使用以下代码更改现有属性,

Properties prop = new Properties();
OutputStream output = null;
try {
    output = new FileOutputStream("filePath/fileName.properties");
    prop.setProperty("label.company.name", "new company name");
    prop.store(output, null);
} catch (IOException io) {
    // If file not found or does not exists
    io.printStackTrace();
} finally {
    if (output != null) {
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

或者如果您想在jsp 中执行此操作,请将上述代码放入&lt;% %&gt; 块中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 2014-01-19
    • 2014-11-10
    • 2012-07-23
    • 2013-06-06
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多