我正在尝试呈现一个页面,但某些 EL 表达式可能会生成一些异常。因此,我首先尝试使用 < c:if> 逐块进行排序,使其仅通过检查关键条件来显示代码。但我看到,如果出现“错误”,我的页面再次被重定向为“HTTP 500 - 内部服务器错误”。所以我坚持认为,在任何情况下,块内的 EL 表达式都可能是计算出来的,如果 < c:if> 块没有显示的话也是如此。因此,当我读到该块将捕获异常时,我用 <c:catch> 包围了我的块。所以我还在所有方法上添加了声明“抛出异常”。但是,再次,当不遵守关键条件时,我的页面将被重定向到 500 错误页面。
我发布了我的 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 错误页面。
提前谢谢你,塞缪尔