How to change locale in a Struts action class?(如何更改 Struts 操作类中的语言环境?)
问题描述
我有一个 Action 类,我想获取我的应用的语言环境并在此处更改它,但我不知道如何也找不到答案.
I have an Action class, and I want to get the locale of my app and change it here, but I don't know how and can't find an answer.
我可以使用 super.getLocale().toString();
但是如何设置语言环境我不知道.
public class LoginAction extends ActionSupport {
    private String login;
    private String password;
    private String language;
    @Override
    public String execute() throws Exception {
        String result = Factory.INSTANCE.getUserDao().checkUser(login, password);
        if(result == null){
            return ERROR;
        }
        return SUCCESS;
    }
    public String getLogin() {
        return login;
    }
    public void setLogin(String login) {
        this.login = login;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getLanguage() {
        return language;
    }
    public void setLanguage(String language) {
        this.language = language;
    }
    @Override
    public void validate() {
        super.validate();
        if(login.isEmpty() | password.isEmpty()){
            addActionError(getText("login.error"));
        }else {
            addActionMessage(getText("login.correct"));
        }
    }
}
我想在上面的 Action 类中更改我的应用程序的语言环境.我该怎么做?
I want to change the locale for my app right in the above Action class. How can I do this?
推荐答案
使用 ActionContext.getContext().setLocale(locale) 并将其放入 HTTP sessionsession.put(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, locale).
Use ActionContext.getContext().setLocale(locale) and to put it in HTTP session
session.put(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, locale).
这篇关于如何更改 Struts 操作类中的语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改 Struts 操作类中的语言环境?
				
        
 
            
        - Jersey REST 客户端:发布多部分数据 2022-01-01
 - 将log4j 1.2配置转换为log4j 2配置 2022-01-01
 - value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
 - Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
 - Java包名称中单词分隔符的约定是什么? 2022-01-01
 - C++ 和 Java 进程之间的共享内存 2022-01-01
 - 从 finally 块返回时 Java 的奇怪行为 2022-01-01
 - 如何使用WebFilter实现授权头检查 2022-01-01
 - Eclipse 插件更新错误日志在哪里? 2022-01-01
 - Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
 
						
						
						
						
						