您的位置:首页 > Web前端 > JavaScript

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

2008-04-09 22:49 459 查看
 
就是一个下拉框,可以配置选取的范围,用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("" + clientId + "" name="" + clientId + ""> ");




        for (int i = ifrom; i <= ito; i++) ...{


            writer.write("" + i + """);


            if(year == i)




            ...{


                writer.write(" selected ");


            }


            writer.write(">" + i );


            writer.write("年 ");


        }


        writer.write("");


    }


    


    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;


    }


    


    



 





        jafyear


        


            com.cfcc.jaf.webx.component.jafdate.jafyear.JafYearTag


        



        empty


        


            from


            true


        



        


            to


            true


        



        


            value


            true


        



    
 





            jaf.jafdate


            com.cfcc.jaf.webx.component.jafdate.jafyear.JafYearRenderer


            com.cfcc.jaf.webx.component.jafdate.jafyear.JafYearRenderer


        





    


        


            com.cfcc.jaf.webx.component.jafdate.jafyear.JafYear


        



        


            com.cfcc.jaf.webx.component.jafdate.jafyear.JafYear


        



    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: