我在以下项目中遇到了一些问题。目标是当用户选择编辑列命令按钮时,将调用另一个JavaBean。
这是我的clients.xhtml文件:
<ui:define name="title">Companies</ui:define>
<ui:define name="content">
<h:form id="form">
<div class="ui-g">
<div class="ui-g-12">
<div class="card card-w-title">
<p:toolbar>
<f:facet name="left">
<p:button value="New company" icon="ui-icon-plus"
outcome="createClients.xhtml" />
</f:facet>
<f:facet name="right">
<p:commandButton type="button" title="Delete"
icon="ui-icon-delete"
actionListener="#{clientsSelectionView.onRemove()}" />
</f:facet>
</p:toolbar>
<p:dataTable var="company"
value="#{clientsSelectionView.companies}" selectionMode="single"
reflow="true" selection="#{clientsSelectionView.selected}"
rowKey="#{company.pk}" paginator="true" rows="10">
<f:facet name="header">
Company
</f:facet>
<p:column headerText="Nome" sortBy="#{company.name}">
<h:outputText value="#{company.name}" />
</p:column>
<p:column style="width:32px;text-align: center">
<p:commandButton
icon="ui-icon-edit"
title="View" action="#{clientsSelectionView.onEditCompany(company)}">
</p:commandButton>
</p:column>
</p:dataTable>
</div>
</div>
</div>
</h:form>
</ui:define>
这是xhtml文件的JavaBean:
@Named(value = "clientsSelectionView")
@SessionScoped
public class ClientsSelectionView implements Serializable {
@Inject
CompanyController clientController;
private List<Company> companies;
private Company selected;
@PostConstruct
public void init() {
this.companies = clientController.getAllClients();
}
public List<Company> getCompanies() {
return companies;
}
public void setCompanies(List<Company> companies) {
this.companies = companies;
}
public Company getSelected() {
return selected;
}
public void setSelected(Company selected) {
this.selected = selected;
}
public void onRowSelect(SelectEvent event) {
FacesMessage msg = new FacesMessage("Car Selected", ((Company) event.getObject()).getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
System.out.println("Sno qui");
}
public void onRowUnselect(UnselectEvent event) {
FacesMessage msg = new FacesMessage("Car UnSelected", ((Company) event.getObject()).getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRemove() {
System.out.println("Ciao " + selected);
}
public void onEditCompany(Company company) {
System.out.println(company.getName());
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("company", selected);
try {
FacesContext.getCurrentInstance().getExternalContext().redirect("./clients.xhtml");
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是目标JavaBean:
@Named
@RequestScoped
public class CreateCompanyView implements Serializable {
@Inject
CompanyController companyController;
private Company company;
@PostConstruct
public void init() {
this.company = new Company();
}
public boolean canDelete() {
return true;
}
public void save() {
System.out.println(company.getName());
companyController.store(company);
info();
try {
FacesContext.getCurrentInstance().getExternalContext().redirect("./clients.xhtml");
} catch (IOException e) {
e.printStackTrace();
}
}
public void delete() {
companyController.remove(company);
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public void info() {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, "Ok", "L'ordine è stato salvato correttamente!"));
}
}
该模式在哪里出错?如何在与JavaBean链接的xhtml和JSF中的另一个JavaBean的另一个xhtml之间传递数据?是否有任何简单的解决方案来执行这种操作而又没有太多麻烦?