1、获取某列(如: id)的值
struts 2:
<display:table name="usersInfo" id="tabrow" pagesize="${pageSize}" requestURI="web/sys/userManager/UserQuery.action" class="scroll_its" style="width:680px;">
<display:caption>
<thead>
<tr>
<th style="text-align:center;width:7%;"><input type="checkbox" name="box" οnclick="selcheck()" /></th>
<th style="text-align:center;width:20%;"><s:text name="web.usermanager.userid"/></th>
<th style="text-align:center;width:20%;"><s:text name="web.usermanager.username"/></th>
<th style="text-align:center;width:20%;"><s:text name="web.login.password"/></th>
<th style="text-align:center;width:33%;"><s:text name="web.management.remark"/></th></tr>
</thead>
</display:caption>
<display:column title='' style="text-align:center;width:7%;">
<s:iterator value="seluser" status="status">
<s:if test="seluser[#status.index]==#attr.tabrow.pcUsrId">
<s:set name="Selbool" value="1" />
</s:if>
</s:iterator>
<s:if test="#Selbool==1">
<s:checkbox name="seluser" id="seluser" theme="simple" value="true" fieldValue="%{#attr.tabrow.pcUsrId}" οnclick="changeButtonStatus();"></s:checkbox>
</s:if>
<s:else>
<s:checkbox name="seluser" id="seluser" theme="simple" value="false" fieldValue="%{#attr.tabrow.pcUsrId}" οnclick="changeButtonStatus();"></s:checkbox>
</s:else>
<s:set name="Selbool" value="0" />
</display:column>
<display:column property="pcUsrId" titleKey="web.usermanager.userid" style="text-align:left;width:20%;" escapeXml="true" />
<display:column property="pcUsrNm" titleKey="web.usermanager.username" style="text-align:left;width:20%;" escapeXml="true" />
<display:column property="pcUsrPw" titleKey="web.login.password" style="whitespace: nowrap;width:20%;" escapeXml="true" maxLength="200" />
<display:column property="note" titleKey="web.management.remark" style="text-align:center;width:33%;" />
</display:table>
关键代码:
<s:if test="seluser[#status.index]==#attr.tabrow.pcUsrId">
fieldValue="%{#attr.tabrow.pcUsrId}"
JSTL:
<display:table id="row" name="mylist">
<display:column title="row number" >
<c:out value="${row_rowNum}"/>
</display:column>
<display:column title="name" >
<c:out value="${row.first_name}"/>
<c:out value="${row.last_name}"/>
</display:column>
</display:table>
2、通过<display:table id="xxxx"> 的id 属性构造和获取其传递当前页的参数名称(适合 displaytag 自动分页方式)
/**
* 通过表格ID名称取得PAGE的字符串
* @param id
* @return
*/
public static String getPageParamName(String id){
return new org.displaytag.util.ParamEncoder(id).
encodeParameterName(org.displaytag.tags.TableTagParameters.PARAMETER_PAGE);
}
3、自定义实现分页,传递页面的参数名默认为( page )
第一步:实现 PaginatedList 接口的类
package jp.co.snjp.kddi.web.util;
import java.util.List;
import org.displaytag.pagination.PaginatedList;
import org.displaytag.properties.SortOrderEnum;
public class PageList implements PaginatedList {
private List list; /** 每页的列表 **/
private int pageNumber = 1 ; /** 当前页码 **/
private int objectsPerPage = 15 ; /** 每页记录数 page size **/
private int fullListSize = 0 ; /** 总记录数 **/
private String sortCriterion;
private SortOrderEnum sortDirection;
private String searchId;
public void setFullListSize(int fullListSize) {
this.fullListSize = fullListSize;
}
public void setList(List list) {
this.list = list;
}
public void setObjectsPerPage(int objectsPerPage) {
this.objectsPerPage = objectsPerPage;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public void setSearchId(String searchId) {
this.searchId = searchId;
}
public void setSortCriterion(String sortCriterion) {
this.sortCriterion = sortCriterion;
}
public void setSortDirection(SortOrderEnum sortDirection) {
this.sortDirection = sortDirection;
}
public int getFullListSize() {
return fullListSize;
}
public List getList() {
return list;
}
public int getObjectsPerPage() {
return objectsPerPage;
}
public int getPageNumber() {
return pageNumber;
}
public String getSearchId() {
return searchId;
}
public String getSortCriterion() {
return sortCriterion;
}
public SortOrderEnum getSortDirection() {
return sortDirection;
}
}
第二步:提供一个实现分页的Action 的基类
package jp.co.snjp.kddi.web.util;
import jp.co.snjp.kddi.web.login.form.LoginUserForm;
/**
* 所有需要分页操作的action 的基类
* @author GongQiang
*
*/
public class PageListAction extends BaseAction {
private static final long serialVersionUID = 1L;
/** 分页显示用户信息的 pagelist **/
protected PageList pageList;
/** 总页数 **/
protected int totalPage;
/** 每页显示记录条数 **/
protected int pageSize;
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public PageList getPageList() {
return pageList;
}
public void setPageList(PageList pageList) {
this.pageList = pageList;
}
public int getPageSize() {
return 5;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
@Override
public String executeSCH() throws Exception {
// TODO Auto-generated method stub
return null;
}
/**
* 执行查询操作的当前页
* @return
*
* Date :2011-11-28
* Author :GongQiang
*/
public int getPage(){
String pageStr = request.getParameter( "page" );
int page;
try {
page = Integer.parseInt( pageStr );
} catch (NumberFormatException e) {
page = 1;
}
return page;
}
/**
* 执行删除、修改操作的当前页
* @return
*
* Date :2011-11-28
* Author :GongQiang
*/
public int getPageHidden(){
String pageStr = request.getParameter( "page_hidden" );
int page;
try {
page = Integer.parseInt( pageStr );
} catch (NumberFormatException e) {
page = 1;
}
return page;
}
/**
* 在查询页面提供一个 page_hidden 参数
* 提供给 删除、修改操作获取
* Date :2011-11-29
* Author :GongQiang
*/
public void savePageHiddenInRequest(){
request.setAttribute( "page_hidden", getPage() );
}
/**
* 当前用户的 companyId
* @return
*
* Date :2011-11-28
* Author :GongQiang
*/
public String getCompanyId(){
LoginUserForm loginUserForm = ( LoginUserForm )session.getAttribute( "userList" );
return loginUserForm.getCompanyid();
}
}
第三步:实际分页的 Action,完成PageListAction 中 pageList 和 totalPage 的初始化。(具体示例 ...略)
第四步:页面显示
<display:table name="pageList" id="tabrow" partialList="true" size="${totalPage}"
requestURI="/bpp/web/sys/userManager/UserQuery.action" class="scroll_its" style="width:680px;"
excludedParams="page_hidden search __checkbox_seluser seluser box">
<display:caption>
<thead>
<tr>
<th style="text-align:center;width:7%;"><input type="checkbox" name="box" οnclick="selcheck()" /></th>
<th style="text-align:center;width:20%;"><s:text name="web.usermanager.userid"/></th>
<th style="text-align:center;width:20%;"><s:text name="web.usermanager.username"/></th>
<th style="text-align:center;width:20%;"><s:text name="web.login.password"/></th>
<th style="text-align:center;width:33%;"><s:text name="web.management.remark"/></th></tr>
</thead>
</display:caption>
<display:column title='' style="text-align:center;width:7%;">
<s:iterator value="seluser" status="status">
<s:if test="seluser[#status.index]==#attr.tabrow.pcUsrId">
<s:set name="Selbool" value="1" />
</s:if>
</s:iterator>
<s:if test="#Selbool==1">
<s:checkbox name="seluser" id="seluser" theme="simple" value="true" fieldValue="%{#attr.tabrow.pcUsrId}" οnclick="changeButtonStatus();"></s:checkbox>
</s:if>
<s:else>
<s:checkbox name="seluser" id="seluser" theme="simple" value="false" fieldValue="%{#attr.tabrow.pcUsrId}" οnclick="changeButtonStatus();"></s:checkbox>
</s:else>
<s:set name="Selbool" value="0" />
</display:column>
<display:column property="pcUsrId" titleKey="web.usermanager.userid" style="text-align:left;width:20%;" escapeXml="true" />
<display:column property="pcUsrNm" titleKey="web.usermanager.username" style="text-align:left;width:20%;" escapeXml="true" />
<display:column property="pcUsrPw" titleKey="web.login.password" style="whitespace: nowrap;width:20%;" escapeXml="true" maxLength="200" />
<display:column property="note" titleKey="web.management.remark" maxLength="60" style="text-align:left;width:33%;white-space:nowrap;" escapeXml="true"/>
</display:table>
最主要代码:
<display:table name="pageList" id="tabrow" partialList="true" size="${totalPage}"
4、部分属性说明
partialList
能否显示集合的一部分,有效的值是true或false
size
当partialList为true时才能使用,是写入一个整型对象,它是包含总数据集大小的,要用方法给整数赋予属性
maxLength
表格里的值截断,与显示空白.
<display:column property="longDescription" maxLength="10" style="whitespace: nowrap;"/>
sort="external"
告诉DisplayTag传入的数据集已经由外部程序排好序了..
defaultsort="1"
说明默认是升序(Descending is 2, Ascending is 1);
partialList="true"
说明部分装入数据;
pagesize="20"
每页显示记录数;
size="resultSize"
显示记录的总条数(此参数结合PageSize,使得表格在只拿到某一页的完整数据的同时,可以知道会有多少页,并将其他的页数也列举出来,当用户实际翻页时才去获取当页数据).