如何在Struts动作类中更改语言环境? [英] How to change locale in a Struts action class?

查看:169
本文介绍了如何在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会话
session.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动作类中更改语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆