Struts2中的DisplayTag用于分页

时间:2012-04-11 12:12:05

标签: struts2 displaytag

我想将分页应用到我的struts2 Web应用程序中。当用户登录时,我将它们重定向到主页,在该主页上我想使用display标记显示分页中的所有用户。

我已完成研究并最终将其集成到我的struts2中,但是当我在登录后运行代码时,它会显示消息Nothing found to display

当我在struts1.3中完成相同的操作时,从this site开始,它正在运行。 我已将以下JAR文件复制到我的lib文件夹:

commons-logging.jar
commons-lang.jar
commons-collections.jar
commons-beanutils.jar
displaytag-1.2.jar

我还将displaytag.tldstruts-2.17.dtd复制到我的web-inf文件夹。

以下是我的代码:

我的个人资料.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@taglib uri="http://displaytag.sf.net" prefix="display" %>
<%@taglib uri="/struts-tags"  prefix="s" %>
<html>
   <head>
   </head>
<body>
    <div id="wrapper">
        <div id="steps">
            <fieldset class="step">
                <legend>Profile
                    </legend>
                <display:table id="data" name="list" requestURI="/display.action" pagesize="1" >
                    <display:column sortable="true">
                        <p>
                            <label for="username">User Name</label>
                            <input id="username" name="username" value="<s:property value="firstName" />" disabled="disabled"/>
                        </p>
                        <p>
                            <label for="email">Father Name</label>
                            <input id="email" name="email" value="<s:property value="lastName"/>" disabled="disabled" />
                        </p>
                        <p>
                            <label for="password">Age</label>
                            <input  name="password" value="<s:property value="dob"/>" disabled="disabled"/>
                        </p>
                        <p>
                            <label for="address">Address</label>
                            <input name="address"  value="<s:property value="emailID"/>" disabled="disabled"/>
                        </p>
                </fieldset>
                    </div>
                </display:column>
            </display:table>
        </div>
    </div>
</body>
</html>

struts.xml中

<action name="display" class="com.java.action.SearchAction">
        <result name="success">/profile.jsp</result>
        <result name="errror">/error.jsp</result>
</action>

SearchAction.java

private ArrayList<UserBean> list=new ArrayList<UserBean>();
//setter getter 
public String execute()
{
    UserBean rt=new UserBean();
    SearchDB user=new SearchDB();
    this.setList(user.search(gender,age_min,age_max,religion,caste,photo_display));
    return SUCCESS;
}

UserBean.java

public class UserBean {

private String emailID;
private String userName;
private String gender;
private String dob;
private String firstName;
private String lastName;
private int Id;
//setter and getter
}

SearchDB.java

//code to get records. their is no problem here because it is taking records out from db fine.

我不确定,但我的猜测是requestURIdisplaytag中的名称属性,因为在上面链接的示例中,他们使用的是name="sessionScope.UserForm.userList"。有人请告诉我哪里做错了吗?

2 个答案:

答案 0 :(得分:0)

你可能已经解决了,但无论如何......试试OGNL:

<input id="username" name="username" value="%{data.firstName}" disabled="disabled"/>

它直接使用您的用户属性的getter。顺便说一句,我不确定你的禁用标签。你可能应该使用readonly。

答案 1 :(得分:0)

您已设置显示标记的记录总数,如

<display:table id="data" name="lstEntities"
                        sort="external" uid="row" htmlId="rowid" class="tborder"
                        style="width:100%"  excludedParams="*"
                        pagesize="${pageCriteria.recordsPerPage}" partialList="true"
                        size="${pageCriteria.totalRecords}" export="false"
                        requestURI="XXX.action">





   public class PaginationCriteria implements Cloneable, CommonConstants,
            Serializable {

        /**
         * Holds the Unie value of Class.
         */
        private static final long serialVersionUID = 8047568459658871831L;

        /**
         * Stores cache Mode.
         */
        private boolean cached;

        /**
         * Stores current page number in the user screen.
         */
        private int currentPage;

        /**
         * Holds the Name of the attribute in Entity to be unique.
         */
        private String distinctRootEntityName;

        /**
         * Stores the information about no of entities to be fetched.
         */
        private boolean fetchAll;

        /**
         * Stores the information about no. of records to be fetched.
         */
        private int recordsPerPage;

        /**
         * Stores the secondary sort column of the entity.
         */
        private String secondarySortBy;

        /**
         * Stores the Sort column of the entity.
         */
        private String sortBy;

        /**
         * Stores the sort order of the entity.
         */
        private boolean sortDescending;

        /**
         * Stores total no. of records.
         */
        private int totalRecords;

//Getters and setters of this properties   

}

从动作类设置每页的记录和页面的第一条记录以及所有内容。在查询执行中设置执行总数。在Action类中拥有此域对象。

在动作中,使用以下方法设置分页信息,

/**
     * Fills the Sort column, order, page number to be retrieved.
     * 
     * @param tableId -
     *        Display tag table Id to retrieve the Sort column, order, page
     *        number
     * @param defaultOrderCoulmn -
     *        If no parameter passed for sorting default order column will be
     *        applied.
     */
    protected void fillPaginationInfo(final String tableId,
            final String defaultOrderCoulmn, final String secondarySortColumn) {
        BaseAction.LOGGER.debug(BaseAction.LOG_PREFIX
                + "calling fillPaginationInfo param: tableId :" + tableId
                + "\ndefaultOrderCoulmn:" + defaultOrderCoulmn);
        this.getPageCriteria().setCurrentPage(
                this.getPageNumber(this.getHttpRequest(), tableId));
        String orderBy = this.getSortColumn(this.getHttpRequest(), tableId);
        this.getPageCriteria().setSortBy(
                orderBy == null || orderBy.equals("null") ? defaultOrderCoulmn
                        : orderBy);
        this.getPageCriteria().setSortDescending(
                this.getSortOrderDesc(this.getHttpRequest(), tableId));
        if (secondarySortColumn != null)
            this.getPageCriteria().setSecondarySortBy(secondarySortColumn);
        BaseAction.LOGGER.debug(BaseAction.LOG_PREFIX
                + "done fillPaginationInfo");
    }

希望它会有所帮助。如果您需要任何其他信息,请告诉我。