今天我们来学习一下自定义FormComponent,温度与摄氏温度的转换,绝对温度T=摄氏温度t+273度
我们来看一下homePage.java代码
public class HomePage extends WebPage {
public HomePage() {
Form form = new Form("form");
Model<Double> kelvinModel = new Model<Double>();
form.add(new TemperatureDegreeField("temperatureDegreeField", kelvinModel));
form.add(new Label("kelvinValue", kelvinModel));
add(form);
}
}
对应的html
<html lang="en" xmlns:wicket="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="hd">
<div id="logo">
<img src="logo.png" width="50px" height="50px" alt="Wicket Logo" />
<h1>Apache Wicket</h1>
</div>
</div>
<div id="bd">
<form wicket:id="form">
<span wicket:id="temperatureDegreeField">
</span>
(Equals to: <span wicket:id="kelvinValue"></span> °K)
<br/>
<input type="submit" valuse="Submit"/>
</form>
</div>
<div id="ft">
</div>
</body>
</html>
TemperatureDegreeField实现代码如下
public class TemperatureDegreeField extends FormComponentPanel<Double> {
private TextField<Double> userDegree;
public TemperatureDegreeField(String id) {
super(id);
}
public TemperatureDegreeField(String id, IModel<Double> model) {
super(id, model);
}
@Override
protected void onInitialize() {
super.onInitialize();
IModel<String> labelModel = () -> getLocale().equals(Locale.US) ? "°F" : "°C";
add(new Label("mesuramentUnit", labelModel));
userDegree = new TextField<Double>("registeredTemperature", new Model<Double>());
add(userDegree);
userDegree.setType(Double.class);
}
@Override
public void convertInput() {
Double userDegreeVal = userDegree.getConvertedInput();
Double kelvinDegree;
if(userDegreeVal == null) {
return;
}
if(getLocale().equals(Locale.US)){
kelvinDegree = userDegreeVal + 459.67;
BigDecimal bdKelvin = new BigDecimal(kelvinDegree);
BigDecimal fraction = new BigDecimal(5).divide(new BigDecimal(9), 5, RoundingMode.HALF_UP);
kelvinDegree = bdKelvin.multiply(fraction).doubleValue();
}else{
kelvinDegree = userDegreeVal + 273.15;
}
setConvertedInput(kelvinDegree);
}
@Override
protected void onBeforeRender() {
super.onBeforeRender();
Double kelvinDegree = (Double) getDefaultModelObject();
Double userDegreeVal;
if(kelvinDegree == null) {
return;
}
if(getLocale().equals(Locale.US)){
BigDecimal bdKelvin = new BigDecimal(kelvinDegree);
BigDecimal fraction = new BigDecimal(9).divide(new BigDecimal(5));
kelvinDegree = bdKelvin.multiply(fraction).doubleValue();
userDegreeVal = kelvinDegree - 459.67;
}else{
userDegreeVal = kelvinDegree - 273.15;
}
userDegree.setModelObject(userDegreeVal);
}
}
对应的html页面
<html lang="en" xmlns:wicket="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<wicket:panel>
Registered temperature: <input size="3" maxlength="3" wicket:id="registeredTemperature"/>
<label wicket:id="mesuramentUnit"></label>
</wicket:panel>
</body>
</html>
wepapplication
public class WicketApplication extends WebApplication {
@Override
public Class<? extends Page> getHomePage() {
return HomePage.class;
}
}
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>wicket.AnnotationsRolesStrategyExample</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>demo.WicketApplication</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>wicket.AnnotationsRolesStrategyExample</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>