import java.text.DecimalFormat; import java.text.ParseException; import java.util.List; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.util.GenericAutowireComposer; import org.zkoss.zkplus.databind.AnnotateDataBinder; import org.zkoss.zkplus.databind.DataBinder; import com.sun4love.common.web.zk.converter.SexRadiogroupConverter; /** * 注解绑定类 * <p> * 在GenericAutowireComposer基础上对注解进行增强, 因此你无需再页面上显式添加注解支持指令 * * <pre> * <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?> * </pre> * <p> * * @author sun4love * */ abstract public class GenericDataBinderComposer extends GenericAutowireComposer { private static final long serialVersionUID = -8962566563467903754L; protected DataBinder binder; @Override public void doAfterCompose(Component comp) throws Exception { super.doAfterCompose(comp); binder = new AnnotateDataBinder(comp); comp.setAttribute("binder", binder); } protected void loadAll() { binder.loadAll(); } protected void loadAttribute(Component comp, String attr) { binder.loadAttribute(comp, attr); } protected void loadComponent(Component comp) { binder.loadComponent(comp); } /** * 绑定bean到ui组件上 * <p> * 范例 * <hr /> * * <pre> * <code> * <window id="userWin" apply="GenericDataBinderComposerChild"> * <textbox id="txtFirstName" /> * <textbox id="txtlastName" /> * <label id="lblFullName" /> * </window> * GenericDataBinderComposerChild类 * * public GenericDataBinderComposerChild extends GenericDataBinderComposer{ * private Textbox txtFirstName; * private Textbox txtLastName; * private Label lblFullName; * * public void doAfterCompose(Component comp) throws Exception { * super.doAfterCompose(comp); * binder.addBinding(txtFirstName, "value", "userWin$composer.user.firstName"); * binder.addBinding(txtLastName, "value", "userWin$composer.user.lastName"); * * //"txtLastName.onBlur"中的txtLastName是页面上<textbox id="txtlastName" /> * //userWin$composer中的userWin是页面上window的id,$.composer是固定格式 * addBinding(lblFullName, "value", "userWin$composer.user.fullName","txtLastName.onBlur" , null,null,null); * } * } * </pre> * * @param comp * ui组件 * @param attr * ui组件的属性,例如<textbox value="@{user.name}"/>中的value * @param expr * 表达式,例如,例如<textbox value="@{user.name}"/>中的user.name * @param loadWhenEvents * 加载bean到ui的时机,例如comp1.onClck、comp2、onBlur、comp3.onSelect, * 等同于<textbox id="txtFullName" * value="@{user.fullName, load-when= * 'txtLastName.onBlur'}"/>中的txtLastName.onBlur * @param saveWhenEvent * 保存ui value到bean的时机,例如comp3.onBlur, 等同于<textbox * id="txtFullName" value="@{user.fullName, * save-when= * 'txtFullName.onBlur',}"/>中的txtFullName.onBlur * @param access * 访问安全性,允许保存ui value 到bean,还是是load bean到ui,还是两者都是,或者什么都不做, * 可选值为none,save,load,both(save/load), * @param converter * 转换器,ui到bean之间的互相转换,类似hibernate jdbc type到db type或者struts中的转换器 * ,样例见{@link SexRadiogroupConverter} */ protected void addBinding(Component comp, String attr, String expr, String loadWhenEvent, String saveWhenEvent, String access, String converter) { if (loadWhenEvent != null && "".equals(loadWhenEvent.trim())) { binder.addBinding(comp, attr, expr, (List) null, saveWhenEvent, access, converter); } else { binder.addBinding(comp, attr, expr, new String[] { loadWhenEvent }, saveWhenEvent, access, converter); } } /** * 绑定bean到ui组件上 * <p> * 范例 * <hr /> * * <pre> * 页面 * <window id="userWin" apply="GenericDataBinderComposerChild"> * <textbox id="txtFirstName" /> * <textbox id="txtlastName" /> * <label id="lblFullName" /> * </window> * GenericDataBinderComposerChild类 * * public GenericDataBinderComposerChild extends GenericDataBinderComposer{ * private Textbox txtFirstName; * private Textbox txtLastName; * private Label lblFullName; * * public void doAfterCompose(Component comp) throws Exception { * super.doAfterCompose(comp); * binder.addBinding(txtFirstName, "value", "userWin$composer.user.firstName"); * binder.addBinding(txtLastName, "value", "userWin$composer.user.lastName"); * * //"txtLastName.onBlur"中的txtLastName是页面上<textbox id="txtlastName" /> * //userWin$composer中的userWin是页面上window的id,$.composer是固定格式 * addBinding(lblFullName, "value", "userWin$composer.user.fullName","txtLastName.onBlur" , null,null,null); * } * } * </pre> * * @param comp * ui组件 * @param attr * ui组件的属性,例如<textbox value="@{user.name}"/>中的value * @param expr * 表达式,例如,例如<textbox value="@{user.name}"/>中的user.name * @param access * 访问安全性,允许保存ui value 到bean,还是是load bean到ui,还是两者都是,或者什么都不做, * 可选值为none,save,load,both(save/load), */ protected void addBinding(Component comp, String attr, String expr, String access) { binder.addBinding(comp, attr, expr, (List) null, (List) null, access, null); } /** * 绑定bean到ui组件上 * <p> * 范例 * <hr /> * * <pre> * <window id="userWin" apply="GenericDataBinderComposerChild"> * <textbox id="txtFirstName" /> * <textbox id="txtlastName" /> * <label id="lblFullName" /> * </window> * GenericDataBinderComposerChild类 * * public GenericDataBinderComposerChild extends GenericDataBinderComposer{ * private Textbox txtFirstName; * private Textbox txtLastName; * private Label lblFullName; * * public void doAfterCompose(Component comp) throws Exception { * super.doAfterCompose(comp); * binder.addBinding(txtFirstName, "value", "userWin$composer.user.firstName"); * binder.addBinding(txtLastName, "value", "userWin$composer.user.lastName"); * * //"txtLastName.onBlur"中的txtLastName是页面上<textbox id="txtlastName" /> * //userWin$composer中的userWin是页面上window的id,$.composer是固定格式 * addBinding(lblFullName, "value", "userWin$composer.user.fullName","txtLastName.onBlur" , null,null,null); * } * } * * </pre> * * @param comp * ui组件 * @param attr * ui组件的属性,例如<textbox value="@{user.name}"/>中的value * @param expr * 表达式,例如,例如<textbox value="@{user.name}"/>中的user.name * @param loadWhenEvents * 加载bean到ui的时机,例如comp1.onClck、comp2、onBlur、comp3.onSelect, * 等同于<textbox id="txtFullName" * value="@{user.fullName, load-when= * 'txtLastName.onBlur'}"/>中的txtLastName.onBlur * @param access * 访问安全性,允许保存ui value 到bean,还是是load bean到ui,还是两者都是,或者什么都不做, * 可选值为none,save,load,both(save/load), * @param converter * 转换器,ui到bean之间的互相转换,类似hibernate jdbc type到db type或者struts中的转换器 * ,样例见{@link SexRadiogroupConverter} */ protected void addBinding(Component comp, String attr, String expr, String access, String converter) { binder.addBinding(comp, attr, expr, (List) null, (List) null, access, converter); } }