---
title: "jsf实现翻页"
author: "fourfire"
date: "2007-09-17T16:25:37Z"
tags: []
slug: "jsf"
draft: false
lastmod: "2007-09-17T16:25:37Z"
id: 124768
url: "https://fourfire.iteye.com/blog/124768"
---

用jsf实现了翻页功能，主要原理是扩展datamodel和datatabletag  
1 扩展datatabletag，增加了几个属性，用于后台执行查询语句

java 代码

 

001. package com.longtop.core.web.jsf.tag;
002. import javax.faces.application.Application;
003. import javax.faces.component.UIComponent;
004. import javax.faces.context.FacesContext;
005. import javax.faces.el.ValueBinding;
006. import com.sun.faces.taglib.html\_basic.DataTableTag;
007. public class DataTableWithScrollTag extends DataTableTag {
008. private boolean scroll = true;
009. private int pageSize = 10;
010. private String scrollPageCss = "";
011. private String moduleId;
012. private String statementId;
013. private String beanName;
014. private String implType;
015. private String searchCondition;
016. /\**
017. * @return the pageSize
018. \*/
019. public int getPageSize() {
020. return pageSize;
021. }
022. /\**
023. * @param pageSize
024. *            the pageSize to set
025. \*/
026. public void setPageSize(int pageSize) {
027. this.pageSize = pageSize;
028. }
029. /\**
030. * @return the scroll
031. \*/
032. public boolean isScroll() {
033. return scroll;
034. }
035. /\**
036. * @param scroll
037. *            the scroll to set
038. \*/
039. public void setScroll(boolean scroll) {
040. this.scroll = scroll;
041. }
042. public String getComponentType() {
043. return "com.longtop.core.web.jsf.render.DataTableWithScrollType";
044. }
045. public void setProperties(UIComponent component) {
046. super.setProperties(component);
047. setStringProperty(component, "pageSize", String.valueOf(pageSize));
048. setStringProperty(component, "scroll", String.valueOf(scroll));
049. setStringProperty(component, "scrollPageCss", scrollPageCss);
050. setStringProperty(component, "moduleId", moduleId);
051. setStringProperty(component, "statementId", statementId);
052. setStringProperty(component, "beanName", beanName);
053. setStringProperty(component, "implType", implType);
054. setStringProperty(component, "searchCondition", searchCondition);
055. }
056. private void setStringProperty(UIComponent component, String attrName,
057. String attrValue) {
058. if (attrValue == null)
059. return;
060. if (isValueReference(attrValue)) {
061. FacesContext context = FacesContext.getCurrentInstance();
062. Application application = context.getApplication();
063. ValueBinding binding = application.createValueBinding(attrValue);
064. component.setValueBinding(attrName, binding);
065. } else {
066. component.getAttributes().put(attrName, attrValue);
067. }
068. }
069. /\**
070. * @return the scrollPageCss
071. \*/
072. public String getScrollPageCss() {
073. return scrollPageCss;
074. }
075. /\**
076. * @param scrollPageCss the scrollPageCss to set
077. \*/
078. public void setScrollPageCss(String scrollPageCss) {
079. this.scrollPageCss = scrollPageCss;
080. }
081. /\**
082. * @return the moduleId
083. \*/
084. public String getModuleId() {
085. return moduleId;
086. }
087. /\**
088. * @param moduleId the moduleId to set
089. \*/
090. public void setModuleId(String moduleId) {
091. this.moduleId = moduleId;
092. }
093. /\**
094. * @return the statementId
095. \*/
096. public String getStatementId() {
097. return statementId;
098. }
099. /\**
100. * @param statementId the statementId to set
101. \*/
102. public void setStatementId(String statementId) {
103. this.statementId = statementId;
104. }
105. /\**
106. * @return the beanName
107. \*/
108. public String getBeanName() {
109. return beanName;
110. }
111. /\**
112. * @param beanName the beanName to set
113. \*/
114. public void setBeanName(String beanName) {
115. this.beanName = beanName;
116. }
117. /\**
118. * @return the implType
119. \*/
120. public String getImplType() {
121. return implType;
122. }
123. /\**
124. * @param implType the implType to set
125. \*/
126. public void setImplType(String implType) {
127. this.implType = implType;
128. }
129. /\**
130. * @return the searchCondition
131. \*/
132. public String getSearchCondition() {
133. return searchCondition;
134. }
135. /\**
136. * @param searchCondition the searchCondition to set
137. \*/
138. public void setSearchCondition(String searchCondition) {
139. this.searchCondition = searchCondition;
140. }
141. }

2 dragon.tld

xml 代码

 

001. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
002. &lt;!DOCTYPE taglib
003. PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
004. "http://java.sun.com/dtd/web-jsptaglibrary\_1\_2.dtd"&gt;
005. &lt;taglib&gt;
006. &lt;tlib-version&gt;1.0&lt;/tlib-version&gt;
007. &lt;jsp-version&gt;2.0&lt;/jsp-version&gt;
008. &lt;short-name&gt;d&lt;/short-name&gt;
009. &lt;uri&gt;http://com.longtop.net/dataTableWithScroll&lt;/uri&gt;
010. &lt;tag&gt;
011. &lt;name&gt;scrollDataTable&lt;/name&gt;
012. &lt;tag-class&gt;com.longtop.core.web.jsf.tag.DataTableWithScrollTag&lt;/tag-class&gt;
013. &lt;tei-class&gt;com.sun.faces.taglib.FacesTagExtraInfo&lt;/tei-class&gt;
014. &lt;body-content&gt;JSP&lt;/body-content&gt;
015. &lt;description&gt;
016. &lt;/description&gt;
017. &lt;attribute&gt;
018. &lt;name&gt;first&lt;/name&gt;
019. &lt;required&gt;false&lt;/required&gt;
020. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
021. &lt;description&gt;
022. Zero-relative row number of the first row to be displayed.  If this
023. property is set to zero, rendering will begin with the first row of
024. the underlying data.
025. &lt;/description&gt;
026. &lt;/attribute&gt;
027. &lt;attribute&gt;
028. &lt;name&gt;id&lt;/name&gt;
029. &lt;required&gt;false&lt;/required&gt;
030. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
031. &lt;description&gt;
032. The component identifier for this component.  This value must be
033. unique within the closest parent component that is a naming
034. container.
035. &lt;/description&gt;
036. &lt;/attribute&gt;
037. &lt;attribute&gt;
038. &lt;name&gt;rendered&lt;/name&gt;
039. &lt;required&gt;false&lt;/required&gt;
040. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
041. &lt;description&gt;
042. Flag indicating whether or not this component should be rendered
043. (during Render Response Phase), or processed on any subsequent
044. form submit.
045. &lt;/description&gt;
046. &lt;/attribute&gt;
047. &lt;attribute&gt;
048. &lt;name&gt;rows&lt;/name&gt;
049. &lt;required&gt;false&lt;/required&gt;
050. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
051. &lt;description&gt;
052. The number of rows to display, starting with the one identified by the
053. "first" property.  If this value is set to zero, all available rows in
054. the underlying data model will be displayed.
055. &lt;/description&gt;
056. &lt;/attribute&gt;
057. &lt;attribute&gt;
058. &lt;name&gt;value&lt;/name&gt;
059. &lt;required&gt;false&lt;/required&gt;
060. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
061. &lt;description&gt;
062. The current value of this component.
063. &lt;/description&gt;
064. &lt;/attribute&gt;
065. &lt;attribute&gt;
066. &lt;name&gt;var&lt;/name&gt;
067. &lt;required&gt;false&lt;/required&gt;
068. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
069. &lt;description&gt;
070. Name of a request-scope attribute under which the model data for the
071. row selected by the current value of the "rowIndex" property (i.e.
072. also the current value of the "rowData" property) will be exposed.
073. &lt;/description&gt;
074. &lt;/attribute&gt;
075. &lt;attribute&gt;
076. &lt;name&gt;bgcolor&lt;/name&gt;
077. &lt;required&gt;false&lt;/required&gt;
078. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
079. &lt;description&gt;
080. Name or code of the background color for this table.
081. &lt;/description&gt;
082. &lt;/attribute&gt;
083. &lt;attribute&gt;
084. &lt;name&gt;border&lt;/name&gt;
085. &lt;required&gt;false&lt;/required&gt;
086. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
087. &lt;description&gt;
088. Width (in pixels) of the border to be drawn
089. around this table.
090. &lt;/description&gt;
091. &lt;/attribute&gt;
092. &lt;attribute&gt;
093. &lt;name&gt;cellpadding&lt;/name&gt;
094. &lt;required&gt;false&lt;/required&gt;
095. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
096. &lt;description&gt;
097. Definition of how much space the user agent should
098. leave between the border of each cell and its contents.
099. &lt;/description&gt;
100. &lt;/attribute&gt;
101. &lt;attribute&gt;
102. &lt;name&gt;cellspacing&lt;/name&gt;
103. &lt;required&gt;false&lt;/required&gt;
104. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
105. &lt;description&gt;
106. Definition of how much space the user agent should
107. leave between the left side of the table and the
108. leftmost column, the top of the table and the top of
109. the top side of the topmost row, and so on for the
110. right and bottom of the table.  It also specifies
111. the amount of space to leave between cells.
112. &lt;/description&gt;
113. &lt;/attribute&gt;
114. &lt;attribute&gt;
115. &lt;name&gt;columnClasses&lt;/name&gt;
116. &lt;required&gt;false&lt;/required&gt;
117. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
118. &lt;description&gt;
119. Comma-delimited list of CSS style classes that will be applied
120. to the columns of this table.  A space separated list of
121. classes may also be specified for any individual column.  If
122. the number of elements in this list is less than the number of
123. columns specified in the "columns" attribute, no "class"
124. attribute is output for each column greater than the number of
125. elements in the list.  If the number of elements in the list
126. is greater than the number of columns specified in the
127. "columns" attribute, the elements at the posisiton in the list
128. after the value of the "columns" attribute are ignored.
129. &lt;/description&gt;
130. &lt;/attribute&gt;
131. &lt;attribute&gt;
132. &lt;name&gt;dir&lt;/name&gt;
133. &lt;required&gt;false&lt;/required&gt;
134. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
135. &lt;description&gt;
136. Direction indication for text that does not inherit directionality.
137. Valid values are "LTR" (left-to-right) and "RTL" (right-to-left).
138. &lt;/description&gt;
139. &lt;/attribute&gt;
140. &lt;attribute&gt;
141. &lt;name&gt;footerClass&lt;/name&gt;
142. &lt;required&gt;false&lt;/required&gt;
143. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
144. &lt;description&gt;
145. Space-separated list of CSS style class(es) that will be
146. applied to any footer generated for this table.
147. &lt;/description&gt;
148. &lt;/attribute&gt;
149. &lt;attribute&gt;
150. &lt;name&gt;frame&lt;/name&gt;
151. &lt;required&gt;false&lt;/required&gt;
152. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
153. &lt;description&gt;
154. Code specifying which sides of the frame surrounding
155. this table will be visible.  Valid values are:
156. none (no sides, default value); above (top side only);
157. below (bottom side only); hsides (top and bottom sides
158. only); vsides (right and left sides only); lhs (left
159. hand side only); rhs (right hand side only); box
160. (all four sides); and border (all four sides).
161. &lt;/description&gt;
162. &lt;/attribute&gt;
163. &lt;attribute&gt;
164. &lt;name&gt;headerClass&lt;/name&gt;
165. &lt;required&gt;false&lt;/required&gt;
166. &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
167. &lt;description&gt;
168. Space-separated list of CSS style class(es) that will be
169. applied to any header generated for this table.
170. &lt;/description&gt;
171. &lt;/attribute&gt;
172. &lt;attribute&gt;
173. &lt;name&gt;lang&lt;/name&gt;
174. &lt;required&gt;false&lt;/required&gt;
175. &lt;rtexprvalue&gt;false&lt;/