Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

java - How to change locale in a Struts action class?

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.

I can get the current locale using super.getLocale().toString();

But how to set locale I don't know.

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"));
        }
    }
}


I want to change the locale for my app right in the above Action class. How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use ActionContext.getContext().setLocale(locale) and to put it in HTTP session session.put(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, locale).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...