Jsf 2 jsf会话或配置问题
这是一个来自mkyong.com的示例,我稍微修改了一下。我添加了输入文本和命令 发送mysql中分页的下一个偏移量。第一页按预期偏移量0自动加载。如果我在inputtext中键入5,它实际上可以工作,下一页显示接下来的5条记录。然而,若我现在输入10,它将停止工作,页面将继续尝试加载。我等了5分钟,什么也没等。另一次重试,0首次加载输入15只是为了测试,成功了。但在第一次使用commandButton之后,为了让它工作起来,它只是停了下来 在url中显示当前会话ID。这是会话问题还是配置问题 我是jsf新手,正在尝试学习如何正确地发送请求以使页面显示某些内容。我直接从oracles网站上的javaeetutorial6获得了inputText和commandButton的想法,即guessnumber/duke示例Jsf 2 jsf会话或配置问题,jsf-2,Jsf 2,这是一个来自mkyong.com的示例,我稍微修改了一下。我添加了输入文本和命令 发送mysql中分页的下一个偏移量。第一页按预期偏移量0自动加载。如果我在inputtext中键入5,它实际上可以工作,下一页显示接下来的5条记录。然而,若我现在输入10,它将停止工作,页面将继续尝试加载。我等了5分钟,什么也没等。另一次重试,0首次加载输入15只是为了测试,成功了。但在第一次使用commandButton之后,为了让它工作起来,它只是停了下来 在url中显示当前会话ID。这是会话问题还是配置问题
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;
}
}
包模型;
//导入java.util.Date;
公共类客户{
公共字符串petid;
公共字符串名称;
公共字符串tmpvar;
私有字符串偏移量;
//公共字符串地址;
//公共日期创建日期;
//私有字符串搜索文本;
//公共字符串getSearchText(){return searchText;}
//public void setSearchText(字符串s){searchText=s;}
公共字符串getpetid(){
返回petid;
}
公共void setpetid(字符串petid){
this.petid=petid;
}
公共字符串getpetname(){
返回petname;
}
公共void setpetname(字符串petname){
this.petname=petname;
}
/*公共字符串getAddress(){
回信地址;
}
公共无效设置地址(字符串地址){
this.address=地址;
}
公共日期getCreated_Date(){
返回创建日期;
}
公共作废设置创建日期(创建日期){
this.created_date=创建日期;
}*/
公共字符串gettmpvar(){
返回tmpvar;
}
公共无效设置tmpvar(字符串tmpvar){
this.tmpvar=tmpvar;
}
}
包装客户;
导入java.io.Serializable;
导入java.sql.Connection;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.util.ArrayList;
导入java.util.List;
导入javax.annotation.Resource;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.SessionScoped;
导入javax.faces.context.FacesContext;
导入javax.naming.Context;
导入javax.naming.InitialContext;
导入javax.naming.NamingException;
导入javax.servlet.http.HttpServletRequest;
导入javax.sql.DataSource;
进口型号。客户;
@ManagedBean(name=“客户”)
//@会议范围
公共类CustomerBean实现了可序列化{
int用户号;
串偏移量;
//字符串搜索文本;
//资源注入
@资源(name=“jdbc/petback2”)
私有数据源ds;
//如果不支持资源注入,您仍然可以手动获取它。
/*公共CustomerBean(){
试一试{
Context ctx=新的InitialContext();
ds=(数据源)ctx.lookup(“jdbc/petback2”);
}捕获(NamingE例外){
}
}*/
//连接到数据库并获取客户列表
公共字符串搜索文本;
//私有字符串偏移量;
公共字符串getSearchText(){return searchText;}
public void setSearchText(字符串s){searchText=s;}
public List getCustomerList()引发SQLException{
int rowsperpage=5;
//串偏移量;
//HttpServletRequest=(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
//offset=request.getParameter(“offset”);
//if(offset==null?”“==null:offset.equals(“”)
//如果(偏移量为null?“==null:offset.equals(“0”)
//{
//getoffset();
//弦频偏;
//voffset=偏移量;
如果(偏移量==null)
{
offset=“0”;
}
系统输出打印项次(偏移量);
//}
//int pageno=0;
//整数偏移=5;
//offset=“5”;
//偏移量=CustomerBean.this.searchText;
//offset=getResponse();
如果(ds==null)
抛出新的SQLException(“无法获取数据源”);
//获取数据库连接
Connection con=ds.getConnection();
如果(con==null)
抛出新的SQLException(“无法获取数据库连接”);
编制报表
=con.准备陈述(
“从pets限制中选择petid、petname”+偏移量+”、“+行页面);
//“从pets限制中选择petid、petname”+偏移量+”、“+行页面);
//从数据库中获取客户数据
结果集结果=ps.executeQuery();
列表=新的ArrayList();
//客户cust2=新客户();
while(result.next()){
客户cust2=新客户();
cust2.setpetid(result.getString(“petid”);
cust2.setpetname(result.getString(“petname”));
//cust2.setAddress(result.getString(“地址”);
//cust2.setCreated_date(result.getDate(“created_date”);
//将所有数据存储到列表中
添加列表(cust2);
}
//List list1=新的ArrayList();
//客户cust3=新客户();
客户cust2=新客户();
cust2.settmpvar(“你好”);
添加列表(cust2);
退货清单;
}
公共void setoffset(字符串用户号){
this.offset=用户号;
系统输出打印项次(偏移量);
}
公共字符串getoffset(){
返回偏移量;
}
}
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>
佩蒂德
#{c.petid}
宠物名
<?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>
<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;
}