2

I would like to call a setter directly from a command button and pass a value. My problem is that the setter is expecting a Character and jsf if passing it back as a String. Is there a good way to 'fix' this on the front end instead of having to over load the setter on my backing bean?

commandButton:

<p:commandButton value="SignOff"
    actionListener="#{manageItemHandler.dataEntryOp.setBomComplete('Y')}"
    rendered="#{speed2Session.isRendered('editManageItemOp')}"/>

getter/setter from backing bean:

protected Character bomComplete;

/**
 * @return the bomComplete
 */
public Character getBomComplete() {
    return bomComplete;
}
/**
 * @param bomComplete the bomComplete to set
 */
public void setBomComplete(Character bomComplete) {
    this.bomComplete = bomComplete;
}

When I click the commandbutton I get

11:47:19,270 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-steves-172.16.8.26-15081-1) JSF1073: javax.faces.event.AbortProcessingException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=centerForm:j_idt271, Message=Method not found: data.operation.OperationData@595025a.setBomComplete(java.lang.String)
11:47:19,273 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-steves-172.16.8.26-15081-1) Method not found: data.operation.OperationData@595025a.setBomComplete(java.lang.String): javax.faces.event.AbortProcessingException: Method not found: data.operation.OperationData@595025a.setBomComplete(java.lang.String)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
SteveS
  • 1,008
  • 3
  • 18
  • 32

2 Answers2

3

This is unfortunately by design. Everything in quotes is in EL treated as String. A workaround would be to pass String#charAt() instead.

#{manageItemHandler.dataEntryOp.setBomComplete('Y'.charAt(0))}

This is only ugly. An alternative is to pass its int codepoint instead, which is 89 for Y.

#{manageItemHandler.dataEntryOp.setBomComplete(89)}

But this is not exactly self-documenting. Much better is to just make use of enums.

public enum Choice {
    Y, N;
}

with

protected Choice bomComplete;

which you can just invoke the desired way

#{manageItemHandler.dataEntryOp.setBomComplete('Y')}

The string 'Y' will be automatically converted to that enum. As a bonus, enums have many more additional advantages, such as compile time type safety.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for your (as usual) thorough explanation of the behavior and giving multiple possible solutions. – SteveS Oct 22 '12 at 18:02
1

I have tried to use simple quotes directly without using charAt(0) tips ...

and this work correctly when parameter type is char or Character.

Example passing a character:

#{manageItemHandler.dataEntryOp.setBomComplete('Y')}

Example passing a String literal and a character constant:

<p:panel style="border:none;#{vC.traceUpdate('p:panel','X')}">

The code of traceUpdate() java method is following:

@ManagedBean(name = "vC")
@ViewScoped
public class ActionViewController
extends AbstractViewController
    {
    public String traceUpdate(String s, Character c)
        {
        System.out.println("s:" + s + " " + c);
        return "";
        }

OR

@ManagedBean(name = "vC")
@ViewScoped
public class ActionViewController
extends AbstractViewController
    {
    public String traceUpdate(String s, char c)
        {
        System.out.println("s:" + s + " " + c);
        return "";
        }

where Character as been replace by char

This example run on Glassfish 4.1 using Primefaces 6.2.4.

schlebe
  • 3,387
  • 5
  • 37
  • 50
  • 1
    Note that `#{...}` things are not part of JSF. They are part of EL. So mentioning JSF version is irrelevant. More interesting is the EL version and also EL implementation. Browser is completely irrelevant as Java/JSF/EL/etc runs on server not on client. – BalusC Aug 31 '18 at 11:28
  • And please, for completeness, add the backing bean method for this – Kukeltje Aug 31 '18 at 11:48
  • @BalusC: How can I found EL version that I use ? – schlebe Aug 31 '18 at 12:44
  • It's usually tight coupled to the server make/version (e.g. "WildFly 14.0.0.Final", we can find in its modules subfolder that this ships by default with an Oracle EL 3.0.1 JAR file), unless you have overridden the EL implementation from the webapp side on (you most likely didn't, else you'd have known that). – BalusC Aug 31 '18 at 13:01
  • @BalusC, I have given some precision about Glassfish and Primefaces version instead of JSF and browser :-) – schlebe Aug 31 '18 at 13:05
  • 1
    GlassFish 4.1 ships by default with Oracle EL 3.0.1 as well. PrimeFaces has no influence on EL. – BalusC Aug 31 '18 at 13:19