博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jsf自定义组件-jafyear选择年份
阅读量:4190 次
发布时间:2019-05-26

本文共 5684 字,大约阅读时间需要 18 分钟。

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.csdn.net/TrackBack.aspx?PostId=1832619

你可能感兴趣的文章
How to Perform an Upgrade from Icehouse to Juno(ice升级到juno)
查看>>
高扩展性网站的50条原则(转)-思维导图
查看>>
解决openstack novnc一段时间后自动挂断登录不上问题,novncproxy dead but pid file exists
查看>>
构建OpenStack的云基础架构:ManageIQ(转)
查看>>
云管理软件 ManageIQ(转)
查看>>
CentOS 7.0,启用iptables防火墙(转)
查看>>
DISCUZ浅析之COOKIE篇
查看>>
实战DDD(Domain-Driven Design领域驱动设计:Evans DDD)
查看>>
SSH中各个框架的作用以及Spring AOP,IOC,DI详解
查看>>
openstack juno 配置vmware(vcenter、vsphere)
查看>>
远程debug调试(eclipse)之openstack windows
查看>>
PAAS平台对比:OpenShift VS CloudFoundry【51CTO调研报告】
查看>>
JAX-RS(java restful实现讲解)(转)
查看>>
Spring MVC与JAX-RS比较与分析
查看>>
openstack官方docker介绍
查看>>
头痛与早餐
查看>>
[转]在ASP.NET 2.0中操作数据::创建一个数据访问层
查看>>
Linux命令之chmod详解
查看>>
【java小程序实战】小程序注销功能实现
查看>>
linux下文件夹的创建、复制、剪切、重命名、清空和删除命令
查看>>