RenderSupport Usage in Tapestry5
RenderSupport是在写组件中非常有用的一个service,主要用于提供对组件渲染的支持,可以用其导入css/javascript文件,最重要的一个功能是其可以为组件生成客户端HTML标签的唯一id。
要生成此客户端的id,可以用二个方法产生,一是RenderSupport.allocateClientId(String id),由自己指定一个id字符串,如果一个页面中包含多个相同组件,则第二个之后的组件会在此id后加上"_"和0开始的数字做为其id。
另外是用RenderSupport.allocateClientId(ComponentResources resources),resources会抽取其组件的id做为其客户端的id值。
举例写一个checkBoxes组件,需要提一点是不要分配"checkbox"做为组件的id,这个会导致server崩掉,Tapestry5的一个bug,还没有解决。
public class CheckBoxes {
@Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
private String value;
@Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
private String name;
@Parameter
private boolean checked;
@Inject
private Request request;
@Inject
private ComponentResources resources;
@Inject
private RenderSupport renderSupport;
@BeginRender
void begin(MarkupWriter writer) {
String id = renderSupport.allocateClientId("chk"); // don't use checkbox.
System.out.println(id);
Element checkbox = writer.element("input", "type", "checkbox", "name", name, "id", id, "value", value);
checkbox.attribute("checked", checked ? "checked" : null);
}
@AfterRender
void after(MarkupWriter writer) {
writer.end();
}
}
@Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
private String value;
@Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
private String name;
@Parameter
private boolean checked;
@Inject
private Request request;
@Inject
private ComponentResources resources;
@Inject
private RenderSupport renderSupport;
@BeginRender
void begin(MarkupWriter writer) {
String id = renderSupport.allocateClientId("chk"); // don't use checkbox.
System.out.println(id);
Element checkbox = writer.element("input", "type", "checkbox", "name", name, "id", id, "value", value);
checkbox.attribute("checked", checked ? "checked" : null);
}
@AfterRender
void after(MarkupWriter writer) {
writer.end();
}
}
No comments :
Post a Comment