読者です 読者をやめる 読者になる 読者になる

いろんなJava Webフレームワークで同じ画面を作ってみる(Wicket編)

いろんなJava Webフレームワークで同じ画面を作ってみる(Wicket編)です。
お題は、こちら

Wicketの特徴は、

といったところでしょうか。

(追記:2010/12/13)コメント頂き、Wicketの場合、画面を分けるのはおかしいとのことで一緒にしました。また、CompoundPropertyModel使ったらちょっとすっきりしました。

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
   version="2.4">
  <display-name>wicket</display-name>
  <filter>
    <filter-name>wicket.add</filter-name>
     <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
    <init-param>
      <param-name>applicationClassName</param-name>
      <param-value>webapp.AddApplication</param-value>
     </init-param>
   </filter>
  <filter-mapping>
    <filter-name>wicket.add</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

applicationClassName にアプリケーションクラスを指定します。

AddApplication.java

package webapp;

import org.apache.wicket.protocol.http.WebApplication;

public class AddApplication extends WebApplication {
    public AddApplication() {
    }

    public Class<AddPage> getHomePage() {
        return AddPage.class;
    }
}

AddPage.java

package webapp;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;

public class AddPage extends WebPage {

    private static final long serialVersionUID = 1L;

    private Integer arg1;
    private Integer arg2;

    public AddPage() {
        Form<AddPage> form = new Form<AddPage>("form",
                new CompoundPropertyModel<AddPage>(this));

        form.add(new TextField<Integer>("arg1").setRequired(true));
        form.add(new TextField<Integer>("arg2").setRequired(true));
        form.add(new Label("result", new AbstractReadOnlyModel<Integer>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Integer getObject() {
                if (arg1 == null || arg2 == null) {
                    return null;
                }
                return arg1 + arg2;
            }
        }));

        this.add(form, new FeedbackPanel("feedback"));
    }
}

addPage.html

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
<head>
<title>add</title>
</head>
<body>
<div wicket:id="feedback"></div>
<form wicket:id="form">
arg1:<input wicket:id="arg1" type="text" /> +
arg2:<input wicket:id="arg2" type="text" /> =
<span wicket:id="result"></span>
<br/>
<input type="submit" />
</form>
</body>
</html>

以下、修正前のコードです。

CompoundPropertyModelを使うように修正しておりますが。

AddApplication.java
package webapp;

import org.apache.wicket.protocol.http.WebApplication;

public class AddApplication extends WebApplication {
    public AddApplication() {
    }

    public Class<InputPage> getHomePage() {
        return InputPage.class;
    }
}
InputPage.java
package webapp;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.CompoundPropertyModel;

public class InputPage extends WebPage {

    private static final long serialVersionUID = 1L;

    private Integer arg1;
    private Integer arg2;

    public InputPage() {

        Form<InputPage> form = new Form<InputPage>("form",
                new CompoundPropertyModel<InputPage>(this));

        Button submit = new Button("submit") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit() {
                this.setResponsePage(new ResultPage(arg1 + arg2));
            }
        };

        form.add(new TextField<Integer>("arg1").setRequired(true),
                new TextField<Integer>("arg2").setRequired(true), submit);

        this.add(form);
        this.add(new FeedbackPanel("error"));
    }
}
ResultPage.java
package webapp;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.Model;

public class ResultPage extends WebPage {

    private static final long serialVersionUID = 1L;

    public ResultPage(Integer result) {
        this.add(new Label("result", new Model<Integer>(result)));
    }
}
InputPage.html
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" >
<head>
  <title>input</title>
</head>
<body>
  <div wicket:id="error"></div>
  <form wicket:id="form">
  arg1: <input wicket:id="arg1" type="text" /><br/>
  arg2: <input wicket:id="arg2" type="text" /><br/>
  <input wicket:id="submit" type="submit" />
  </form>
</body>
</html>
ResultPage.html
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" >
<head>
  <title>input</title>
</head>
<body>
  result: <span wicket:id="result">dummy</span>
</body>
</html>

以上