Show warning () after executing managed bean action method
I have a command link:
<h:commandLink value="Delete"
onclick="return confirm('Are you sure?');"
action="#{bean.deleteDestination(destination, '/destination/show')}" />
It calls this managed bean action method:
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;
}
As stated in the comment, I would like to show alert()
that the end user cannot delete the item. How can I achieve this?
+3
source to share
1 answer
Let JSF conditionally render the script you want based on the bean property.
eg.
this.undeleteable = true;
<h:outputScript rendered="#{bean.undeleteable}">
alert("You can't delete it.");
</h:outputScript>
The canonical way, however, is to simply show the (global) entities' message.
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("You can't delete it."));
<h:messages globalOnly="true" />
The alerts date back to 1990.
+3
source to share