此组件为一个JSPF文件(相当于JSP中的静态导入),先说说在任意页面中应用此组件的一段事例代码,如下:
<webuijsf:body binding="#{Dijisousuo.body1}" id="body1" style="-rave-layout: grid">
<webuijsf:form binding="#{Dijisousuo.form1}" id="form1">
<h:dataTable binding="#{Dijisousuo.dataTable1}" border="1" cellpadding="1" cellspacing="1" headerClass="list-header" id="dataTable1"
rowClasses="list-row-even,list-row-odd"
style="background-color: aqua; color: purple; font-size: 12px; font-style: normal; font-weight: bolder; height: 70px; left: 120px; top: 144px; position: absolute"
value="#{FenyeComponent.datalist}" var="currentRow" width="336">
<h:column binding="#{Dijisousuo.column5}" id="column5">
<h:outputText binding="#{Dijisousuo.outputText9}" id="outputText9"
style="color: green; font-size: 14px; font-style: normal; font-weight: bolder" value="#{FenyeComponent.jiluxuhao}"/>
</h:column>
<h:column binding="#{Dijisousuo.column1}" id="column1">
<h:outputLink target="_blank" value="WoDEXiaoWu.jsp">
<h:outputText binding="#{Dijisousuo.outputText1}" id="outputText1" value="#{currentRow.id}"/>
<f:param name="huiyuanid" value="#{currentRow.id}"/>
</h:outputLink>
<f:facet name="header">
<h:outputText binding="#{Dijisousuo.outputText2}" id="outputText2" value="会员ID"/>
</f:facet>
</h:column>
<h:column binding="#{Dijisousuo.column2}" id="column2">
<h:outputLink target="_blank" value="WoDEXiaoWu.jsp">
<h:outputText binding="#{Dijisousuo.outputText3}" id="outputText3" value="#{currentRow.nicheng}"/>
<f:param name="huiyuanid" value="#{currentRow.id}"/>
</h:outputLink>
<f:facet name="header">
<h:outputText binding="#{Dijisousuo.outputText4}" id="outputText4" value="会员呢称"/>
</f:facet>
</h:column>
<h:column binding="#{Dijisousuo.column3}" id="column3">
<h:outputText binding="#{Dijisousuo.outputText5}" id="outputText5" value="#{currentRow.shifouzaixian}"/>
<f:facet name="header">
<h:outputText binding="#{Dijisousuo.outputText6}" id="outputText6" value="是否在线"/>
</f:facet>
</h:column>
<h:column binding="#{Dijisousuo.column4}" id="column4">
<h:outputText binding="#{Dijisousuo.outputText7}" id="outputText7" value="#{currentRow.email}"/>
<f:facet name="header">
<h:outputText binding="#{Dijisousuo.outputText8}" id="outputText8" value="会员Email"/>
</f:facet>
</h:column>
</h:dataTable>
<h:messages binding="#{Dijisousuo.messageList1}" errorClass="errorMessage" fatalClass="fatalMessage" id="messageList1"
infoClass="infoMessage"
style="border- 5px; border-style: groove; font-size: 14px; font-style: normal; font-weight: bolder; left: 168px; top: 72px; position: absolute" warnClass="warnMessage"/>
[color=royalblue]<div style="left: 24px; top: 264px; position: absolute">
<jsp:directive.include file="FenyeComponent.jspf"/>
</div>[/color] </webuijsf:form>
</webuijsf:body>
同时在该页面文件的页面Bean的init方法中加入如下代码(其目的是把查询语句和参数和参数类型传递给该分页组件):
以下是代码:
String chaxunyuju="from HuiYuan as hy where hy.xingbie=:xb and hy.shifoushangchuanzhaopian=:sfsczp and hy.chushengriqi>=:csrq1 and hy.chushengriqi<=:csrq2 and hy.gongzuosheng=:gzsheng and hy.gongzuoshi=:gzshi";
Class[] type=new Class[]{String.class,Boolean.class,java.sql.Date.class,java.sql.Date.class,String.class,String.class};
HashMap map=new HashMap();
map.put("xb",getSessionBean1().getXingbie());
map.put("sfsczp",new Boolean(getSessionBean1().isZhaopian()));
map.put("csrq1",getSessionBean1().getD1());
map.put("csrq2",getSessionBean1().getD2());
map.put("gzsheng",getSessionBean1().getSheng());
map.put("gzshi",getSessionBean1().getShi());
JSFUtil.getSession().setAttribute("chaxunyuju",chaxunyuju);
/*JSFUtil类为自定义的实用类,它的此方法为获取当前Session,你完全可以自己来获取Session而不用这个实用类。*/
JSFUtil.getSession().setAttribute("canshuclassarray",type);
JSFUtil.getSession().setAttribute("canshuzhimap",map);
JSFUtil.getSession().setAttribute("meiyexianshishu",null);
好了,应用此组件的事例代码如上面所示,下面的是该分页组件本身的代码,它包括一个.jspf页面文件和一个对应的request范围Bean,其.jspf页面如下:
<?xml version="1.0" encoding="UTF-8"?>
<div style="-rave-layout: grid; 400px; height: 200px" xmlns:f="[url]http://java.sun.com/jsf/core"[/url] xmlns:h="[url]http://java.sun.com/jsf/html"[/url] xmlns:webuijsf="[url]http://www.sun.com/webui/webuijsf">[/url]
<f:subview id="FenyeComponent">
<h:panelGrid binding="#{FenyeComponent.feyetable}" border="0" columns="4" id="feyetable" rowClasses="Btn1Hov_sun4 Btn2_sun4"
style="height: 24px; left: 24px; top: 72px; position: absolute" width="550">
<webuijsf:panelGroup binding="#{FenyeComponent.jlzsgroup}" id="jlzsgroup">
<h:outputText binding="#{FenyeComponent.zgtext}" id="zgtext" value="总共"/>
<h:outputText binding="#{FenyeComponent.jlzsbing}" id="jlzsbing" style="color: red; font-style: normal; font-weight: bolder" value="#{sessionScope.jiluzongshu}"/>
<h:outputText binding="#{FenyeComponent.tjltext}" id="tjltext" value="条记录"/>
</webuijsf:panelGroup>
<webuijsf:panelGroup binding="#{FenyeComponent.curryegroup}" id="curryegroup">
<h:outputText binding="#{FenyeComponent.dqwdtext}" id="dqwdtext" value="当前为第"/>
<h:outputText binding="#{FenyeComponent.dqyhbing}" id="dqyhbing" style="color: red; font-style: normal; font-weight: bolder" value="#{sessionScope.dangqianyehao}"/>
<h:outputText binding="#{FenyeComponent.ygtext}" id="ygtext" value="页/共"/>
<h:outputText binding="#{FenyeComponent.zysbing}" id="zysbing" style="color: red; font-style: normal; font-weight: bolder" value="#{sessionScope.zongyeshu}"/>
<h:outputText binding="#{FenyeComponent.yetext}" id="yetext" value="页"/>
</webuijsf:panelGroup>
<webuijsf:panelGroup binding="#{FenyeComponent.anniugroup}" id="anniugroup">
<h:commandButton action="#{FenyeComponent.firstb_action}" binding="#{FenyeComponent.firstb}" id="firstb" value="首页"/>
<h:commandButton action="#{FenyeComponent.previousb_action}" binding="#{FenyeComponent.previousb}" id="previousb" value="上一页"/>
<h:commandButton action="#{FenyeComponent.nextb_action}" binding="#{FenyeComponent.nextb}" id="nextb" value="下一页"/>
<h:commandButton action="#{FenyeComponent.lastb_action}" binding="#{FenyeComponent.lastb}" id="lastb" value="末页"/>
</webuijsf:panelGroup>
<webuijsf:panelGroup binding="#{FenyeComponent.tiaozgroup}" id="tiaozgroup">
<h:outputText binding="#{FenyeComponent.tzddtext}" id="tzddtext" value="跳转到第"/>
<h:selectOneMenu binding="#{FenyeComponent.yedown}" id="yedown" onchange="form1.submit();" value="#{sessionScope.dangqianyehao}" valueChangeListener="#{FenyeComponent.yedown_processValueChange}">
<f:selectItems binding="#{FenyeComponent.dropdown1SelectItems}" id="dropdown1SelectItems" value="#{FenyeComponent.dropdown1DefaultItems}"/>
</h:selectOneMenu>
<h:outputText binding="#{FenyeComponent.ytext}" id="ytext" value="页"/>
</webuijsf:panelGroup>
</h:panelGrid>
</f:subview>
</div>
此.jspf页面文件对应的Bean如下:
以下是代码:
package jsfzuopin;
import com.sun.rave.faces.data.DefaultSelectItemsArray;
import com.sun.rave.web.ui.appbase.AbstractFragmentBean;
import com.sun.webui.jsf.component.PanelGroup;
import java.util.HashMap;
import java.util.List;
import javax.faces.FacesException;
import javax.faces.component.UISelectItems;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import org.hibernate.Query;
public class FenyeComponent extends AbstractFragmentBean {
private int __placeholder;
public List datalist;/*代表一页的数据的集合,每页的数据都会不同,整个分页过程都是围绕着如何获取这一页的数据,应用该分页组件的页面中的数据表格的value属性只需要绑定到该分页组件的这一集合型的属性就OK了.*/
public String chaxunyuju;/*代表获得每页的集合所需要查询语句,应用该分页组件的页面的初始化方法应该向改组件传递这个查询字符串,方法是把要传递的字符串以"chaxunyuju"作为Key保存到session中,该组件的这一属性的get方法会自动向session中获取.*/
public int kaishiweizhi=0;/*分页的关键就是确定从哪条记录开始取,一次取meiyexianshishu条记录.*/
public int meiyexianshishu=3;/*分页时每页显示记录的数量,默认为3条,如需改变则在应用该分页组件的页面的初始化方法中向session中以"meiyexianshishu"作为Key保存一个整数值,如果想采用默认值,则将null值保存到session中的这一属性,该组件的这一属性的get方法会优先从session中获取这个值.*/
public HashMap canshuzhimap;//该属性代表查询语句中命名参数的值的一个映射,应用该组件的页面应该将查询语句中命名参数的值以映射的形式传递给该组件,传递的方法也是保存到session中以"canshuzhimap"为键,如果查询语句不带参数则向session中的这一属性保存保存null值.*/
public Class[] canshuclassarray;//该属性代表查询语句中命名参数的值的类型一个数组,应用该组件的页面应该将查询语句中命名参数的值的类型以Class数组的形式传递给
该组件,传递的方法也是保存到session中以"canshuclassarray"为键,如果查询语句不带参数则向session中的这一属性保存保存null值.*/
public int jiluxuhao=0;/*代表记录集中记录的序号,如果要在页面中显示记录的序号,则应该绑定到该组件的这一属性.*/
/**
* public int jiluzongshu;符合查询语句的记录的总数,每次用到时都从session中获取所以把该字段作为session的属性了.
*public int zongyeshu;代表总的页数,每次用到时都从session中获取所以把该字段作为session的属性了.
*public int dangqianyehao=1;代表当前页的页号,初始值为1,每次用到时都从session中获取所以把该字段作为session的属性了.
*/
//此方法根据记录总数计算出总页数并将其保存到Session里
public void jisuanAndSaveZongyeshu(){
Integer integer=(Integer)JSFUtil.getSession().getAttribute("jiluzongshu");
int i=integer.intValue();
if(i<getMeiyexianshishu()){
JSFUtil.getSession().setAttribute("zongyeshu",new Integer(1));
}
else if(i%getMeiyexianshishu()==0){
JSFUtil.getSession().setAttribute("zongyeshu",new Integer(i/getMeiyexianshishu()));
}else{
JSFUtil.getSession().setAttribute("zongyeshu",new Integer(i/getMeiyexianshishu()+1));
}
}
//此方法根据查询字符串获取数据库中满足条件的记录的总数并将总数保存到session
public void getAndSaveJiluzongshu(){
if(chaxunyuju.equals("test")){
JSFUtil.getSession().setAttribute("jiluzongshu",new Integer(10));
}else{
String countyuju="select count(*)"+chaxunyuju;
Query query=HBUtil.createQueryWithoutSessionClose(countyuju);
if(getCanshuzhimap()!=null&&getCanshuclassarray()!=null){
HashMap hm=getCanshuzhimap();
Class[] c=getCanshuclassarray();
String[] parameternames=query.getNamedParameters();
for(int i=0;i<parameternames.length;i++){
query.setParameter(parameternames[i],c[i].cast(hm.get(parameternames[i])));
}
}
List jilushulist=query.list();
HBUtil.closeSession();
if(jilushulist.size()!=0){
Integer integer=(Integer)jilushulist.get(0);
JSFUtil.getSession().setAttribute("jiluzongshu",integer);
}else{
JSFUtil.getSession().setAttribute("jiluzongshu",new Integer(0));
}
}
}
//该方法被该组件的init方法调用
private void _init() throws Exception {
//初始化时先检查查询语句是否改变,并获取后保存在属性chaxunyuju中
chaxunyuju=getChaxunyuju();
//如果session中没有记录总数则查询出来后存入session,如果有就不用了,目的是只查询一次记录总数
if(JSFUtil.getSession().getAttribute("jiluzongshu")==null){
getAndSaveJiluzongshu();
}
//如果session中没有总页数则计算出来后存入session,因为后面用到的总页数都从session中获取
if(JSFUtil.getSession().getAttribute("zongyeshu")==null){
jisuanAndSaveZongyeshu();
}
//如果session中没有当前页号则设置session中的当前页号为1,
//因为当前页号是不断变化的所以以后的程序都是围绕着设置session里的这个当前页号
//只有确定当前页的页号才能计算出这一页记录的开始位置也就是kaishiweizhi属性
if(JSFUtil.getSession().getAttribute("dangqianyehao")==null){
JSFUtil.getSession().setAttribute("dangqianyehao",new Integer(1));
}
//以下为在初始化时根据总页数动态的设置下拉列表框的的选项
Integer in=(Integer)JSFUtil.getSession().getAttribute("zongyeshu");
int zys=in.intValue();
dropdown1DefaultItems.clear();
for(int i=1;i<=zys;i++){
Integer integer=new Integer(i);
SelectItem si=new SelectItem(integer,integer.toString());
dropdown1DefaultItems.add(si);
}
//以下为在初始化时设置首页按牛和上一页按牛的的状态为禁用
Integer integer=(Integer)JSFUtil.getSession().getAttribute("dangqianyehao");
if(integer.intValue()==1){
firstb.setDisabled(true);
previousb.setDisabled(true);
}
//以下为在初始化时如果总页数=1则设置下一页和末页按牛的状态为禁用
Integer i=(Integer)JSFUtil.getSession().getAttribute("zongyeshu");
if(i.intValue()==1){
nextb.setDisabled(true);
lastb.setDisabled(true);
}
}
private HtmlPanelGrid feyetable = new HtmlPanelGrid();
public HtmlPanelGrid getFeyetable() {
return feyetable;
}
public void setFeyetable(HtmlPanelGrid hpg) {
this.feyetable = hpg;
}
private PanelGroup jlzsgroup = new PanelGroup();
public PanelGroup getJlzsgroup() {
return jlzsgroup;
}
public void setJlzsgroup(PanelGroup pg) {
this.jlzsgroup = pg;
}
private HtmlOutputText zgtext = new HtmlOutputText();
public HtmlOutputText getZgtext() {
return zgtext;
}
public void setZgtext(HtmlOutputText hot) {
this.zgtext = hot;
}
private HtmlOutputText jlzsbing = new HtmlOutputText();
public HtmlOutputText getJlzsbing() {
return jlzsbing;
}
public void setJlzsbing(HtmlOutputText hot) {
this.jlzsbing = hot;
}
private HtmlOutputText tjltext = new HtmlOutputText();
public HtmlOutputText getTjltext() {
return tjltext;
}
public void setTjltext(HtmlOutputText hot) {
this.tjltext = hot;
}
private PanelGroup anniugroup = new PanelGroup();
public PanelGroup getAnniugroup() {
return anniugroup;
}
public void setAnniugroup(PanelGroup pg) {
this.anniugroup = pg;
}
private HtmlCommandButton firstb = new HtmlCommandButton();
public HtmlCommandButton getFirstb() {
return firstb;
}
public void setFirstb(HtmlCommandButton hcb) {
this.firstb = hcb;
}
private HtmlCommandButton previousb = new HtmlCommandButton();
public HtmlCommandButton getPreviousb() {
return previousb;
}
public void setPreviousb(HtmlCommandButton hcb) {
this.previousb = hcb;
}
private HtmlCommandButton nextb = new HtmlCommandButton();
public HtmlCommandButton getNextb() {
return nextb;
}
public void setNextb(HtmlCommandButton hcb) {
this.nextb = hcb;
}
private HtmlCommandButton lastb = new HtmlCommandButton();
public HtmlCommandButton getLastb() {
return lastb;
}
public void setLastb(HtmlCommandButton hcb) {
this.lastb = hcb;
}
private PanelGroup curryegroup = new PanelGroup();
public PanelGroup getCurryegroup() {
return curryegroup;
}
public void setCurryegroup(PanelGroup pg) {
this.curryegroup = pg;
}
private HtmlOutputText dqwdtext = new HtmlOutputText();
public HtmlOutputText getDqwdtext() {
return dqwdtext;
}
public void setDqwdtext(HtmlOutputText hot) {
this.dqwdtext = hot;
}
private HtmlOutputText dqyhbing = new HtmlOutputText();
public HtmlOutputText getDqyhbing() {
return dqyhbing;
}
public void setDqyhbing(HtmlOutputText hot) {
this.dqyhbing = hot;
}
private HtmlOutputText ygtext = new HtmlOutputText();
public HtmlOutputText getYgtext() {
return ygtext;
}
public void setYgtext(HtmlOutputText hot) {
this.ygtext = hot;
}
private HtmlOutputText zysbing = new HtmlOutputText();
public HtmlOutputText getZysbing() {
return zysbing;
}
public void setZysbing(HtmlOutputText hot) {
this.zysbing = hot;
}
private HtmlOutputText yetext = new HtmlOutputText();
public HtmlOutputText getYetext() {
return yetext;
}
public void setYetext(HtmlOutputText hot) {
this.yetext = hot;
}
private PanelGroup tiaozgroup = new PanelGroup();
public PanelGroup getTiaozgroup() {
return tiaozgroup;
}
public void setTiaozgroup(PanelGroup pg) {
this.tiaozgroup = pg;
}
private HtmlOutputText tzddtext = new HtmlOutputText();
public HtmlOutputText getTzddtext() {
return tzddtext;
}
public void setTzddtext(HtmlOutputText hot) {
this.tzddtext = hot;
}
private HtmlSelectOneMenu yedown = new HtmlSelectOneMenu();
public HtmlSelectOneMenu getYedown() {
return yedown;
}
public void setYedown(HtmlSelectOneMenu hsom) {
this.yedown = hsom;
}
private DefaultSelectItemsArray dropdown1DefaultItems = new DefaultSelectItemsArray();
public DefaultSelectItemsArray getDropdown1DefaultItems() {
return dropdown1DefaultItems;
}
public void setDropdown1DefaultItems(DefaultSelectItemsArray dsia) {
this.dropdown1DefaultItems = dsia;
}
private UISelectItems dropdown1SelectItems = new UISelectItems();
public UISelectItems getDropdown1SelectItems() {
return dropdown1SelectItems;
}
public void setDropdown1SelectItems(UISelectItems uisi) {
this.dropdown1SelectItems = uisi;
}
private HtmlOutputText ytext = new HtmlOutputText();
public HtmlOutputText getYtext() {
return ytext;
}
public void setYtext(HtmlOutputText hot) {
this.ytext = hot;
}
public FenyeComponent() {
}
/**
* <p>Callback method that is called whenever a page containing
* this page fragment is navigated to, either directly via a URL,
* or indirectly via page navigation. Override this method to acquire
* resources that will be needed for event handlers and lifecycle methods.</p>
*
* <p>The default implementation does nothing.</p>
*/
@Override
public void init() {
super.init();
try {
_init();
} catch (Exception e) {
log("FenyeComponent Initialization Failure", e);
throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
}
}
@Override
public void destroy() {
}
//该方法为首页按牛的action方法
public String firstb_action() {
JSFUtil.getSession().setAttribute("dangqianyehao",new Integer(1));
firstb.setDisabled(true);//在第一页时这两个按牛禁用
previousb.setDisabled(true);
Integer in=(Integer)JSFUtil.getSession().getAttribute("zongyeshu");
if(in.intValue()>1){
lastb.setDisabled(false);//在第一页时总页数又大于1这两个按牛不禁用
nextb.setDisabled(false);
}
return null;
}
//该方法为下一页按牛的action方法
public String previousb_action() {
Integer i=(Integer)JSFUtil.getSession().getAttribute("dangqianyehao");
int dqyh=i.intValue()-1;
JSFUtil.getSession().setAttribute("dangqianyehao",new Integer(dqyh));
if(dqyh==1){
firstb.setDisabled(true);//在第一页时这两个按牛禁用
previousb.setDisabled(true);
}
Integer in=(Integer)JSFUtil.getSession().getAttribute("zongyeshu");
if(dqyh<in.intValue()){
lastb.setDisabled(false);//如果当前页号小于总页数则这两个不禁用
nextb.setDisabled(false);
}
return null;
}
//该方法为下一页按牛的action方法
public String nextb_action() {
Integer i=(Integer)JSFUtil.getSession().getAttribute("dangqianyehao");
int dqyh=i.intValue()+1;
JSFUtil.getSession().setAttribute("dangqianyehao",new Integer(dqyh));
Integer in=(Integer)JSFUtil.getSession().getAttribute("zongyeshu");
if(dqyh==in.intValue()){
lastb.setDisabled(true);//在最后一页时这两个按牛禁用
nextb.setDisabled(true);
}
if(dqyh>1){
firstb.setDisabled(false);//如果当前页号大于1则这两个按牛不禁用
previousb.setDisabled(false);
}
return null;
}
//该方法为末页按牛的action方法
public String lastb_action() {
Integer i=(Integer)JSFUtil.getSession().getAttribute("zongyeshu");
JSFUtil.getSession().setAttribute("dangqianyehao",new Integer(i));
lastb.setDisabled(true);//在最后一页时这两个按牛禁用
nextb.setDisabled(true);
if(i.intValue()>1){
firstb.setDisabled(false);//在末页时总页数>1则这两个按牛不禁用
previousb.setDisabled(false);
}
return null;
}
/*不同的页面在应用此分页组件时会向该组件传递不同的查询字符串(通过session来传递),该方法就是要检测session中的查询字符串有没有发生变化,如果发生变化说明该用户已经进入到别的应用此组件的页面此时应该应用session中的新字符串
进行重新统计符合条件的记录总数和应用新字符串进行重新查询*/
public String getChaxunyuju() {
Object o=JSFUtil.getSession().getAttribute("chaxunyuju");
if(o==null){
return "test";/*当在某页面中插入该分页组件,但还没绑定到该组件时为了在测试该页面时不产生错误所以先随便返回一个字符串*/
}
String cxyj=(String)o;
if(JSFUtil.getSession().getAttribute("chaxunyuju2")==null){
String copycxyj=new String(cxyj);
JSFUtil.getSession().setAttribute("chaxunyuju2",copycxyj);
}
String cxyj2=(String)JSFUtil.getSession().getAttribute("chaxunyuju2");
if(cxyj2.equals(cxyj)){
return cxyj;
}else{
String cxyjcopy=new String(cxyj);
JSFUtil.getSession().setAttribute("chaxunyuju2",cxyjcopy);/*使查询语句2属性应用新值以便于进行下一次比较.*/
JSFUtil.getSession().setAttribute("jiluzongshu",null);/*当查询语句改变时应该设置session的记录总数属性为null这样才能重新统计记录总数重新分页.*/
JSFUtil.getSession().setAttribute("zongyeshu",null);/*当查询语句改变时应该设置session的总页数属性为null这样才能重新计算总页数.*/
JSFUtil.getSession().setAttribute("dangqianyehao",null);//当查询语句改变时应该设置session的当前页号属性为null这样才能从第一页开始查询.*/
return cxyj;
}
}
public void setChaxunyuju(String chaxunyuju) {
this.chaxunyuju = chaxunyuju;
}
public int getMeiyexianshishu() {
Object o=JSFUtil.getSession().getAttribute("meiyexianshishu");
if(o!=null){
Integer i=(Integer)o;
return i.intValue();
}
return meiyexianshishu;
}
public void setMeiyexianshishu(int meiyexianshishu) {
this.meiyexianshishu = meiyexianshishu;
}
//此方法就是获取一页中的数据
public List getDatalist() {
/*其中的HBUtil类为自定义的实用类,他集成了Hibernate的一些类的方法,下面的HBUtil.createQueryWithoutSessionClose方法作用就是创建一个Hibernate的查询,创建后不关闭Hibernate的session。该组件是用Hibernate来分页了,你完全可以把getDatelist方法改为用JDBC实现。*/
Query query=HBUtil.createQueryWithoutSessionClose(chaxunyuju);
if(getCanshuzhimap()!=null&&getCanshuclassarray()!=null){
HashMap hm=getCanshuzhimap();
Class[] c=getCanshuclassarray();
String[] parameternames=query.getNamedParameters();
for(int i=0;i<parameternames.length;i++){
query.setParameter(parameternames[i],c[i].cast(hm.get(parameternames[i])));
}
}
query.setFirstResult(getKaishiweizhi());
query.setMaxResults(getMeiyexianshishu());
datalist=query.list();
HBUtil.closeSession();
return datalist;
}
public void setDatalist(List datalist) {
this.datalist = datalist;
}
//此方法根据当前页号计算开始位置
public int getKaishiweizhi() {
Object o=JSFUtil.getSession().getAttribute("dangqianyehao");
if(o==null){
return 0;
}
Integer i=(Integer)o;
int ii=i.intValue();
return (ii-1)*getMeiyexianshishu();
}
public void setKaishiweizhi(int kaishiweizhi) {
this.kaishiweizhi = kaishiweizhi;
}
//此方法为跳转到下拉列表的值改变事件监听器
public void yedown_processValueChange(ValueChangeEvent vce) {
Integer i=(Integer)yedown.getValue();
int dqyh=i.intValue();
Integer integer=(Integer)JSFUtil.getSession().getAttribute("zongyeshu");
int zys=integer.intValue();
if(dqyh==1){
firstb.setDisabled(true);//在第一页时这两个按牛禁用
previousb.setDisabled(true);
if(zys>1){
lastb.setDisabled(false);
nextb.setDisabled(false);
}
}
if(dqyh==zys){
lastb.setDisabled(true);//在最后一页时这两个按牛禁用
nextb.setDisabled(true);
if(zys>1){
firstb.setDisabled(false);
previousb.setDisabled(false);
}
}
if(dqyh>1&&dqyh<zys){
firstb.setDisabled(false);
previousb.setDisabled(false);
nextb.setDisabled(false);
lastb.setDisabled(false);
}
}
//获取session中的参数值映射
public HashMap getCanshuzhimap() {
Object o=JSFUtil.getSession().getAttribute("canshuzhimap");
if(o!=null){
canshuzhimap=(HashMap)o;
}else{
canshuzhimap=null;//适合无参数的查询语句
}
return canshuzhimap;
}
public void setCanshuzhimap(HashMap canshuzhimap) {
this.canshuzhimap = canshuzhimap;
}
//获取session中的参数值类型数组
public Class[] getCanshuclassarray() {
Object o=JSFUtil.getSession().getAttribute("canshuclassarray");
if(o!=null){
canshuclassarray=(Class[])o;
}else{
canshuclassarray=null;//适合无参数的查询语句
}
return canshuclassarray;
}
public void setCanshuclassarray(Class[] canshuclassarray) {
this.canshuclassarray = canshuclassarray;
}
public int getJiluxuhao() {
int kswz=getKaishiweizhi();
if(jiluxuhao==0){
jiluxuhao=kswz;
}
jiluxuhao=jiluxuhao+1;
return jiluxuhao;
}
public void setJiluxuhao(int jiluxuhao) {
this.jiluxuhao = jiluxuhao;
}
}
上面就是该分页组件的bean,在其中用到了HBUtil类和JSFUtil类,下面把这两个类也列出来吧:
以下是代码:
package jsfzuopin;
import org.hibernate.*;
import java.io.Serializable;
import java.util.*;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
/**
*
* @author xuechangfeng
*/
public class HBUtil
{
/** Creates a new instance of HBUtil */
public HBUtil() {
}
public static SessionFactory sessionfactory;
public static SessionFactory getSessionFactory(){
if(sessionfactory==null){
sessionfactory=(SessionFactory)JSFUtil.getApplication().getAttribute("sf");
}
return sessionfactory;
}
public static ThreadLocal localsession=new ThreadLocal();
public static Session currentSession() throws HibernateException{
Session s=(Session)localsession.get();
if(s==null){
s=getSessionFactory().openSession();
localsession.set(s);
}
return s;
}
public static void closeSession()throws HibernateException{
Session s=(Session)localsession.get();
localsession.set(null);
if(s!=null){
s.close();
}
}
public static void saveOrUpdateWithoutSessionClose(Object duixiang)throws HibernateException
{
Session session=currentSession();
Transaction ts=session.beginTransaction();
ts.begin();
session.saveOrUpdate(duixiang);
ts.commit();
}
public static void saveOrUpdateWithSessionClose(Object duixiang)throws HibernateException
{
Session session=currentSession();
Transaction ts=session.beginTransaction();
ts.begin();
session.saveOrUpdate(duixiang);
ts.commit();
closeSession();
}
public static Object getWithoutSessionClose(String leiming,Serializable id)throws HibernateException
{
Session session=currentSession();
Object object=session.get(leiming,id);
return object;
}
public static Object getWithSessionClose(String leiming,Serializable id)throws HibernateException
{
Session session=currentSession();
Object object=session.get(leiming,id);
closeSession();
return object;
}
public static Query createQueryWithoutSessionClose(String chaxunyuju){
Session session=currentSession();
Query query=session.createQuery(chaxunyuju);
return query;
}
public static List chaxunWithoutSessionClose(String chaxunyuju)throws HibernateException
{
List list;
Session session=currentSession();
Query query=session.createQuery(chaxunyuju);
list=query.list();
return list;
}
public static List chaxunWithSessionClose(String chaxunyuju)throws HibernateException
{
List list;
Session session=currentSession();
Query query=session.createQuery(chaxunyuju);
list=query.list();
closeSession();
return list;
}
public static int gengxinWithoutSessionClose(String gengxinyuju)throws HibernateException
{
int gengxinjilushu;
Session session=currentSession();
Query query=session.createQuery(gengxinyuju);
Transaction ts=session.beginTransaction();
ts.begin();
gengxinjilushu=query.executeUpdate();
ts.commit();
return gengxinjilushu;
}
public static int gengxinWithSessionClose(String gengxinyuju)throws HibernateException
{
int gengxinjilushu;
Session session=currentSession();
Query query=session.createQuery(gengxinyuju);
Transaction ts=session.beginTransaction();
ts.begin();
gengxinjilushu=query.executeUpdate();
ts.commit();
closeSession();
return gengxinjilushu;
}
public static void updateWithoutSessionClose(Object duixiang)throws HibernateException
{
Session session=currentSession();
Transaction ts=session.beginTransaction();
ts.begin();
session.update(duixiang);
ts.commit();
}
public static void updateWithSessionClose(Object duixiang)throws HibernateException
{
Session session=currentSession();
Transaction ts=session.beginTransaction();
ts.begin();
session.update(duixiang);
ts.commit();
closeSession();
}
public static void saveWithoutSessionClose(Object duixiang)throws HibernateException
{
Session session=currentSession();
Transaction ts=session.beginTransaction();
ts.begin();
session.save(duixiang);
ts.commit();
}
public static void saveWithSessionClose(Object duixiang)throws HibernateException
{
Session session=currentSession();
Transaction ts=session.beginTransaction();
ts.begin();
session.save(duixiang);
ts.commit();
closeSession();
}
}
package jsfzuopin;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
*
* @author xuechangfeng
*/
public class JSFUtil {
public static HttpSession getSession(){
HttpSession hs=(HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
return hs;
}
public static ServletContext getApplication(){
ServletContext sc=(ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
return sc;
}
public static HttpServletRequest getRequest(){
HttpServletRequest hsr=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
return hsr;
}
public static void log(){
FacesContext.getCurrentInstance().getExternalContext().log("########################");
}
public static void log(Exception e){
FacesContext.getCurrentInstance().getExternalContext().log("########################"+e.getMessage());
FacesContext.getCurrentInstance().getExternalContext().log("########################"+e.getCause());
}
public static void log(String s){
FacesContext.getCurrentInstance().getExternalContext().log("########################"+s);
}
public static FacesMessage createMessage(String message){
return new FacesMessage(FacesMessage.SEVERITY_ERROR,message,message);
}
public static void addSystemMessage(String message){
FacesContext fc=FacesContext.getCurrentInstance();
fc.addMessage(null,createMessage(message));
}
public static void addComponentMessage(FacesContext fc,UIComponent c,String message){
if(fc!=null){
fc.addMessage(c.getClientId(fc),createMessage(message));
}else{
fc=FacesContext.getCurrentInstance();
fc.addMessage(c.getClientId(fc),createMessage(message));
}
}
}
好了,该组件的所有内容都全了,还有不明白的地方小弟愿意为你效劳,我的油箱为:
xuecf8679@yahoo.com.cn