這是一個來自mkyong.com的示例,我稍加修改。我添加了一個輸入文本和命令 來發送下一個在mysql中進行分頁的偏移量。如預期的那樣,第一頁自動加載偏移量爲0。如果我在輸入文本中鍵入5,它實際上是有效的,下一頁顯示下面的5條記錄。但是,如果我現在輸入10,它將停止工作,並且頁面會一直嘗試加載。我等了5分鐘,什麼也沒有。另一個重試,0第一次加載輸入15只是爲了測試,它的工作。但是在第一次使用commandButton之後試圖使它工作,它只是停滯不前。jsf會話或配置問題
在url中顯示當前的sessionid。這是會話問題還是配置問題?
我是jsf的新手,並且正在努力學習如何通過正確發送請求來使頁面顯示某些內容。我從oracles站點上的javaeetutorial6,guessnumber/duke示例直接獲得了inputText和commandButton的想法。
package model;
//import java.util.Date;
public class Customer{
public String petid;
public String petname;
public String tmpvar;
private String offset;
//public String address;
//public Date created_date;
//private String searchText;
//public String getSearchText() { return searchText; }
//public void setSearchText(String s) { searchText = s; }
public String getpetid() {
return petid;
}
public void setpetid(String petid) {
this.petid = petid;
}
public String getpetname() {
return petname;
}
public void setpetname(String petname) {
this.petname = petname;
}
/*public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getCreated_date() {
return created_date;
}
public void setCreated_date(Date created_date) {
this.created_date = created_date;
}*/
public String gettmpvar() {
return tmpvar;
}
public void settmpvar(String tmpvar) {
this.tmpvar = tmpvar;
}
}
package cust;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import model.Customer;
@ManagedBean(name="Customer")
//@SessionScoped
public class CustomerBean implements Serializable{
int userNumber;
String offset;
//String SearchText;
//resource injection
@Resource(name="jdbc/petback2")
private DataSource ds;
//if resource injection is not support, you still can get it manually.
/*public CustomerBean(){
try {
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup("jdbc/petback2");
} catch (NamingException e) {
}
}*/
//connect to DB and get customer list
public String searchText;
//private String offset;
public String getSearchText() { return searchText; }
public void setSearchText(String s) { searchText = s; }
public List<Customer> getCustomerList() throws SQLException{
int rowsperpage = 5;
//String offset;
//HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
//offset = request.getParameter("offset");
//if ( offset == null ? "" == null : offset.equals(""))
//if (offset null ? "" == null : offset.equals("0")
//{
//getoffset();
//String voffset;
//voffset = offset;
if (offset == null)
{
offset = "0";
}
System.out.println(offset);
//}
//int pageno = 0;
//int offset = 5;
//offset = "5";
//offset = CustomerBean.this.searchText;
//offset = getResponse();
if(ds==null)
throw new SQLException("Can't get data source");
//get database connection
Connection con = ds.getConnection();
if(con==null)
throw new SQLException("Can't get database connection");
PreparedStatement ps
= con.prepareStatement(
"select petid, petname from pets LIMIT "+ offset + ", " + rowsperpage);
//"select petid, petname from pets LIMIT "+ offset + ", " + rowsperpage);
//get customer data from database
ResultSet result = ps.executeQuery();
List<Customer> list = new ArrayList<Customer>();
//Customer cust2 = new Customer();
while(result.next()){
Customer cust2 = new Customer();
cust2.setpetid(result.getString("petid"));
cust2.setpetname(result.getString("petname"));
//cust2.setAddress(result.getString("address"));
//cust2.setCreated_date(result.getDate("created_date"));
//store all data into a List
list.add(cust2);
}
//List<Customer> list1 = new ArrayList<Customer>();
//Customer cust3 = new Customer();
Customer cust2 = new Customer();
cust2.settmpvar("hello");
list.add(cust2);
return list;
}
public void setoffset(String user_number) {
this.offset = user_number;
System.out.println(offset);
}
public String getoffset() {
return offset;
}
}
newjsf.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
<script type="text/javascript" src="include/jquery.js"></script>
<script type="text/javascript" src="include/myjq.js"></script>
</h:head>
<h:body>
<h:form>
<h:dataTable id="myTable" value="#{Customer.customerList}" var="c" border="1"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
<h:column>
<f:facet name="header">
petid
</f:facet>
#{c.petid}
</h:column>
<h:column>
<f:facet name="header">
petname
</f:facet>
#{c.petname}
</h:column>
</h:dataTable>
<h:inputText
id="userNo"
value="#{Customer.offset}">
</h:inputText>
<h:commandButton id="submit" value="Submit"
action="newjsf.xhtml"/>
</h:form>
</h:body>
</html>
配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
<!--<url-pattern>/faces/*</url-pattern>
<url-pattern>/faces/*</url-pattern>-->
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/newjsf.xhtml</welcome-file>
</welcome-file-list>
</web-app>
也注意到沒有那個懶加載的東西,純粹的限制子句通過mysql,因此完全有效,這可以處理數百萬條記錄,因爲你只加載了很多你放在rowsperpage var。似乎BalusC需要更多的代碼行 – user560131