《wicket学习十》&CustomFormComponentPanel(温度转换)

24 篇文章 1 订阅

今天我们来学习一下自定义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>&nbsp;
            &nbsp;(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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yGIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值