我在JavaServer Faces中初学者。我用setter和getters相应的index.xhtml文件创建了Person类。我从数据库中获取值并显示在index.xhtml页面中。但是在新的second.xhtml文件中,我无法从Person Managed bean java类中获取这些值。问题是什么?我搜索了托管bean的配置但找不到答案。我的源代码在这里:
index.xhtml
<?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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Facelet Title</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></link>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"></link>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--Row Element is Selected-->
<script>
$(document).ready(function() {
//get row contents
$("#userTbl tbody tr").click(function(){
var tableData=$(this).children("td").map(function(){
return $(this).text();
}).get();
var td=tableData[0]+'*'+tableData[1]+'*'+tableData[2]+'*'+tableData[3];
// alert(td);
var tr1=tableData[0];
var tr2=tableData[1];
var tr3=tableData[2];
var tr4=tableData[3];
// document.getElementById('editForm:tx1').innerHTML = tr;
document.getElementById('editForm:tx1').value = tr1;
document.getElementById('editForm:tx2').value = tr2;
document.getElementById('editForm:tx3').value = tr3;
document.getElementById('editForm:tx4').value = tr4;
});
});
});
</script>
<!--ROw Elements are selected is finished-->
<h:outputStylesheet library="css" name="mainStyle.css"/>
<h:outputStylesheet library="css" name="popup.css"/>
<h:outputStylesheet library="css" name="anim.css"/>
<h:outputStylesheet library="css" name="profile.css"/>
</h:head>
<h:body>
<div class="upper" style="margin-left: 200px;">
<h:form id="editForm">
<h:inputText styleClass="inpp" id="tx1" value="#{Person.id}" style="display: none;"/>
<h:inputText styleClass="inpp" id="tx2" value="#{Person.fname}" style="display: none;"/>
<h:inputText styleClass="inpp" id="tx3" value="#{Person.lname}" style="display: none;"/>
<h:inputText styleClass="inpp" id="tx4" value="#{Person.dept}" style="display: none;"/>
<h:commandButton id="butr" onclick="popp();" value="delete" action="#{Person.dellete()}" style="display: none;"/>
</h:form>
</div>
<form>
<div class="container" >
<h2 style="margin-left: 500px;">Admin Paneli</h2>
<div class="updat">
<button type="button" id="upd" onClick="reply_click(this.id)">Qo`shish</button>
</div>
<div class="form-group pull-right" style="margin-right: 100px;">
<input type="text" class="form-control search" placeholder="What you lookin for?"></input>
</div>
<table class="table table-striped" id="userTbl">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
</tr>
</thead>
<tbody>
<ui:repeat var="o" value="#{main.list}" varStatus="status">
<tr>
<td><h:outputText value="#{o.id}" /></td>
<td><h:outputText value="#{o.fname}" /></td>
<td><h:outputText value="#{o.lname}"/></td>
<td><h:outputText value="#{o.dept}" /></td>
</tr>
</ui:repeat>
</tbody>
</table>
</div>
</form>
</h:body>
</html>
My Person.java
package net.best;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name="Person")
@RequestScoped
public class Person {
private String id;
private String fname;
private String lname;
private String dept;
private String sid;
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public Person(){
}
public String dellete(){
try {
Connection connection =null;
DBConnect obj_DB_connect = new DBConnect();
connection=obj_DB_connect.getConnection();
PreparedStatement ps=connection.prepareStatement("delete from person where lname='"+lname+"'");
ps.executeUpdate();
} catch (Exception e) {
System.out.println(e);
}
return "/index.xhtml?faces-redirect=true";
}
public String update(){
try {
Connection connection =null;
DBConnect obj_DB_connect = new DBConnect();
connection=obj_DB_connect.getConnection();
PreparedStatement ps=connection.prepareStatement("update person set id='"+id+"',fname='"+fname+"',lname='"+lname+"',dept='"+dept+"' where id='"+id+"'");
ps.executeUpdate();
} catch (Exception e) {
System.out.println(e);
}
return "/index.xhtml?faces-redirect=true";
}
//Adding New Users
public void add_Person(){
try {
Connection connection =null;
DBConnect obj_DB_connect = new DBConnect();
connection=obj_DB_connect.getConnection();
PreparedStatement ps=connection.prepareStatement("insert into person(id,fname,lname,dept) value('"+id+"','"+fname+"','"+lname+"','"+dept+"')");
ps.executeUpdate();
} catch (Exception e) {
System.out.println(e);
}
}
}
MainController.java
package net.best;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "main")
@SessionScoped
public class MainController {
ListQuery query =new ListQuery();
private List<Person> list=new ArrayList<Person>();
public List<Person> getList(){
list=query.listPerson();
return list;
}
public void setList(List<Person> list){
this.list=list;
}
}
>Second.xhtml
<?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://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Facelet Title</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></link>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"></link>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--Row Element is Selected-->
<script>
$(document).ready(function() {
//get row contents
$("#userTbl tbody tr").click(function(){
var tableData=$(this).children("td").map(function(){
return $(this).text();
}).get();
var td=tableData[0]+'*'+tableData[1]+'*'+tableData[2]+'*'+tableData[3];
var tr1=tableData[0];
var tr2=tableData[1];
var tr3=tableData[2];
var tr4=tableData[3];
});
});
</script>
<h:outputStylesheet library="css" name="mainStyle.css"/>
<h:outputStylesheet library="css" name="popup.css"/>
<h:outputStylesheet library="css" name="anim.css"/>
<h:outputStylesheet library="css" name="profile.css"/>
</h:head>
<h:body>
<!--//USer table-->
<form>
<div class="container">
<h2 style="margin-left: 500px;">Admin Paneli</h2>
<div class="form-group pull-right" style="margin-right: 100px;">
<input type="text" class="form-control search" placeholder="What you lookin for?"></input>
</div>
<table class="table table-striped" id="userTbl">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
</tr>
</thead>
<tbody>
<ui:repeat var="obj" value="#{main.list}" varStatus="status">
<tr>
<td><h:outputText value="#{obj.id}" /></td>
<td><h:outputText value="#{obj.fname}" /></td>
<td><h:outputText value="#{obj.lname}"/></td>
<td><h:outputText value="#{obj.dept}" /></td>
</tr>
</ui:repeat>
</tbody>
</table>
<h1>sdfsf</h1>
<h:outputText value="Hello"></h:outputText>
<h:outputText value="#{Person.dept}"></h:outputText>
</div>
</form>
<!--//User table is finished-->
</h:body>
</html>