c:set for bean properties

DominikAngerer picture DominikAngerer · Aug 7, 2012 · Viewed 21k times · Source

I'm looking for some piece of code for setting a property in a JSF managed bean. My first idea was something like that:

<c:set var="#{loginBean.device}" value="mobil"></c:set>

That means I want to set the attribute device to the value "mobil" without a button have to been clicked.

Answer

Ingo picture Ingo · Aug 7, 2012

Yes, you can use c:set for this purpose.

<c:set value="mobil" target="#{loginBean}" property="device" />

Doc: http://docs.oracle.com/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/pdldocs/facelets/c/set.html

However, setting a static value rarely makes sense. You might consider to set a default value directly in your managed bean class. Also in terms of maintainability since you can handle constants better in the Java code than in the view layer.