shokosブログ

プログラミング

自分のコードリファクタリング

その②
CompoundPropertyModelを使ってすっきりさせる


まずは使わないバージョンについて

public HomePage() {
     Form<ValueKeeper> form=new Form<ValueKeeper>("form");
     ValueKeeper value = new ValueKeeper();
     add(form);
     TextField<String> inputID = new TextField<String>("id",new PropertyModel<String>(value,"id"));
    inputID.setRequired(true);
     form.add(inputID);
     TextField inputPass = new PasswordTextField("password",new PropertyModel<String>(value,"password"));
     inputPass.setRequired(true);
     form.add(inputPass);
     TextField<String> inputPost = new TextField<String>("text",new PropertyModel<String>(value,"text"));
    inputText.setRequired(true);
     form.add(inputPost);
     NextButton button = new NextButton("submit",value);
     form.add(button);
    }

PropertyModelを使っています。
ええっと、
TextField inputID = new TextField("コンポーネントID",new PropertyModel(value,"プロパティ式"));
となっているんですけど、
基本、コンポーネントのIDとPropertyModelのプロパティ式は一緒にするらしいです。
そのほうがわかりやすいからだそうです。


ちなみにプロパティ式とは、ValueKeeper内のフィールドのことです
TextFieldに入力された値がValueKeeper内で各プロパティ名で保存されます

ValueKeeperは以下のようになっています

import java.io.Serializable;

public class ValueKeeper  implements Serializable{
	private static final long serialVersionUID = 1L;
	private String id;
	private String password;
	private String text;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String passward) {
		this.password = passward;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}

	
}

長くなったので続きは次の記事で