2008-04-27
Dynamically Generated Checkboxes
关键字: tapestry checkbox
转自:http://wiki.apache.org/tapestry/Dynamically_Generated_Checkboxes?highlight=(checkbox)
If you need to generate a sequence of check boxes in a form, it can be tricky to work out in your form listener which check boxes have been selected. Here is my solution which involves reconstructing the list of check boxes when the value method of each @Checkbox component is called.
First create a Serializable object to hold the id of the check box:
This class must be serializable because Tapestry will stringify the object and use it as the value of the input element in the html output.
Then create the datasource for your checkbox list. I have used a class with a static method for demonstration purposes, but you would normally pull some data out of a database to create the list.
Then your Tapestry page class would be as follows. This page simply logs the values, but you could do whatever you like with them, e.g. put the values back into a db.
The html page is as follows.
If you need to generate a sequence of check boxes in a form, it can be tricky to work out in your form listener which check boxes have been selected. Here is my solution which involves reconstructing the list of check boxes when the value method of each @Checkbox component is called.
First create a Serializable object to hold the id of the check box:
package com.myweb.pages.admin;
import java.io.Serializable;
public class CheckboxItem implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Can be whatever you like - perhaps a primary key from a database
*/
private Serializable id;
private boolean selected;
public Serializable getId() {
return id;
}
public void setId(Serializable id) {
this.id = id;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
This class must be serializable because Tapestry will stringify the object and use it as the value of the input element in the html output.
Then create the datasource for your checkbox list. I have used a class with a static method for demonstration purposes, but you would normally pull some data out of a database to create the list.
package com.myweb.pages.admin;
import java.util.ArrayList;
import java.util.List;
public class CheckboxItemFactory {
public static List<CheckboxItem> getCheckboxItems() {
List<CheckboxItem> items = new ArrayList<CheckboxItem>();
{
CheckboxItem item = new CheckboxItem();
item.setId("ITEM1");
items.add(item);
}
{
CheckboxItem item = new CheckboxItem();
item.setId("ITEM2");
items.add(item);
}
{
CheckboxItem item = new CheckboxItem();
item.setId("ITEM3");
item.setSelected(true);
items.add(item);
}
return items;
}
}
Then your Tapestry page class would be as follows. This page simply logs the values, but you could do whatever you like with them, e.g. put the values back into a db.
package com.myweb.pages.admin;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.html.BasePage;
public abstract class MultipleCheckboxes extends BasePage implements
PageBeginRenderListener {
private static final Logger logger = Logger
.getLogger(MultipleCheckboxes.class);
public abstract void setCheckboxItems(List<CheckboxItem> checkboxItems);
public abstract List<CheckboxItem> getCheckboxItems();
public abstract void setUpdatedCheckboxItems(List<CheckboxItem> checkboxItems);
public abstract List<CheckboxItem> getUpdatedCheckboxItems();
public abstract void setCurrentItem(CheckboxItem item);
public abstract CheckboxItem getCurrentItem();
//生成需要的Checkbox列表值
public static List<CheckboxItem> getCheckboxItems1() {
List<CheckboxItem> items = new ArrayList<CheckboxItem>();
{
CheckboxItem item = new CheckboxItem();
item.setId("ITEM1");
items.add(item);
}
{
CheckboxItem item = new CheckboxItem();
item.setId("ITEM2");
items.add(item);
}
{
CheckboxItem item = new CheckboxItem();
item.setId("ITEM3");
item.setSelected(true);
items.add(item);
}
return items;
}
public boolean isCurrentItemSelected() {
return getCurrentItem().isSelected();
}
public void setCurrentItemSelected(boolean selected) {
CheckboxItem item = getCurrentItem();
item.setSelected(selected);
getUpdatedCheckboxItems().add(item);
}
public void pageBeginRender(PageEvent pageEvent) {
if (pageEvent.getRequestCycle().isRewinding()) {
logger.info("Rewinding");
setUpdatedCheckboxItems(new ArrayList<CheckboxItem>());
} else {
logger.info("Beginning full page render");
if (getUpdatedCheckboxItems() == null) {
setCheckboxItems(getCheckboxItems1());
} else {
setCheckboxItems(getUpdatedCheckboxItems());
}
}
}
public void doSubmit() {
logger.info("Form submitted");
for (CheckboxItem item : getUpdatedCheckboxItems()) {
logger.info("Item " + item.getId() + " is "
+ (item.isSelected() ? "" : "not ") + "selected");
System.out.println("Item " + item.getId() + " is "
+ (item.isSelected() ? "" : "not ") + "selected");
}
}
}
The html page is as follows.
<html> <body> <h1>Multiple Checkboxes</h1> <form jwcid="@Form"> <span jwcid="@For" source="ognl:checkboxItems" value="ognl:currentItem"> <input jwcid="@Checkbox" value="ognl:currentItemSelected"/> <span jwcid="@Insert" value="ognl:currentItem.id"/><br/> </span> <input jwcid="@Submit" listener="listener:doSubmit" value="Submit"/> </form> </body> </html>
发表评论
- 浏览: 3366 次
- 性别:

- 来自: 深圳

- 详细资料
搜索本博客
我的相册
image031
共 53 张
共 53 张
链接
最新评论
-
自定义Hibernate Dialect ...
呵呵,对于Hibernate我也算不上很熟练,大家互相学习吧,最近都很久没弄了
-- by jackytang520 -
自定义Hibernate Dialect ...
太高兴看到了。 出这样的问题. 我们这些菜鸟 是没办法解决的. 能交 ...
-- by yyy851124 -
英文Locale下使用中文输入 ...
好文章,学习了.
-- by devin






评论排行榜