在我的页面顶部,我想要一个下拉菜单来更改首选语言。如果它改变了页面,则将重新加载并使用所选语言呈现。在我的解决方案中,语言不会立即加载。
@SuppressWarnings("serial")
public class CurrentLanguageDropdown extends LanguageDropDown {
private String sessionlanguage;
private IModel<String> model;
public CurrentLanguageDropdown(String id, Locale locale) {
super(id, locale);
this.sessionlanguage = locale.getCountry();
GlobalAccountSession session = GlobalAccountSession.get();
model = new SelectedLocaleModel("sessionlanguage", this, new Model<Serializable>(session.getLocale()),
locale.getCountry());
setModel(model);
}
@Override
public void onSelectionChanged(String newSelection) {
GlobalAccountSession session = GlobalAccountSession.get();
model = new SelectedLocaleModel("sessionlanguage", this, new Model<Serializable>(session.getLocale()),
newSelection);
}
@Override
protected boolean wantOnSelectionChangedNotifications() {
return true;
}
String getSessionlanguage() {
return sessionlanguage;
}
private class SelectedLocaleModel extends StringResourceModel {
private String selectedValue = "";
private Component parent;
public SelectedLocaleModel(String resourceKey, Component parent, IModel<?> model, String selectedValue) {
super(resourceKey, parent, model);
this.selectedValue = selectedValue;
this.parent = parent;
}
@Override
public void setObject(java.lang.String object) {
GlobalAccountSession session = GlobalAccountSession.get();
Locale locale = new Locale(object.toString());
session.setLocale(locale);
selectedValue = locale.getLanguage();
}
@Override
public String getObject() {
GlobalAccountSession session = GlobalAccountSession.get();
selectedValue = session.getLocale().getLanguage();
return selectedValue;
}
}
}
查看我的代码,我看到首先调用 getObject,然后调用 setObject。所以渲染似乎是在处理用户所做的更改之前完成的。
谁能告诉我我做错了什么?