JSF render contents of attribute dynamically [duplicate] - jsf

I need to create a callback for <h:commandButton> while as a parameter I need to pass an argument that is string-concatenated with an external parameter id:
I tried nesting an EL expression something like this:
<h:commandButton ... action="#{someController.doSomething('#{id}SomeTableId')}" />
However this failed with an EL exception. What is a right syntax/approach to do this?

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 EL 2.2 capability of invoking direct methods with arguments, which you then apply on String#concat():
<h:commandButton ... action="#{someController.doSomething(id.concat('SomeTableId'))}" />
Or if you're not on EL 2.2 yet, then use JSTL <c:set> to create a new EL variable with the concatenated values just inlined in value:
<c:set var="tableId" value="#{id}SomeTableId" />
<h:commandButton ... action="#{someController.doSomething(tableId)}" />
See also:
String concatenation in EL for dynamic ResourceBundle key

Related

jsf el expression to build methodname

Is something like that possibile to build the MethodName with a variable value?
<c:forEach value="#{db.pojo.classList}" var="v">
<c:forEach items="#{db.pojo.methodNamesList}" var="o">
<c:set var="superman" value="#{o}" />
#{v.[superman]}
</c:forEach>
</c:forEach >
... database.entity.list is a List of Generic classes.
List<?> list..
I can use the generic class if i know the Methodname.
<c:forEach value="#{db.pojo.classList}" var="v">
#{v.value}
</c:forEach >
#{v.[superman]}
Punch that period.
#{v[superman]}
That <c:set> is unnecessary, by the way.
#{v[o]}
I would only use more self-documenting variable names though. E.g.
#{bean[property]}
#{entity[field]}
See also:
Our EL wiki page

Appending Strings in global resource bundle [duplicate]

I need to create a callback for <h:commandButton> while as a parameter I need to pass an argument that is string-concatenated with an external parameter id:
I tried nesting an EL expression something like this:
<h:commandButton ... action="#{someController.doSomething('#{id}SomeTableId')}" />
However this failed with an EL exception. What is a right syntax/approach to do this?
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 EL 2.2 capability of invoking direct methods with arguments, which you then apply on String#concat():
<h:commandButton ... action="#{someController.doSomething(id.concat('SomeTableId'))}" />
Or if you're not on EL 2.2 yet, then use JSTL <c:set> to create a new EL variable with the concatenated values just inlined in value:
<c:set var="tableId" value="#{id}SomeTableId" />
<h:commandButton ... action="#{someController.doSomething(tableId)}" />
See also:
String concatenation in EL for dynamic ResourceBundle key

JSF2 variable element of bundle [duplicate]

I need to create a callback for <h:commandButton> while as a parameter I need to pass an argument that is string-concatenated with an external parameter id:
I tried nesting an EL expression something like this:
<h:commandButton ... action="#{someController.doSomething('#{id}SomeTableId')}" />
However this failed with an EL exception. What is a right syntax/approach to do this?
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 EL 2.2 capability of invoking direct methods with arguments, which you then apply on String#concat():
<h:commandButton ... action="#{someController.doSomething(id.concat('SomeTableId'))}" />
Or if you're not on EL 2.2 yet, then use JSTL <c:set> to create a new EL variable with the concatenated values just inlined in value:
<c:set var="tableId" value="#{id}SomeTableId" />
<h:commandButton ... action="#{someController.doSomething(tableId)}" />
See also:
String concatenation in EL for dynamic ResourceBundle key

JSF: Ternary operator used to assign a value to an <f:param> fails

I am using JSF tags within an XHTML file. My intention is to enable or disable a <rich:MenuItem> context-menu item by setting the "disabled" attribute to "true" or "false" appropriately. To do this, I am using a backing bean variable in a ternary operator and setting an <f:param> value to either "true" or "false" based on the bean variable, as below:
<rich:componentControl event="oncontextmenu" for="network-tree-menu"
operation="show">
<f:param id="nestlevel" value="#{item.nestLevel > 10 ? 'true' : 'false'}"
name="nestlevel" />
</rich:componentControl>
where item is the backing bean and item.nestLevel is an integer.
I am using this <f:param> value later in the XHTML file as below:
<rich:contextMenu ...
<rich:menuItem id="abc" ajaxSingle="true" disabled="{nestlevel}"
onclick="doSomething();" value="Do something...">
</rich:contextMenu>
This is not working !! The menu item is always enabled, (I guess this is the default behaviour) even though the result of the ternary operation is "true". Is there something I am missing here w.r.t the syntax, or is there some other way I can do this conditional enabling of context-menu items within the XHTML file?
Thanks in advance.
Regards,
Nagendra U M
How about this:
<f:param id="nestlevel" value="#{item.nestLevel > 10}" name="nestlevel" />

In JSF what is the shortest way to output List<SomeObj> as comma separated list of "name" properties of SomeObj

I have a question about outputing a list of objects as a comma separated list in JSF.
Let's say:
public class SomeObj {
private String name;
... constructors, getters and setters ...
}
and List<SomeObj>:
List<SomeObj> lst = new ArrayList<SomeObj>();
lst.add(new SomeObj("NameA"));
lst.add(new SomeObj("NameB"));
lst.add(new SomeObj("NameC"));
to output it as a listbox I can use this code:
<h:selectManyListbox id="id1"
value="#{listHolder.selectedList}">
<s:selectItems value="#{listHolder.lst}"
var="someObj"
label="#{someObj.name}"/>
<s:convertEntity />
</h:selectManyListbox>
But what is the easiest way to output the list as is, comma seperated ? Like this:
NameA, NameB, NameC
Should I use JSTL <c:forEach/> or may be the <s:selectItems/> tag can also be used ?
Given a List<Person> persons where Person has a name property,
If you're already on Java EE 7 with EL 3.0, then use EL stream API.
#{bean.persons.stream().map(p -> p.name).reduce((p1, p2) -> p1 += ', ' += p2).get()}
If you're not on EL 3.0 yet, but have JSF 2.x at hands, then use Facelets <ui:repeat>.
<ui:repeat value="#{bean.persons}" var="person" varStatus="loop">
#{person.name}#{not loop.last ? ', ' : ''}
</ui:repeat>
Or if you're still on jurassic JSP, use JSTL <c:forEach>.
<c:forEach items="#{bean.persons}" var="person" varStatus="loop">
${person.name}${not loop.last ? ', ' : ''}
</c:forEach>
See also:
How iterate over List<T> and render each item in JSF Facelets
JSTL in JSF2 Facelets... makes sense?
use <ui:repeat> (from facelets). It's similar to c:forEach
Or pre-compute the comma-separated string in the managed bean, and obtain it via a getter.
If you can't use varStatus because you're stuck with using JSF 1.2, you can do:
<ui:repeat value="#{listHolder.lst}" var="someObj">#{someObj != listHolder.lst[0] ? ',' : ''}
#{someObj.name}</ui:repeat>
The absence of whitespace around the EL-expressions is deliberate, we don't want a space to appear there in the rendered HTML.

Resources