执行托管 bean 操作方法后显示 alert()

Show alert() after executing managed bean action method

我有一个命令link:

<h:commandLink value="Delete"
    onclick="return confirm('Are you sure?');" 
    action="#{bean.deleteDestination(destination, '/destination/show')}" />

它调用这个托管 bean 操作方法:

public String deleteDestination(Destination selected, String action) {
    List<Flight> flights = getEjbFlightFacade().findFlightWithDestination(selected);

    if (flights.isEmpty()) {
        getEjbDestinationFacade().remove(selected);
        setDestinations(getEjbDestinationFacade().findAll());
    }
    else {
        // Here need to show an alert() that user can't remove the item.
    }

    return action;
}

如评论所示,我想显示一个 alert() 最终用户无法删除该项目。我怎样才能做到这一点?

让 JSF 根据 bean 有条件地呈现所需的脚本 属性。

例如

this.undeleteable = true;
<h:outputScript rendered="#{bean.undeleteable}">
    alert("You can't delete it.");
</h:outputScript>

然而,规范的方法是仅显示(全局)面孔消息。

FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("You can't delete it."));
<h:messages globalOnly="true" />

警报是 soo 1990。