Wicketチュートリアルでは、テキストボックスを作成し、検証し、次のページにテキストボックスの値を渡す方法を説明します。

…​.//Java
import org.apache.wicket.markup.html.form.TextField;
…​

最終的なTextField <String> username =新しいTextField <String>( “username”、Model.of( “”));
form.add(username);
 //HTML
<input wicket:id = “username” type = “text” size = “20”/>

=== 1. Wicketテキストボックスの例

Wicketの `TextField`を介してテキストボックスをレンダリングするユーザーページ。

__File:UserPage.java__

パッケージcom.mkyong.user;

インポートorg.apache.wicket.PageParameters; 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.Model;

public class UserPageはWebページ{

public UserPage(最後のPageParametersパラメータ){

add(新しいFeedbackPanel( “feedback”));

最終的なTextField <String> username =新しいTextField <String>( “username”、Model.of( “”)); username.setRequired(true); username.add(新しいUsernameValidator());

フォーム<?>フォーム=新しいフォーム<Void>( “userForm”){

@Override protected void onSubmit(){

最終的な文字列usernameValue = username.getModelObject();

PageParameters pageParameters =新しいPageParameters(); pageParameters.add( “username”、usernameValue); setResponsePage(SuccessPage.class、pageParameters);

}

};

追加(フォーム); form.add(username);

}
}

__File:UserPage.html__

<html> <head> <style>ラベル{background-color:#eee;パディング:4px; }

feedbackPanelERROR {カラー:赤; } </style> <body> <h1> Wicket TextBoxの例 – UserPage.html </h1>

<wicket:id = “userForm”> <p> <label>ユーザー名</label>:<input wicket:id = “username” type = “text” <div wicket:id = “feedback”> </div> size = “20”/> <input type = “submit” value = “Register”/> </form>

</body>
</html>

=== 2.ユーザー名バリデーター

usernameの長さが5の間であることを確認するユーザ名バリデータ
15、および文字または記号a〜z、0〜9、アンダースコア、ハイフン。

__File:UsernameValidator.java__

パッケージcom.mkyong.user;

インポートorg.apache.wicket.validation.CompoundValidator;インポートorg.apache.wicket.validation.validator.PatternValidator;インポートorg.apache.wicket.validation.validator.StringValidator;

パブリッククラスUsernameValidator extends CompoundValidator <String> {プライベート静的final long serialVersionUID = 1L;

public UsernameValidator(){
    
        add(StringValidator.lengthBetween(5、15));
        add(新しいPatternValidator( “[a-z0-9 __-]+”)));
    
    }
}

=== 3.テキストボックスの値を表示する

**  UserPage.html ** のテキストボックス値を表示するページ。

__File:SuccessPage.java__

パッケージcom.mkyong.user;

インポートorg.apache.wicket.PageParameters; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.WebPage;

パブリッククラスSuccessPageはWebPage {

public SuccessPage(最後のPageParametersパラメータ){

String username = “”;
        
        if(parameters.containsKey( “username”)){
            username = parameters.getString( “username”);
        }
        
        最終ラベル結果=新しいラベル( “結果”、 “ユーザー名:” +ユーザー名);
        追加(結果);
        
    }
}

__File:SuccessPage.html__

<html> <body> <h1> Wicket TextBoxの例 – SuccessPage.html </h1>

<label wicket:id = “result”> </label>

</body>
</html>

=== 4.デモ

開始と訪問 -  __http://localhost:8080/WicketExamples/__

テキストボックスにエラーがある場合:

image://wp-content/uploads/2011/05/wicket-textbox-example1.png[wicket
テキストボックスの例、title = "wicket-textbox-example1"、width = 639、height = 326]
テキストボックスにエラーがない場合は、次のページに移動し、テキストボックスを表示します
値。

image://wp-content/uploads/2011/05/wicket-textbox-example2.png[wicket
テキストボックス、タイトル= "wicket-textbox-example2"、幅= 639、高さ= 310]
それをダウンロードする -
リンク://wp-content/uploads/2011/05/Wicket-textbox-examples.zip[Wicket-textbox-examples.zip](8KB)

===参考文献

。 link://正規表現/正規表現を使ったユーザ名 - 正規表現付きのユーザ名/[Validate
正規表現でのユーザ名]。 http://wicket.apache.org/apidocs/1.4/org/apache/wicket/markup/html/form/TextField.html[Wicket
TextField Javadoc]
link://tag/textbox/[textbox]link://タグ/wicket/[wicket]