0

在我的页面顶部,我想要一个下拉菜单来更改首选语言。如果它改变了页面,则将重新加载并使用所选语言呈现。在我的解决方案中,语言不会立即加载。

@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。所以渲染似乎是在处理用户所做的更改之前完成的。

谁能告诉我我做错了什么?

4

2 回答 2

1

您正在 onSelectionChanged 中创建一个新模型对象。这违背了应该修改但不替换模型的目的。如果您需要创建一个新的模型对象,您也必须替换组件使用的模型(再次调用 setModel(model))。

于 2013-01-30T09:05:09.610 回答
0

我知道这不是一个组件,而是一个组件的实现,但显示了总体思路

public class LocaleDropDownPanel extends Panel
{
private Locale sessionLocale;

public LocaleDropDownPanel(String id, Locale locale) 
{
    super(id);


    ArrayList<Locale> choices = Lists.newArrayList(Locale.getAvailableLocales());

    //didnt know if you need country or language 
    IChoiceRenderer<Locale> choiceRenderer = new ChoiceRenderer<Locale>("language");

    DropDownChoice<Locale> ddc = new DropDownChoice<Locale>("ddc", new PropertyModel<Locale>(this, "sessionLocale"), 
            new Model<ArrayList<Locale>>(choices), choiceRenderer)
    {
        @Override
        protected void onSelectionChanged(Locale newSelection) 
        {
             //if you want to save the selected locale in your session.
             GlobalAccountSession session = GlobalAccountSession.get();
             session.setLocale(newSelection);
        }
    };

    //This Language selected to begin with
    ddc.setDefaultModelObject(locale);

}

public Locale getSessionLocale() {
    return sessionLocale;
}

public void setSessionLocale(Locale sessionLocale) {
    this.sessionLocale = sessionLocale;
}
}
于 2013-01-30T14:54:03.940 回答