我正在使用:JSF 2.0,GlassFish Server 3.0。
支持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.out.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!");
}
}
Until
类:
public static String getRequestParameter(String key) {
return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
}
观点:
<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>
我希望Model只在第一次加载#{Users.usersList}。现在,当我的应用程序运行并且有人点击更改页面或其他内容时,它打印如下:
// 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!
如何在不加载包含10条记录的初始页面1的情况下加载数据?
答案 0 :(得分:2)
每当您在同一视图中提交表单时,视图范围的bean构造函数都应该不。所有初始属性都应保留在视图范围的bean中。您的视图范围bean将在每个请求上重新创建,因为您正在绑定<c:forEach>
属性。 <c:forEach>
是标记处理程序,而不是像<h:dataTable>
这样的真正JSF组件。
相应地替换它:
<h:dataTable value="#{Users.usersList}" var="user">
<h:column>#{user.UId}</h:column>
<h:column>#{user.UName}</h:column>
</h:dataTable>