我正在尝试使用 JSF 和 PrimeFaces 设置 2 个链接选择框。我已经按照官方网站上的示例创建了它们,但是:
- 当我使用新按钮进入页面时(没有要显示的现有记录),更改事件将从控制器返回 500 错误
- 当我使用编辑按钮(存在现有记录)进入页面时,更改事件不会返回 500 错误,但是来自选择的 id 未映射到记录。
任何帮助,将不胜感激。
以下相关代码位:
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:o="http://omnifaces.org/ui"
template="/WEB-INF/templates/page.xhtml">
<f:metadata>
<o:viewParam name="userUploadedWorkId" value="#{userUploadedWorkController.userUploadedWork}"
converter="#{userUploadedWorkConverter}"
converterMessage="Solicitare eronata. Va rugam sa navigati in aceasta pagina dintr-o lista de lucrari"/>
</f:metadata>
<ui:define name="bread-crumb">
<p:menuitem value="Lucrarile mele" outcome="user-uploaded-work-list?faces-redirect=true"/>
<p:menuitem
value="#{userUploadedWorkController.userUploadedWork.id eq null ? 'Lucrare noua' : userUploadedWorkController.userUploadedWork.title}"
url="#"/>
</ui:define>
<ui:define name="page-content">
<h:form>
<p:panelGrid columns="2" columnClasses="right-aligned,raw" id="user-uploaded-work-details">
<f:facet name="header">Detalii lucrare</f:facet>
<p:outputLabel value="Titlu" for="uuwTitle"/>
<p:inputText id="uuwTitle" value="#{userUploadedWorkController.userUploadedWork.title}" required="true"
style="width: 400px"/>
<p:outputLabel value="Capitol" for="chapter"/>
<p:selectOneMenu style="width: 400px" required="true" id="chapter" value="#{userUploadedWorkController.userUploadedWork.criterion eq null?'':userUploadedWorkController.userUploadedWork.criterion.chapter.id}" effect="fade">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{chapters}" var="ch" itemLabel="#{ch.name}" itemValue="#{ch.id}"/>
<p:ajax update="uuwCriterion" listener="#{userUploadedWorkController.chapterChange}"/>
</p:selectOneMenu>
<p:outputLabel value="Criteriu" for="uuwCriterion"/>
<p:selectOneMenu style="width: 400px" required="true" id="uuwCriterion" value="#{userUploadedWorkController.userUploadedWork.criterion eq null?'':userUploadedWorkController.userUploadedWork.criterion.id}" effect="fade">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{userUploadedWorkController.criteria}" var="criterion" itemLabel="#{criterion.name}" itemValue="#{criterion.id}"/>
</p:selectOneMenu>
<f:facet name="footer">
<p:message for="uuwTitle"/>
<p:message for="uuwCriterion"/>
<p:message for="uuwChapter"/>
<p:commandButton action="#{userUploadedWorkController.save}" value="Salveaza atribute" process="@form"
update="@form :messages" icon="ui-icon-disk"/>
<p:button outcome="user-uploaded-work-list?faces-redirect=true" value="Lista lucrari"
icon="ui-icon-arrowreturnthick-1-e"/>
</f:facet>
</p:panelGrid>
</h:form>
</ui:define>
托管bean:
@Named
@ViewScoped
public class UserUploadedWorkController implements Serializable {
private static final long serialVersionUID = -4736897416993974840L;
@Inject
private OrganizationalChartService service;
private UserUploadedWork userUploadedWork = new UserUploadedWork();
private List<Criterion> criteria;
public UserUploadedWork getUserUploadedWork() {
return userUploadedWork;
}
public void setUserUploadedWork(UserUploadedWork userUploadedWork) {
this.userUploadedWork = userUploadedWork;
}
@PostConstruct
public void init() {
criteria = service.findAllCriteria();
}
public List<Criterion> getCriteria() {
return criteria;
}
public void setCriteria(List<Criterion> criteria) {
this.criteria = criteria;
}
public String save() {
userUploadedWork.setCriterion(service.findById(Criterion.class, userUploadedWork.getCriterion().getId()));
userUploadedWork.setUser(service.findBySimpleProperty(User.class, "email", LoginController.getRequest().getUserPrincipal().getName()));
userUploadedWork = service.merge(userUploadedWork);
Messages.addFlashGlobalInfo("Salvare efectuata cu succes");
return "user-uploaded-work-list?faces-redirect=true";
}
public void chapterChange() {
if (userUploadedWork.getChapter() != null
&& userUploadedWork.getChapter().getId() != null) {
criteria = service.findCriteriaByChapter(userUploadedWork.getChapter().getId());
} else {
criteria = new ArrayList<Criterion>();
}
}
}