这是我稍微修改过的mkyong.com的样本。我添加了一个inputtext和命令 在mysql中发送下一个分页偏移量。第一页自动加载,偏移量为0,如预期的那样。如果我在inputtext中输入5,它实际上是有效的,下一页显示接下来的5条记录。但是,如果我现在输入10,它将停止工作,页面只是继续尝试加载。我等了5分钟,没事。另一个重试,0首先加载进入15只是为了测试,它工作。但是在第一次使用commandButton之后尝试使其工作,它只是停滞不前。
在url中显示当前的sessionid。这是会话问题还是配置问题?
我是jsf的新手,我正在尝试学习如何正确发送请求以使页面显示某些内容。我直接从oracles网站上的javaeetutorial6获得了inputText和commandButton的想法,即guessnumber / duke示例。
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>
答案 0 :(得分:0)
让它工作,转到ui:重复,并使用下面的发送获取 而不是发布:
<h:outputLink id="link1" value="newui.xhtml?voffset=#{Customer.offset - 5}">
<h:outputText value="prev" />
</h:outputLink>
<h:outputLink id="link2" value="newui.xhtml?voffset=#{Customer.offset + 5}">
<h:outputText value="next" />
</h:outputLink>
在豆子里:
HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
offset = req.getParameter("voffset");
if (offset == null) {
offset = "0";
}
和
public void setoffset(String voffset) {
this.offset = voffset;
//System.out.println(offset);
}
public String getoffset() {
return offset;
}
我不知道怎么做,但是set / get没有调用它,可能是自动处理的。 无论如何,现在分页工作就像一个冠军,而且比数据表更有控制力。