2015-05-18 22 views
3

后我有一个命令链接:显示警报()执行管理的bean的操作方法

<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; 
} 

正如评论所指出的,我d喜欢显示alert()最终用户无法删除该项目。我怎样才能做到这一点?

回答

3

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

E.g.

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

的正规途径,但是,是只显示一个(全球)faces消息。

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

警报是即1990年洙