我是JSF 2,PrimeFaces和EJB的初学者,我正在制作一个后端应用程序,我成功地在数据表上列出了数据库记录,并在数据表的每条记录的前面创建了一个命令按钮,所以当我点击那个按钮时我可以删除一行,但我制作了所有的代码但是当我点击按钮时没有发生任何事情。这是代码。谢谢你。
EJB中的方法:
@Override
public void DeleteCitizen(Citizen citizen) {
Citizen detachCit = entityManager.merge(citizen);
entityManager.remove(detachCit);
//entityManager.remove(entityManager.merge(citizen));
}
支持bean:
@ManagedBean
@SessionScoped
public class CitizenCtr {
private List<Citizen> citizens = new ArrayList<Citizen>();
private DataModel<Citizen> datamodel = new ListDataModel<Citizen>();
//the Model
Citizen cit = new Citizen();
//injection of the proxy
@EJB
CitizenServicesLocal citizenServiceLocal;
public List<Citizen> getCitizenss() {
citizens = citizenServiceLocal.ListAllCitizen();
return citizens;
}
public DataModel<Citizen> getDatamodel() {
datamodel.setWrappedData(citizenServiceLocal.ListAllCitizen());
return datamodel;
}
public void setDatamodel(DataModel<Citizen> datamodel) {
this.datamodel = datamodel;
}
public Citizen getCit() {
return cit;
}
public void setCit(Citizen cit) {
this.cit = cit;
}
// Login operation
public String TryLogin() {
String goTo = null;
Citizen citizenFound = citizenServiceLocal.Login(cit.getEmail(), cit.getPassword());
if (citizenFound != null) {
cit = citizenFound;
goTo = "/CitizenProfile/Profile?send-redirect=true";
System.out.println("Welcome you are logged In ");
} else {
System.out.println("please enter valid data ! ");
goTo = "/welcome?send-redirect=true";
}
return goTo;
}
//Subscribe operation
public String DoSubscribe() {
String Goto = null;
citizenServiceLocal.Subscribe(cit);
Goto = "/welcome?sendredirect=true";
return Goto;
}
//Update profile operation
public String DoUpdateProfile() {
String Goto = null;
citizenServiceLocal.updateProfile(cit);
Goto = "/CitizenProfile/Profile?sendredirect=true";
return Goto;
}
//Logout operation
public String DoLogout() {
String Goto = "/welcome?sendredirect=true";
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return Goto;
}
public Citizen GetAllCitizen() {
List<Citizen> list;
list = citizenServiceLocal.ListAllCitizen();
return (Citizen) list;
}
// Ban Operation
public void BanCitizen() {
citizenServiceLocal.DeleteCitizen(cit);
}
}
这是XHTML文件:
<h2>Citizen Management</h2>
<p:link action="#{citizenCtr.BanCitizen()}" value="erase" >
<p:ajax update="mytable"></p:ajax>
</p:link>
<p:dataTable id="mytable"
value="#{citizenCtr.datamodel}"
var="citizen">
<f:facet name="header"> List of Citizens </f:facet>
<p:column>
<f:facet name="header">
<h:outputText value="CIN"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.CIN}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Name"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.firstName}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="LastName"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.scondName}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Addess"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.email}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Email"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.password}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Password"></h:outputText>
</f:facet>
<h:outputText value="#{citizen.adresse}"></h:outputText>
</p:column>
<p:column>
<p:commandButton action="#{citizenCtr.BanCitizen()}"
value="Ban"
ajax="true"
update="mytable"
process="@this">
</p:commandButton>
</p:column>
</p:dataTable>
答案 0 :(得分:1)
很难遵循您的逻辑,但是您使用DeleteCitizen(Citizen)
值来调用this.cit
,这个值不明确。
如果您使用的是EL 2.2(Servlet 3.0),则可以这样做:
在JSF中:
<p:commandButton action="#{citizenCtr.banCitizen(citizen)}" value="Ban" ajax="true" update="mytable" process="@this">
在控制器中
public void banCitizen(Citizen citizenToDelete) {
citizenServiceLocal.deleteCitizen(citizenToDelete);
}
改进注意事项:
遵循命名约定。方法名称以小写开头。
每次请求都可能会多次调用getter。对于每个请求,所有公民必须多次查询数据库将非常耗时。例如,将公民列表存储为控制器的属性; getter应该只返回该值。当执行修改列表内容的操作(v.g。banCitizen(Citizen)
)时,该操作应该负责更新属性的值。
检查日志以查看是否有错误消息。
答案 1 :(得分:0)
为什么要对commandButton采取行动?试试actionListener ......
<p:commandButton actionListener="#{citizenCtr.banCitizen(citizen)}" value="Ban" ajax="true" update="mytable" process="@this">