ここでは、「Wicket とりあえず画面遷移とフォームデータの受け取りをする 」で作った、日記アプリケーションを改造して、「まともな」プログラム構造にする。
package com.snail;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public final class DiaryVO implements Serializable {
private static final long serialVersionUID = -4270110566844001704L;
public static String SUBJECT = "subject";
public static String TYPE = "type";
public static String DATE = "date";
public static String ARTICLE = "article";
private List<String> typeList;
private String subject = null;
private String type = null;
private Date date = new Date();
private String article = null;
public DiaryVO() {
typeList = new ArrayList<String>();
typeList.add("Diary");
typeList.add("Book Review");
typeList.add("Tech Memo");
typeList.add("Diet");
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getArtcle() {
return article;
}
public void setArtcle(String artcle) {
this.article = artcle;
}
public List<String> getTypeList() {
return typeList;
}
public void setTypeList(List<String> typeList) {
this.typeList = typeList;
}
}
private Model mSubject = new Model(); private Model mType = new Model(); private Model mDate = new Model(new Date()); private Model mArticle = new Model();
private DiaryVO diary;
private Form form = new Form("f") { private static final long serialVersionUID = -1700095884500348972L; @Override protected void onSubmit() { ResultPage result = new ResultPage((HomePage) this.getParent()); setResponsePage(result); } };
private Form form = new Form("f") { private static final long serialVersionUID = -1700095884500348972L; @Override protected void onSubmit() { ResultPage result = new ResultPage(diary); setResponsePage(result); } };
package com.snail;
import org.apache.wicket.PageParameters;
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.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
public class HomePage extends WebPage {
private static final long serialVersionUID = 3598788934381254916L;
private Form form = new Form("f") {
private static final long serialVersionUID = -1700095884500348972L;
@Override
protected void onSubmit() {
ResultPage result = new ResultPage(diary);
setResponsePage(result);
}
};
private DiaryVO diary;
private FeedbackPanel feedback = new FeedbackPanel("msg");
public HomePage(final PageParameters parameters) {
this(new DiaryVO());
}
public HomePage(final DiaryVO pDiary) {
this.diary = pDiary;
add(feedback);
add(form);
TextField txtSubject = new TextField("subject",
new PropertyModel(diary, DiaryVO.SUBJECT));
txtSubject.setRequired(true);
form.add(txtSubject);
DropDownChoice cmbType = new DropDownChoice("type",
new PropertyModel(diary, DiaryVO.TYPE), diary.getTypeList());
cmbType.setRequired(true);
form.add(cmbType);
TextField txtDate = new TextField("date",
new PropertyModel(diary, DiaryVO.DATE));
txtDate.setRequired(true);
form.add(txtDate);
TextArea txtArticle = new TextArea("article", new PropertyModel(diary,
DiaryVO.ARTICLE));
txtArticle.setRequired(true);
form.add(txtArticle);
}
}
package com.snail;
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.TextArea;
import org.apache.wicket.model.PropertyModel;
public class ResultPage extends WebPage {
private static final long serialVersionUID = -8680953158566804713L;
private DiaryVO diary;
private Form form = new Form("f") {
private static final long serialVersionUID = 5522440160951241211L;
@Override
protected void onSubmit() {
setResponsePage(new HomePage(diary));
}
};
public ResultPage(DiaryVO pDiary) {
this.diary = pDiary;
add(new Label("subject", new PropertyModel(diary, DiaryVO.SUBJECT)));
add(new Label("type", new PropertyModel(diary, DiaryVO.TYPE)));
add(new Label("date", new PropertyModel(diary, DiaryVO.DATE)));
add(new TextArea("article", new PropertyModel(diary, DiaryVO.ARTICLE)));
add(form);
}
}
public class HomePage extends WebPage {
private DiaryVO diary;
private FeedbackPanel feedback = new FeedbackPanel("msg");
public HomePage(final PageParameters parameters) {
this(new DiaryVO());
}
public HomePage(final DiaryVO pDiary) {
this.diary = pDiary;
add(feedback);
Form form = new Form("f", new CompoundPropertyModel(diary)) {
@Override
protected void onSubmit() {
ResultPage result = new ResultPage(diary);
setResponsePage(result);
}
});
add(form);
TextField txtSubject = new TextField("subject");
txtSubject.setRequired(true);
form.add(txtSubject);
DropDownChoice cmbType = new DropDownChoice("type", diary.getTypeList());
cmbType.setRequired(true);
form.add(cmbType);
TextField txtDate = new TextField("date");
txtDate.setRequired(true);
form.add(txtDate);
TextArea txtArticle = new TextArea("article");
txtArticle.setRequired(true);
form.add(txtArticle);
}
}