在JSF from DB中编辑datatable中的行
我不知道是否让事情变得如此复杂,但我不知道如何从我的datatable中更新一行,以下是我的代码: listado.xhtml在JSF from DB中编辑datatable中的行,jsf,datatable,edit,backing-beans,Jsf,Datatable,Edit,Backing Beans,我不知道是否让事情变得如此复杂,但我不知道如何从我的datatable中更新一行,以下是我的代码: listado.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 xml
<?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>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:dataTable border="1" value="#{guardarBean.listaCustomer}" var="o">
<h:column>
<f:facet name="header">Customer ID</f:facet>
#{o.customerId}
</h:column>
<h:column>
<f:facet name="header">Discount Code</f:facet>
#{o.discountCode.discountCode}
</h:column>
<h:column>
<f:facet name="header">Zip</f:facet>
#{o.zip.zipCode}
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
<h:inputText value="#{o.name}" rendered="#{guardarBean.isEditable}"/>
<h:outputText value="#{o.name}" rendered="#{not guardarBean.isEditable}"/>
</h:column>
<h:column>
<f:facet name="header">Address 1</f:facet>
<h:outputText value="#{o.addressline1}" />
</h:column>
<h:column>
<f:facet name="header">Address 2</f:facet>
#{o.addressline2}
</h:column>
<h:column>
<f:facet name="header">City</f:facet>
#{o.city}
</h:column>
<h:column>
<f:facet name="header">State</f:facet>
#{o.state}
</h:column>
<h:column>
<f:facet name="header">Phone</f:facet>
#{o.phone}
</h:column>
<h:column>
<f:facet name="header">Fax</f:facet>
#{o.fax}
</h:column>
<h:column>
<f:facet name="header">Email</f:facet>
#{o.email}
</h:column>
<h:column>
<f:facet name="header">Credit Limit</f:facet>
#{o.creditLimit}
</h:column>
<h:column>
<f:facet name="header">Edit</f:facet>
<h:commandButton action="#{guardarBean.editAction()}" value="Editar" />
</h:column>
<h:column>
<f:facet name="header">Save</f:facet>
<h:commandButton value="Save Changes" action="#{guardarBean.editar(o)}">
<f:ajax render="@form" execute="@form"/>
</h:commandButton>
</h:column>
<h:column>
<f:facet name="header">Delete</f:facet>
<h:commandButton action="#{guardarBean.borrar(o)}" value="Borrar">
<f:ajax render="@form" />
</h:commandButton>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
Facelet标题
客户ID
#{o.customerId}
折扣代码
#{o.discountCode.discountCode}
拉链
#{o.zip.zipCode}
名称
地址1
地址2
#{o.addressline2}
城市
#{o.city}
陈述
#{o.state}
电话
#{o.phone}
传真
#{o.fax}
电子邮件
#{o.email}
信用额度
#{o.creditLimit}
编辑
拯救
删除
guardarBean.java
import app.dao.CustomerFacadeLocal;
import app.dao.DiscountCodeFacadeLocal;
import app.dao.MicroMarketFacadeLocal;
import app.entity.Customer;
import app.entity.DiscountCode;
import app.entity.MicroMarket;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
@ManagedBean
@RequestScoped
public class GuardarBean {
@EJB
private CustomerFacadeLocal customerFacade1;
@EJB
private MicroMarketFacadeLocal microFacade;
@EJB
private DiscountCodeFacadeLocal discFacade;
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getFax() {
return fax;
}
public void setFax(String fax) {
this.fax = fax;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getCredit_limit() {
return credit_limit;
}
public void setCredit_limit(Integer credit_limit) {
this.credit_limit = credit_limit;
}
public String getDiscount() {
return discount;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
private Integer id;
private String name;
private String address1;
private String address2;
private String city;
private String state;
private String phone;
private String fax;
private String email;
private Integer credit_limit;
private String discount;
private String zip;
private boolean isEditable;
private List<DiscountCode> listaDiscount;
private List<Customer> listaCustomer;
public List<Customer> getListaCustomer() {
//FacesContext.getCurrentInstance().getExternalContext().getSession(true);
listaCustomer =(List<Customer>)customerFacade1.findAll();
return listaCustomer;
}
public void setListaCustomer(List<Customer> listaCustomer) {
this.listaCustomer = listaCustomer;
}
public List<DiscountCode> getListaDiscount() {
listaDiscount = (List<DiscountCode>)discFacade.findAll();
return listaDiscount;
}
public void setListaDiscount(List<DiscountCode> listaDiscount) {
this.listaDiscount = listaDiscount;
}
/**
* Creates a new instance of GuardarBean
*/
public GuardarBean() {
}
public void insertar(){
Customer customer = new Customer();
DiscountCode dc = discFacade.find(discount.toCharArray()[0]);
customer.setDiscountCode(dc);
MicroMarket mm = microFacade.find(zip);
customer.setZip(mm);
customer.setName(name);
customer.setCustomerId(id);
customer.setAddressline1(address1);
customer.setAddressline2(address2);
customer.setCity(city);
customer.setCreditLimit(credit_limit);
customer.setEmail(email);
customer.setFax(fax);
customer.setPhone(phone);
customer.setState(state);
customerFacade1.create(customer);
}
public boolean isIsEditable() {
return isEditable;
}
public void setIsEditable(boolean isEditable) {
this.isEditable = isEditable;
}
public void editAction() {
setIsEditable(true);
}
public void editar(Customer customer){
customerFacade1.edit(customer);
setIsEditable(false);
}
public void borrar(Customer c)
{
customerFacade1.remove(c);
}
}
导入app.dao.customerfacadolocal;
导入app.dao.DiscountCodeFacadeLocal;
导入app.dao.MicroMarketFacadeLocal;
导入app.entity.Customer;
导入app.entity.DiscountCode;
导入app.entity.MicroMarket;
导入java.util.List;
导入javax.ejb.ejb;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.RequestScope;
导入javax.faces.context.FacesContext;
@ManagedBean
@请求范围
公共类瓜达豆{
@EJB
私人客户facade本地客户facade1;
@EJB
私人微型市场;地方微型学院;
@EJB
私人折扣CodeFacadeLocal Disc Facade;
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getAddress1(){
回信地址1;
}
公共无效setAddress1(字符串地址1){
this.address1=address1;
}
公共字符串getAddress2(){
回信地址2;
}
公共无效setAddress2(字符串地址2){
this.address2=address2;
}
公共字符串getCity(){
回归城市;
}
公共城市(字符串城市){
this.city=城市;
}
公共字符串getState(){
返回状态;
}
公共无效设置状态(字符串状态){
this.state=状态;
}
公共字符串getPhone(){
回电话;
}
公用无效设置电话(字符串电话){
this.phone=电话;
}
公共字符串getFax(){
回传传真;
}
公用传真(字符串传真){
this.fax=传真;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共整数getCredit_limit(){
退货信用额度;
}
公共无效设置信用额度(整数信用额度){
this.credit\u limit=信用额度;
}
公共字符串getDiscount(){
退货折扣;
}
公共折扣(字符串折扣){
这个。折扣=折扣;
}
公共字符串getZip(){
返回拉链;
}
公共void setZip(字符串zip){
this.zip=zip;
}
私有整数id;
私有字符串名称;
私有字符串地址1;
私有字符串地址2;
私人城市;
私有字符串状态;
私人电话;
私人字符串传真;
私人字符串电子邮件;
私人整数信用额度;
私人字符串折扣;
私人字符串拉链;
私有布尔可编辑;
私人名单;
私有列表列表;
公共列表getListaCustomer(){
//FacesContext.getCurrentInstance().getExternalContext().getSession(true);
listaCustomer=(List)customerFacade1.findAll();
返回listaCustomer;
}
public void setListaCustomer(列表列表列表){
this.listaCustomer=listaCustomer;
}
公共列表getListaDiscount(){
listadiscont=(List)discpeacade.findAll();
返回列表查询;
}
公共无效设置listaDiscount(列表listaDiscount){
this.listadiscont=listadiscont;
}
/**
*创建GuardarBean的新实例
*/
公豆{
}
公共无效插入器(){
客户=新客户();
折扣代码dc=discFacade.find(折扣.toCharray()[0]);
customer.setDiscountCode(dc);
MicroMarket mm=microFacade.find(zip);
customer.setZip(mm);
customer.setName(name);
customer.setCustomerId(id);
customer.setAddressline1(address1);
customer.setAddressline2(address2);
customer.setCity(城市);
客户。设置信用额度(信用额度);
customer.setEmail(电子邮件);
客户传真(传真);
customer.setPhone(电话);
客户设置状态(状态);
customerFacade1.创建(客户);
}
公共布尔值isIsEditable(){
返回可编辑;
}
public void setIsEditable(布尔值isEditable){
this.isEditable=isEditable;
}
公共行动{
可设置为可编辑(true);