wicket 中ListView和WebMarkupContainer以及AjaxButton的使用

此例子为wicket中在页面中点检索后,在本页面显示出检索结果所用到的控件:





import java.util.ArrayList;
import java.util.List;
import jp.co.infomart.foods.common.admin.page.BasePage;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;

/**
*
* @author admin
*/
public class Test_ListView  extends BasePage {
        private Form form;
        private static List books = new ArrayList();
        WebMarkupContainer container;
       
        public Test_ListView(){
            super();
        form = new Form("myForm");
        this.add(form);
        container = new WebMarkupContainer("listViewContainer");
        container.setOutputMarkupId(true);
//       
        ListView listView = new ListView("books",books){

            @Override
            protected void populateItem(ListItem item) {
                Test_ListViewModel book = (Test_ListViewModel)item.getModelObject();
                item.add(new Label("id",Integer.toString(book.getId())));
                item.add(new Label("title",book.getTitle()));
                item.add(new Label("author",book.getAuthor()));
            }
       
        };
        container.add(listView);
        form.add(container);

        AjaxButton ajaxButton = new AjaxButton("ajaxButton") {

            @Override
            protected void onSubmit(AjaxRequestTarget art, Form<?> form) {
                init();
                art.addComponent(container);
            }

            @Override
            protected void onError(AjaxRequestTarget art, Form<?> form) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
       
        };
         form.add(ajaxButton);
        }
       
        public void init(){
            books.clear();
         for(int i=1;i<10;i++){
              Test_ListViewModel book = new Test_ListViewModel();
              book.setId(i);
              book.setAuthor("author"+i);
              book.setTitle("title"+i);
              books.add(book);
           }
       
        }
}




<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" onUnload="fCloseWin()">
    <form wicket:id="myForm">
        <div wicket:id="listViewContainer">
        <table width="240" border="1">
            <tr>
                <th>序号</th>
                <th>书名</th>
                <th>作者</th>
            </tr>
           
            <tr wicket:id="books">
                <td wicket:id="id">id</td>
                <td wicket:id="title">title</td>
                <td wicket:id="author">author</td>
            </tr>
           
        </table>
         </div>
        <input type="submit" wicket:id="ajaxButton"/>
    </form>
</body>
</html>





public class Test_ListViewModel {
    private int id;
    private String title;
    private String author;

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
   
   
}

你可能感兴趣的:(Ajax,ListView,wicket,AjaxButton)