p:commandButton vs. h:commandButton

Like @Partlov wrote in the comments below the question, Main difference is that p:commandButton is AJAX by default, and h:commandButton is non-AJAX by default. So <p:commandButton … /> is more like <h:commandButton …> <f:ajax/> </h:commandButton> but <p:commandButton …> <p:ajax/> </p:commandButton> is wrong and leads to undefined behaviour or the other way around <h:commandButton … /> … Read more

UISelectMany on a List causes java.lang.ClassCastException: java.lang.String cannot be cast to T

Your problem is caused by the following facts: Java generics are compiletime syntactic sugar and completely absent during runtime. EL expressions runs during runtime and not during compiletime. HTTP request parameters are obtained as Strings. Logical consequence is: EL doesn’t see any generic type information. EL doesn’t see a List<Long>, but a List. So, when … Read more

java.lang.IllegalStateException at com.sun.faces.context.FacesContextImpl.assertNotReleased

Your mistake is here: @ManagedBean @ViewScoped public class Login { private FacesContext fCtx; public Login() { fCtx = FacesContext.getCurrentInstance(); } } You should never assign FacesContext as instance variable of a view/session/application scoped managed bean and reuse it in different requests, because the current instance of the FacesContext is tied to the current request which … Read more

Are @ManagedBeans obsolete in JavaEE6 because of @Named in CDI/Weld?

In short, @ManagedBean makes sense for applications that use JSF but do not use JSR 299 (whatever the reason is). Below a longer explanation from Gavin King: Re: Comparisons to @ManagedBean annotations in JSF2?: While looking through the Weld examples, and the older WebBeans documentation, it looks like a competitor to the new @ManagedBean JSF … Read more

How to concatenate Strings in EL expression?

If you’re already on EL 3.0 (Java EE 7; WildFly, Tomcat 8, GlassFish 4, etc), then you could use the new += operator for this: <h:commandButton … action=”#{someController.doSomething(id += ‘SomeTableId’)}” /> If you’re however not on EL 3.0 yet, and the left hand is a genuine java.lang.String instance (and thus not e.g. java.lang.Long), then use … Read more

How do UISelectOne and UISelectMany components preselect defaults in f:selectItems

It uses Object#equals() for that. You can change (fix) this behavior by implementing it accordingly on your entity. private Long id; @Override public boolean equals(Object other) { return (other != null && getClass() == other.getClass() && id != null) ? id.equals(getClass().cast(other).id) : (other == this); } Don’t forget the hashCode() to satisfy the equals-hashCode contract. … Read more

How and when should I load the model from database for JSF dataTable

Do it in bean’s @PostConstruct method. @ManagedBean @RequestScoped public class Bean { private List<Item> items; @EJB private ItemService itemService; @PostConstruct public void init() { items = itemService.list(); } public List<Item> getItems() { return items; } } And let the value reference the property (not method!). <h:dataTable value=”#{bean.items}” var=”item”> In the @PostConstruct you have the advantage … Read more

How to use to iterate over a nested list?

What you need is to nest another <ui:repeat> tag in your outer iteration: <ui:repeat value=”#{bean.listOfA}” var=”a”> … <ui:repeat value=”#{a.listOfB}” var=”b”> … </ui:repeat> </ui:repeat> The only thing left that is worth noting is that nested <ui:repeat> tags used to have problems with state management until Mojarra 2.1.15 version (details in jsf listener not called inside nested … Read more