ロケール切り替えに、DropDownChoice? を使ったので合わせて勉強する
<?xml version="1.0" encoding="UTF-8"?>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.apache.org/">
<head>
<title></title>
</head>
<body>
<form wicket:id="f">
<p>
<select wicket:id="selectedLocale">
<option value="0">XXXXX</option>
<option value="1">YYYYY</option>
<option value="2">ZZZZZ</option>
</select>
</p>
The International
<fieldset>
<legend>1</legend>
Stand up, wretched of the earth
Stand up, galley slaves of hunger
Reason thunders in its volcano
(中略)
</form>
</body>
</html>
package com.snail.wicket.exam.internationalize;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.PropertyModel;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
public class InternationalPage extends WebPage {
private static final long serialVersionUID = -6658650430422411331L;
public InternationalPage() {
Form f = new Form("f");
add(f);
List<Locale> localeList = new LinkedList<Locale>();
localeList.add(Locale.FRENCH);
localeList.add(Locale.JAPANESE);
localeList.add(new Locale("ru"));
localeList.add(Locale.CHINESE);
localeList.add(Locale.ENGLISH);
IChoiceRenderer choiceRenderer = new IChoiceRenderer() {
private static final long serialVersionUID = 6539188524433191181L;
@Override
public Object getDisplayValue(Object object) {
Locale locale = (Locale) object;
return locale.getDisplayName(getSession().getLocale());
}
@Override
public String getIdValue(Object object, int index) {
return index + "_" + object.toString();
}
};
DropDownChoice localeChoice = new DropDownChoice(
"selectedLocale",
new PropertyModel(getSession(), "locale"),
localeList,
choiceRenderer) {
private static final long serialVersionUID = -8923017502166461576L;
@Override
protected boolean wantOnSelectionChangedNotifications() {
return true;
}
@Override
protected void onSelectionChanged(Object newSelection) {
System.out.println("new selection is "
+ newSelection.toString());
}
};
f.add(localeChoice);
}
}
IChoiceRenderer choiceRenderer = new IChoiceRenderer() { private static final long serialVersionUID = 6539188524433191181L; @Override public Object getDisplayValue(Object object) { Locale locale = (Locale) object; return locale.getDisplayName(getSession().getLocale()); } @Override public String getIdValue(Object object, int index) { return index + "_" + object.toString(); } };
DropDownChoice#wantOnSelectionChangedNotifications() : booleanを Override して、true を返すようにすればいい
DropDownChoice#onSelectionChanged(Object newSelection) : voidを Override して、その中に記述する。