从 Struts 2 中的 Action 类访问 ApplicationResource.properties 文件

发布于 2024-10-21 13:30:11 字数 78 浏览 0 评论 0原文

我可以从 Struts 2 中的 Action 类访问 ApplicationResource.properties 文件键吗 并更新键的值?

can i access ApplicationResource.properties file keys from Action Class in Struts 2
and update the values of the key ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

丢了幸福的猪 2024-10-28 13:30:11

我认为您不能直接更新这些键的值,这会破坏它作为(静态)资源的目的。
但是您可以使用占位符。

ApplicationResources.properties

property.key=Hi {0}, there's a problem with {1}

MyAction.java

public ActionForward execute(ActionMapping mapping,
                             ActionForm form,
                             javax.servlet.ServletRequest request,
                             javax.servlet.ServletResponse response)
                      throws java.lang.Exception {
MessageResources msgResource = getResources(request);
String msg = msgResource.getMessage("property.key", "Sankar", "updating values in the resources.");
}

I don't think you can update the values of those keys directly, that would kind of defeat the purpose of it being (static) resources.
You can however use placeholders.

ApplicationResources.properties

property.key=Hi {0}, there's a problem with {1}

MyAction.java

public ActionForward execute(ActionMapping mapping,
                             ActionForm form,
                             javax.servlet.ServletRequest request,
                             javax.servlet.ServletResponse response)
                      throws java.lang.Exception {
MessageResources msgResource = getResources(request);
String msg = msgResource.getMessage("property.key", "Sankar", "updating values in the resources.");
}
哆兒滾 2024-10-28 13:30:11

是的,这是可能的。
假设您在 applicationResources.properties 文件中有一个属性 error.login 。
例如:error.login=无效的用户名/密码。请再试一次。

然后在 Action 类中,您可以像这样访问它: getText("error.login")

完整示例:

applicationResources.properties

error.login= Invalid Username/Password

LoginAction.java

package net.sumitknath.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    private String username;

    private String password;

    public String execute() {
        if (this.username.equals("admin") && this.password.equals("admin123")) {
            return "success";
        } else {
            addActionError(getText("error.login"));
            return "error";
        }
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Yes its possible.
Lets say if you have a property error.login in applicationResources.properties file.
eg : error.login= Invalid Username/Password. Please try again.

then in the Action class you can access it like this : getText("error.login")

Complete example:

applicationResources.properties

error.login= Invalid Username/Password

LoginAction.java

package net.sumitknath.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    private String username;

    private String password;

    public String execute() {
        if (this.username.equals("admin") && this.password.equals("admin123")) {
            return "success";
        } else {
            addActionError(getText("error.login"));
            return "error";
        }
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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