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

时间:2015-05-18 17:16:36

标签: jsf alert facelets

我有一个命令链接:

<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()最终用户无法删除该项目。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

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

E.g。

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" />

警报即1990年。