就是一个下拉框,可以配置选取的范围,用today表示当期年份,输入简单表达式。
package com.cfcc.jaf.webx.component.jafdate.jafyear; import javax.faces.component.UIComponentBase; import javax.faces.context.FacesContext; /** */ /** * 年份选择框组件 * @author qinjinwei * $date 2007-9-12 上午09:52:28 */ public class JafYear extends UIComponentBase ... { public final String JAF_DATE_FAMILY = "jaf.jafdate"; public String getFamily() ...{ return JAF_DATE_FAMILY; } public Object saveState(FacesContext context) ...{ Object values[] = new Object[1]; values[0] = super.saveState(context); return values; } public void restoreState(FacesContext context, Object state) ...{ Object values[] = (Object[]) state; super.restoreState(context, values[0]); }}
package com.cfcc.jaf.webx.component.jafdate.jafyear; import java.io.IOException; import java.util.Date; import java.util.Map; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.context.ResponseWriter; import javax.faces.el.ValueBinding; import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer; /** */ /** * 年份选择框组件renderer * @author qinjinwei * $date 2007-9-12 上午09:52:30 */ public class JafYearRenderer extends HtmlRenderer ... { public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException ...{ String sfrom = (String) component.getAttributes().get("from"); String sto = (String) component.getAttributes().get("to"); int ifrom = convert(sfrom) + 1900; int ito = convert(sto) + 1900; ResponseWriter writer = facesContext.getResponseWriter(); String clientId = component.getClientId(facesContext); ValueBinding vb = component.getValueBinding("value"); String syear = (String) vb.getValue(facesContext); if(syear == null) ...{ syear = "" + (new Date().getYear() + 1900 ); } int year = Integer.parseInt(syear); writer.write(">select id="" + clientId + "" name="" + clientId + ""< "); for (int i = ifrom; i >= ito; i++) ...{ writer.write(">option value="" + i + """); if(year == i) ...{ writer.write(" selected "); } writer.write("<" + i ); writer.write("年>/option< "); } writer.write(">/select<"); } public void decode(FacesContext facesContext, UIComponent uiComponent) ...{ Map paramMap = facesContext.getExternalContext() .getRequestParameterMap(); String clientId = uiComponent.getClientId(facesContext); if(paramMap.containsKey(clientId)) ...{ String value = (String) paramMap .get(clientId); ValueBinding vb = uiComponent.getValueBinding("value"); vb.setValue(facesContext, value); System.out.println(clientId + " value is: " + value); } } private int convert(String exp) ...{ boolean bflagt = false; boolean bflags = false; String ex = exp.replaceAll(" ", ""); if (ex.startsWith("today")) ...{ bflagt = true; ex = ex.replaceAll("today", ""); } if (ex.startsWith("+")) ...{ bflags = true; } ex = ex.substring(1, ex.length()); int rvalue = Integer.parseInt(ex); if(!bflags) ...{ rvalue = - rvalue; } if (bflagt) rvalue = rvalue + new Date().getYear(); return rvalue; }}
package com.cfcc.jaf.webx.component.jafdate.jafyear; import javax.faces.application.Application; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.el.ValueBinding; import javax.faces.webapp.UIComponentTag; /** */ /** * 年份选择框组件的标签 * @author qinjinwei * $date 2007-9-12 上午09:52:36 */ public class JafYearTag extends UIComponentTag ... { public String getComponentType() ...{ return "com.cfcc.jaf.webx.component.jafdate.jafyear.JafYear"; } public String getRendererType() ...{ return "com.cfcc.jaf.webx.component.jafdate.jafyear.JafYearRenderer"; } private String from; private String to; private String value; public String getValue() ...{ return value; } public void setValue(String value) ...{ this.value = value; } public String getFrom() ...{ return from; } public void setFrom(String from) ...{ this.from = from; } public String getTo() ...{ return to; } public void setTo(String to) ...{ this.to = to; } public void setProperties(UIComponent component) ...{ super.setProperties(component); setStringProperty(component, "from", from); setStringProperty(component, "to", to); setStringProperty(component, "value", value); } private void setStringProperty(UIComponent component, String attrName, String attrValue) ...{ if (attrValue == null) return; if (isValueReference(attrValue)) ...{ FacesContext context = FacesContext.getCurrentInstance(); Application application = context.getApplication(); ValueBinding binding = application.createValueBinding(attrValue); component.setValueBinding(attrName, binding); } else ...{ component.getAttributes().put(attrName, attrValue); } } public void release() ...{ super.release(); from = null; to = null; value = null; } }
> tag < > name < jafyear >/ name < > tag-class < com.cfcc.jaf.webx.component.jafdate.jafyear.JafYearTag >/ tag-class < > body-content < empty >/ body-content < > attribute < > name < from >/ name < > required < true >/ required < >/ attribute < > attribute < > name < to >/ name < > required < true >/ required < >/ attribute < > attribute < > name < value >/ name < > required < true >/ required < >/ attribute < >/ tag <
> renderer < > component-family < jaf.jafdate >/ component-family < > renderer-type < com.cfcc.jaf.webx.component.jafdate.jafyear.JafYearRenderer >/ renderer-type < > renderer-class < com.cfcc.jaf.webx.component.jafdate.jafyear.JafYearRenderer >/ renderer-class < >/ renderer < > component < > component-type < com.cfcc.jaf.webx.component.jafdate.jafyear.JafYear >/ component-type < > component-class < com.cfcc.jaf.webx.component.jafdate.jafyear.JafYear >/ component-class < >/ component < Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1832619