jsf自定义组件-jafyear选择年份

本文介绍了一个自定义的年份选择框组件及其渲染器和标签的实现方式。该组件允许用户通过简单的表达式配置选取的年份范围,并能够显示当前年份。组件基于Java和JSF技术。
2007年10月19日 13:16:24

就是一个下拉框,可以配置选取的范围,用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.youkuaiyun.com/TrackBack.aspx?PostId=1832619


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值