0

I want to display name (current user) in a drop down list in JSF. Here name is a dynamic variable that gets populated though some pojo class. The present code that I have is

<f:selectItem itemLabel="#{accessManager.salesManager.displayName} #{' ('.concat(i.m['current user']).concat(') ')}" itemValue="#{accessManager.salesManager.oid}" />

accessManager.salesManager.displayName populates the name on the UI.

#{' ('.concat(i.m['current user']).concat(') ')}" here I am trying to hard code (current user).

But this is throwing exceptions.

Can any one help me in this? It sounds to be a very simple query but I am not used to EL.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3198773
  • 1
  • 1
  • 1
  • What do you exactly want to display? – Aritz Jan 15 '14 at 15:36
  • Lest accessManager.salesManager.displayName evaluates to "Bob", then I want to display Bob (current user) in the drop down list. – user3198773 Jan 15 '14 at 15:38
  • It is to be noted that "current user" is language specific. the values of "current user" in different languages is maintained in a properties file. – user3198773 Jan 15 '14 at 15:40

3 Answers3

0

Based on your example, the easiest way is to provide an extra function in your managed bean which evaluates if the current user is the current choice or not. Later on, you evaluate that function with a ternary operator, which will decide wether to display the bundled (translated) value or not.

@ViewScoped
public class ChoiceBean{

    public List<SalesManager> getSalesManagers(){
        return salesManagers;
    }

    //Checks if your current choice is the current manager(EL 2.2 required to pass the parameter)
    public boolean checkIfCurrent(SalesManager manager){
        if (manager.getName().equals("Bob")) return true;
        return false;
    }

}
<f:selectItem 
    itemLabel="#{manager.name} #{choiceBean.checkIfCurrent(manager) ? i.m['current user'] : ''}" itemValue="#{salesManager.oid}" />

See also:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • I am sorry, I think I was not able to put my question correctly. The above code given by Xtreme will display Bob current user. I want to display Bob (current user). So the question is how do I concat '(' & ')' before and after #{i.m['current user'])}. – user3198773 Jan 15 '14 at 15:50
0

Another solution is to use a function doing this String concatenation for you. Either

  • you use the JSTL functions library with fn:join (see enter link description here) or
  • implement your own static method in EL, and use that one. The method itself would (surprisingly) look like

    public static String concat(String string1, String string2) {
       return string1.concat(string2);
    }
    

    and the call in JSF, nested like #{fn:concat('(',fn:concat(i.m['current user'],')'))}.

But, out of curiosity, why don't you want to add the brackets into the properties file, so the ressource-value holds (current user) instead of current user?

Hope it helps...

Community
  • 1
  • 1
L-Ray
  • 1,637
  • 1
  • 16
  • 29
  • No in the properties file its just current user. In .xhtml we are appending '(' and ')' to current user. – user3198773 Jan 15 '14 at 15:59
  • I know... so I guess, you can't change the ressource bundles on your own and just introduce a second, a "bracket"-version? If so, go with the static concatenation-method explained above and via the link. It works for us and is not as much pain as it looks like on the first view. – L-Ray Jan 15 '14 at 16:01
  • I could have changed the resource bundle to include (current user) =some text in other language. But programmatically it not have been correct. – user3198773 Jan 15 '14 at 16:37
0

So the question is how do I concat '(' & ')' before and after #{i.m['current user'])}

Don't do it the hard way. Just put those parentheses outside the EL expression.

<f:selectItem itemLabel="#{accessManager.salesManager.displayName} (#{i.m['current user']})" itemValue="#{accessManager.salesManager.oid}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • From what @user3198773 says, I understand *(current user)* text should only appear when the current selectItem matches the current (logged??) user. So the `(#{i.m['current user']})` should be conditionally rendered (as I deduce). – Aritz Jan 16 '14 at 08:10
  • @Xtreme: Where did he say that? His initial attempt also doesn't suggest this in any way. – BalusC Jan 16 '14 at 08:16
  • Look at the discussion I had in comments below the question. Maybe I misunderstood it, but it would be strange to have all of them notated with *(current user)* wouldn't it? Anyway, the question itself needs a face washing. – Aritz Jan 16 '14 at 08:29
  • @Xtreme: Hmm, the functional requirement is after all indeed weird. In any case, the above answer still fixes the technical matter. – BalusC Jan 16 '14 at 08:30