开发者

How to prevent Backing Bean loading data on Ajax action?

I am using: JSF 2.0, GlassFish Server 3.0.

The backing bean:

@ManagedBean(name = "Users")
@ViewScoped
public class ModelUsers {

    /** Creates a new instance of ModelUsers */
    public ModelUsers() {
        System.out.println("-----------------------------");
        System.out.println("Invoked Contructor !");
        System.o开发者_如何转开发ut.println("Page = " + page);
        System.out.println("Item per page = " + itemPerPage);
    }

    // ... Getter/setter...    

    public void actionReload(ActionEvent action) {
        System.out.println("Clicked !");
        loadData();
    }

    public void actionChangePage(ActionEvent action) {
        System.out.println("Clicked !");
        boolean isLoad = false;
        int p = 0;
        try {
            p = Integer.parseInt(Until.getActionAttribute(action, "page").toString());
        } catch (Exception ex) {
        }
        int i = 0;
        try {
            i = Integer.parseInt(Until.getActionAttribute(action, "itemPerPage").toString());
        } catch (Exception ex) {
        }
        if (p != 0 && p != page) {
            isLoad = true;
            page = p;
        }
        if (i != 0 && i != itemPerPage) {
            isLoad = true;
            itemPerPage = i;
        }
        if (isLoad) {
            System.out.println("Load by change page or number of items");
            loadData();
        }
    }
    /** Parameter here */
    private int page = 1;
    private int totalCount = 1;
    private int itemPerPage = 10;
    private boolean isLoaded = false;
    private List<User> listUsers = null;

    private void loadData() {
        isLoaded = true;
        System.out.println("Loading Data with : page = " + page + " and " + itemPerPage + " record(s)");
// Load Data from DataBase ...//

        System.out.println("Load done!");
    }
}

The Until class:

public static String getRequestParameter(String key) {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
    }

The view:

<h:form id="pannel">         
    <h:panelGroup id ="controlPannel">
            Page:  <h:outputLabel id="page" value="${Users.page}"/><br/>                                
                Item per page: <h:inputText id ="item" value="#{Users.itemPerPage}" />
                Page: <h:inputText value="#{Users.page}" />
                <h:commandButton actionListener="#{Users.actionReload}" value="Go">
                    <f:ajax  render="pannel" execute="page item"/>
        </h:commandButton> <br/>
        <h:commandButton actionListener="#{Users.actionChangePage}" value="Previous">
            <f:ajax render="pannel"/>
                        <f:attribute name="itemPerPage" value="${Users.itemPerPage}"/>
                        <f:attribute name="page" value="${Users.page-1}"/>
        </h:commandButton>
                <h:commandButton actionListener="#{Users.actionChangePage}" value="Next" >
            <f:ajax render="pannel"/>
                        <f:attribute name="page" value="${Users.page+1}"/>
                        <f:attribute name="itemPerPage" value="${Users.itemPerPage}"/>
        </h:commandButton>
    </h:panelGroup>
    <table>
        <c:forEach items="${Users.usersList}" var="item">
            <tr>
                    <td>${item.UId}</td>
                        <td>${item.UName}</td>
                </tr>
    </c:forEach>
    </table>
</h:form>

I want the Model should load the #{Users.usersList} the first time only. Now, when my app runs and with someone click to change pages or something else, it prints as follows:

// After change the value in text box to 1 and 112, i got this:
INFO: -----------------------------
INFO: Invoked Contructor !
**INFO: Page = 1
INFO: Item per page = 10
INFO: Load data by get list user...
INFO: Loading Data with : page = 1 and 10 record(s)**
INFO: Load done!
INFO: Clicked !
INFO: Page = 1
INFO: Item per page = 112
INFO: Loading Data with : page = 1 and 112 record(s)
INFO: Load done!

How can I load the data without loading the initial page 1 with 10 records?


The view scoped bean constructor should not be invoked whenever you submit a form in the same view. The all initial properties should be retained in a view scoped bean. Your view scoped bean is recreated on every request because you're binding a <c:forEach> attribute to it. The <c:forEach> is a tag handler, not a real JSF component like <h:dataTable>.

Replace it accordingly:

<h:dataTable value="#{Users.usersList}" var="user">
    <h:column>#{user.UId}</h:column>
    <h:column>#{user.UName}</h:column>
</h:dataTable>

See also:

  • @ViewScoped fails in tag handlers
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜