Call a function of a bean with a parameter

Hi,

How can I call a function of a bean with a parameter? I have a list with many letters. And some letters are already in use and should be disabled. But how can I call the test function of my bean?

My test: <c:when test="${word.letterInUse(letter)}">. But this doesn't work. Error message: "The function letterInUse must be used with a prefix when a default namespace is not specified". Unfortunately this error message doesn't help me really.

<h:panelGrid columns="10" styleClass="letters">

<c:forEach items="#{word.allowedLetters}" var="letter">

<c:choose>

<c:when test="${word.letterInUse(letter)}">

<h:outputLabel value="#{letter}" styleClass="disabled" />

</c:when>

<c:otherwise>

<h:outputLabel value="#{letter}" styleClass="enabled" />

</c:otherwise>

</c:choose>

</c:forEach>

</h:panelGrid>

publicclass Word{

private List<Character> mAllowedLetters;

private List<Character> mUsedLetters;

public List<Character> getAllowedLetters(){

return mAllowedLetters;

}

publicboolean letterInUse(char letter){

return mUsedLetters.contains(letter);

}

}

Regards

Martin

[2039 byte] By [mwinandya] at [2007-11-27 6:18:41]
# 1

First of all, throw that c:choose away and rather use the 'rendered' property of the JSF component.

It accepts any boolean expression. When it evaluates to 'false' then the component will be skipped in rendering the response.

Here are some basic examples:

<h:someComponent rendered="#{myBean.booleanValue}" />

<h:someComponent rendered="#{myBean.intValue > 10}" />

<h:someComponent rendered="#{myBean.objectValue == null}" />

<h:someComponent rendered="#{myBean.stringValue != 'someValue'}" />

<h:someComponent rendered="#{!empty myBean.collectionValue}" />

<h:someComponent rendered="#{!myBean.booleanValue && myBean.intValue != 0}" />

<h:someComponent rendered="#{myBean.stringValue == 'oneValue' || myBean.stringValue == 'anotherValue'}" />

Second, passing parameters from JSF to backing beans is described in http://balusc.xs4all.nl/srv/dev-jep-com.html

BalusCa at 2007-7-12 17:32:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hello BalusC,

thanks for you answer. You are right, I can simplify my code with boolean expressions. I don't use the rendered attribute, because I want to change the font color to grey instead of hiding the disabled letters.

<c:forEach items="#{word.allowedLetters}" var="letter">

<h:outputLabel value="#{letter}" styleClass="#{word.letterInUse(letter) ? 'disabled' : 'enabled'}" />

</c:forEach>

> Second, passing parameters from JSF to backing beans is described in http://balusc.xs4all.nl/srv/dev-jep-com.html

Unfortunately I couldn't find a solution in http://balusc.xs4all.nl/srv/dev-jep-com.html. The examples are for Action Listeners and Action Events. But I do not have one of these both cases. Or have I overlooked anything?

Regards

Martin

mwinandya at 2007-7-12 17:32:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Checkout the 'Passing component attributes' section.And rename public boolean letterInUse() to public boolean isLetterInUse(). This method won't be recognized in EL otherwise.
BalusCa at 2007-7-12 17:32:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

There is a problem with the binding. While loading I get a NotSerializableException for HtmlOutputLabel. I think the problem ist, that I create my Output Labels in a loop. So the binding cannot work.

Why is passing component attributes from JSF to backing beans so (unnecessary) difficult :(

public class Word {

private HtmlOutputLabel mOutput;

public void setOutput(HtmlOutputLabel mOutput) {

this.mOutput = mOutput;

}

public HtmlOutputLabel getOutput() {

return mOutput;

}

}

<c:forEach items="#{word.allowedLetters}" var="letter">

<h:outputLabel binding="#{word.output}" value="#{letter}" />

</c:forEach>

mwinandya at 2007-7-12 17:32:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
A very elegant solution is to override a Map in your backin bean.Then you can pass parameters like bla['param'].I can send you an example if you're interested.best regards,flo
Flow86a at 2007-7-12 17:32:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Hi,I will love to see an example
chicoa at 2007-7-12 17:32:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Hi!

Write a Managed Bean which looks like:

public class ParamBean {

private SpecialMap map = new SpecialMap();

public class SpecialMap extends HashMap<String, Boolean> {

public Boolean get(final Object param) {

...

return true;

}

}

public SpecialMap getMap() {

return map;

}

}

In your JSF page you can use it like

#{paramBean.map['param']}

In the overriden get() method you can perform your business logic and return a value. (In this case Boolean, but any other type may be used)

Have Fun!

Best regards,

flo

Flow86a at 2007-7-12 17:32:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
I agree with Flo that maps are a good solution. Because I didn't find a way to call my function with a parameter in a loop, I have implement a similar solution. But I'm not 100% happy with the map solution in my case.
mwinandya at 2007-7-12 17:32:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...