当我尝试通过dataTable元素中的commandLink从数据库中删除行内容时,我遇到了麻烦。 所以我得到了带有viewScop和managedScoped的bean,如下所示:
@ViewScoped
@ManagedBean
public class DivisiController implements Serializable{
private static final long serialVersionUID = 1L;
private String konten;
private String deskripsi;
private Long id;
private Divisi selectedDivisi;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Divisi getSelectedDivisi() {
if(selectedDivisi == null){
selectedDivisi = new Divisi();
}
return selectedDivisi;
}
public void setSelectedDivisi(Divisi selectedDivisi) {
this.selectedDivisi = selectedDivisi;
}
public String getKonten() {
return konten;
}
public void setKonten(String konten) {
this.konten = konten;
}
public String getDeskripsi() {
return deskripsi;
}
public void setDeskripsi(String deskripsi) {
this.deskripsi = deskripsi;
}
public void halo(){
System.out.println("Hi, I'm halo");
}
public void delete(){
DivisiDAO dao = new DivisiDAO();
dao.beginTransaction();
try{
Divisi persistedDivisi = dao.findReferenceOnly(selectedDivisi.getId());
dao.delete(persistedDivisi);
}
catch(Exception e){
dao.rollback();
addMessage("System Error", "Please try again later.");
e.printStackTrace();
}
dao.commitAndCloseTransaction();
}
public void addMessage(String summary, String detail) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
然后,当我从jsf页面点击删除按钮时,我尝试设置selectedDivisi值。并希望我的删除方法将删除所选的值。
我尝试通过f:setPropertyActionListener
但不幸的是,我总是在我的支持bean(DivisiController)中使用null值。 我尝试调试,在我的selectedDivisi setter设置断点,结果是,selectedDivisi值为null。看起来,我选择的Divisi从未使用我的按钮设置#{item}。 以下是我如何设法从jsf中绘制html,并调用我的支持bean来执行操作的片段。
<div class="col-md-12">
<!-- Custom Tabs -->
<h:form id="form">
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li class="active" >
<a id="lampiran" data-toggle="tab" href="#tab_2" aria-expanded="false">
<i class="fa fa-list">
</i> Daftar Divisi</a></li>
<li class="">
<a data-toggle="tab" href="#tab_1" aria-expanded="false">
<i class="fa fa-pencil"></i> Buat Divisi</a>
</li>
</ul>
<div class="tab-content">
<div id="tab_2" class="tab-pane active">
<h:panelGroup layout="block" id="berkas" class="row">
<div class="col-md-12">
<div class="box-footer text-right top-buffer">
<ui:include src="list.xhtml"/>
</div>
</div>
</h:panelGroup>
</div><!-- /.tab-pane -->
<div id="tab_1" class="tab-pane">
<h:panelGroup layout="block" id="tembusan" class="row">
<div class="col-md-12">
<ui:include src="create-form.xhtml"/>
</div>
</h:panelGroup>
<div class="box-footer text-right top-buffer">
<h:commandLink action="#{divisiController.create}" class="btn btn-primary">
Buat Baru <i class="fa fa-arrow-circle-right"></i>
</h:commandLink>
</div>
</div><!-- /.tab-pane -->
</div><!-- /.tab-content -->
</div><!-- nav-tabs-custom -->
</h:form>
<p:confirmDialog header="Confirmation"
message="#{divisiController.selectedDivisi.id}"
global="true"
showEffect="fade"
hideEffect="fade"
widgetVar="confirmDialogWidget">
<h:form>
<h:outputText value="#{divisiController.selectedDivisi.id}"/>
<p:commandButton value="Yes" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" action="#{divisiController.delete()}" />
<p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</h:form>
</p:confirmDialog>
</div>
那是index.html,其中包括list.xhtml(如你所见),这里是list.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">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:dv="http://xmlns.jcp.org/jsf/composite/mycomp">
<div class="row">
<div class="col-md-12 complex">
<dv:data-table value="#{divisiController.view()}"
id="surat"
scrollable="true"
scrollableHeight="500">
<p:column headerText="Konten" width="40%" styleClass="text-left" style="width: 44%">
<h:outputText value="#{item.konten}"/>
</p:column>
<p:column headerText="Deskripsi" width="40%" styleClass="text-left" style="width: 44%">
<h:outputText value="#{item.deskripsi}"/>
</p:column>
<p:column headerText="Aksi" styleClass="text-left">
<h:outputLink styleClass="btn btn-primary text-white" value="edit.xhtml">
<i class="fa fa-edit"></i>
<f:param name="id" value="#{item.id}" />
</h:outputLink>
<p:commandLink styleClass="btn btn-primary text-white" update=":form">
<h:panelGroup styleClass="fa fa-trash" />
<f:setPropertyActionListener value="#{item}" target="#{divisiController.selectedDivisi}" />
<p:confirm header="Confirmation" message="Are you sure?" icon="ui-icon-alert" />
</p:commandLink>
</p:column>
</dv:data-table>
</div>
</div>
</ui:composition>
正如您在最后一个片段中看到的那样,我尝试通过此片段设置selectedDivisi:
<f:setPropertyActionListener value="#{item}" target="#{divisiController.selectedDivisi}" />
但它总是错误的。如果我尝试直接将{item}
传递给我的bean删除方法(如果我使用输入参数设置delete方法),那么它将与之前的技术(来自<f:setPropertyActionListener>
)相同。 / p>
那么,这是怎么发生的,又是什么让它变得混乱?
先谢谢你的帮助。
答案 0 :(得分:0)
我没有足够的声誉来发表评论。我认为由于范围而设置为空。
如果你在View scoped managed bean with setPropertyActionListener看到@BalusC回答,他建议我们在@ViewScoped中使用f:param和f:viewParam
更新@ user3057361的评论:如果您使用的是ConfirmDialog的全局模式,请不要从confirmDialog的yes按钮调用action =&#34;#{divisiController.delete()}。恕我直言,list.xhtml中的p:commandLink应更改为a。保留其他一切。 confirmDialog中的yes和no按钮不应具有全局确认对话框的操作方法。单击“是”后,将自动调用confirmDialog调用者中的操作