我试图呈现一个页面,但是有可能一些EL表达式会产生一些异常。所以我首先尝试用
我发布了我的XHTML代码:
<c:if test="#{!partyBean.emptySet}">
<c:catch>
<p:panel id="panel" style="margin-bottom:10px;">
<f:facet name="header" >
<h:outputLabel value="#{partyBean.currentparty.name}" />
</f:facet>
<f:facet name="actions">
<p:commandButton id="asktojoin" styleClass="ui-panel-titlebar-icon "
action="#{joinRequestBean.askForParty(partyBean.currentparty)}" value="Ask to Join"/>
</f:facet>
<f:facet name="footer" >
<p:commandButton id="pr" action="#{partyBean.previus()}" value="previousParty" rendered="#{partyBean.hasPrevious()}">
</p:commandButton>
<p:commandButton id="nx" style="margin-right:10px" action="#{partyBean.next()}" value="nextParty" rendered="#{partyBean.hasNext()}">
</p:commandButton>
</f:facet>
<h:panelGrid columns="2">
<h:outputLabel value="Symbol:" />
<h:graphicImage value="#{('/partysymbols/'.concat(partyBean.currentparty.symbol))}" width="200" height="171" />
<h:outputLabel for="program" value="Program:" />
<h:outputLabel id="program" value="#{partyBean.currentparty.program}" />
<h:link id="partyname" outcome="memberlist" value="memberlist">
<f:param name="partyname" value="#{partyBean.currentparty.name}" />
</h:link>
</h:panelGrid>
</p:panel>
</c:catch>
</c:if>
<c:if test="#{partyBean.emptySet}">
<h1>There are no parties at the moment</h1></c:if>
</h:form>
</h:body>
和我的豆子:
@ManagedBean(name="partyBean")
@SessionScoped
public class PartyBean {
@EJB
private PartyManagerLocal ejb;
private PartyDTO[] party;
int i=0;
@PostConstruct
private void init() {
i=0;
refresh();
}
private void refresh(){
Object[] list_o = ejb.getListOfParty().toArray();
party = new PartyDTO[list_o.length];
for(int i=0;i<list_o.length;i++){
party[i]=(PartyDTO)list_o[i];
}
if(i>=list_o.length)
i=list_o.length;
}
public boolean isEmptySet() {
refresh();
return party.length==0;
}
public String next() throws Exception{
refresh();
if(i<party.length-1)
i++;
return "partyview.xhtml?faces-redirect=true";
}
public String previus() throws Exception{
refresh();
if(i>0)
i--;
return "partyview.xhtml?faces-redirect=true";
}
public boolean hasPrevious(){
return i>=1;
}
public boolean hasNext(){
return i<party.length-1;
}
public PartyDTO getCurrentparty() throws Exception{
return party[i];
}
}
我很抱歉向你展示我的整个代码,但我不知道错误在哪里。然后我也为我的可怕的代码道歉,但目前我只需要它的工作。
关键条件是数组不能为空。可能发生,我的数组将是空的,但在这种情况下,我需要通知用户,而不是重定向到500错误页面。
提前感谢,Samuele
您可以通过在panelGroup
中包围代码并使用rendered
属性来有条件地封装您想要的内容。
<h:panelGroup rendered="#{PartyBean.isEmptySet}">
<!-- Code inside here... -->
<!-- This will render if isEmptySet returns true -->
</h:panelGroup>