如何防止 Backing Bean 在 Ajax 操作上加载数据?
我正在使用: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>
我希望模型仅在第一次加载#{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 页的情况下加载数据?
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.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!");
}
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当您在同一视图中提交表单时,不应调用视图作用域 bean 构造函数。所有初始属性都应保留在视图作用域 bean 中。您的视图作用域 bean 会在每次请求时重新创建,因为您将
属性绑定到它。
是一个标记处理程序,而不是像
那样的真正的 JSF 组件。相应地替换它:
另请参阅:
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:
See also: