EL Expression parsing integer as long
Asked Answered
S

4

3

I'm using JSF 2.0 with primefaces over JBoss 7. In some part of the code, I have the following:

public void setItemValue(int value) {
    this.value = value;
}

and in the xhtml:

<p:commandButton ajax="true" value="Button" update="@form" 
action="#{bean.setItemValue(1)}"/>

The problem is, when I click the button, I get an javax.el.MethodNotFoundException, saying that setItemValue(java.lang.Long) doesn't exists. Off course it doesn't, it should be a int or Integer value! Anyone has seen this problem? there is any alternative other than changing my method to receive a long? Thanks!

EDIT: Just downloaded the SNAPSHOT of JBoss 7.2, and it works fine on it. Looks like its a bug of JBoss 7.1.1 :(

Saccular answered 2/7, 2012 at 19:25 Comment(3)
Try with f:setPropertyActionListener or #{bean.setItemValue('1')}Wrack
Second try: Method not found: bean.setItemValue(java.lang.String). But the first try worked! The bad part is that I'm still not able to pass an Integer or int to a method through jsf, but this is a nice workaround :)Saccular
Same with Tomcat 7.0.26. Switching to Tomcat 7.0.28 fixed it for me. I do think that literals in EL are indeed Long but I like the fact that the method with Integer is found as matching in Tomcat 7.0.28.Ashlieashlin
B
1

The method expression type for action is

String action()

So use

 public String setItemValue(Integer value) {
    this.value = value;
    return null;
}

See also:

UPDATE You need to declare the Servlet version as 3.0 to take full advantage of the EL 2.2 such as passing the parameter. For that change your web-app element in your web.xml to this:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"  
 version="3.0">
Bolzano answered 3/7, 2012 at 0:31 Comment(3)
If you return null, then the current page will be reloaded. If you have one then just return an outcome view.Bolzano
Thanks for the explanation about the action method signature! Unfortunately, the error persists :( Method not found: bean.setItemValue(java.lang.Long)Saccular
Hey Ravi, sorry for taking too long. I already use Servlet 3.0, I can pass the parameter OK, but it goes as Long, instead of Integer, that I was expecting. I downloaded JBoss 7.2 SNAPSHOT, and it went well... maybe it is a bug of JBoss 7.1.1Saccular
N
4

It looks a bit strange, but you can call the method intValue on the Long object self inside EL 2.2

<p:commandButton ... action="#{bean.setItemValue((1).intValue())}"/>
Norri answered 16/8, 2013 at 8:37 Comment(2)
Property 'intValue' not found on type java.lang.LongPsychrometer
Did you add the brackets to intValue? #{bean.myLongValue.intValue()}?Norri
B
1

The method expression type for action is

String action()

So use

 public String setItemValue(Integer value) {
    this.value = value;
    return null;
}

See also:

UPDATE You need to declare the Servlet version as 3.0 to take full advantage of the EL 2.2 such as passing the parameter. For that change your web-app element in your web.xml to this:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"  
 version="3.0">
Bolzano answered 3/7, 2012 at 0:31 Comment(3)
If you return null, then the current page will be reloaded. If you have one then just return an outcome view.Bolzano
Thanks for the explanation about the action method signature! Unfortunately, the error persists :( Method not found: bean.setItemValue(java.lang.Long)Saccular
Hey Ravi, sorry for taking too long. I already use Servlet 3.0, I can pass the parameter OK, but it goes as Long, instead of Integer, that I was expecting. I downloaded JBoss 7.2 SNAPSHOT, and it went well... maybe it is a bug of JBoss 7.1.1Saccular
P
1

Don't use get or set prefix in any bean methods (Its a really bad practice) , action attribute expects a method name rather than some getter or setter

get and set are used only for getters and setters of your bean variables

Better replace your setItemValue with something like assignItemValue

like this:

<p:commandButton ajax="true" value="Button" update="@form" 
    action="#{bean.assignItemValue(1)}"/>

where

public void assignItemValue(Long value) { //you could also try with int value...
   //set the value to whenever you want too...
}
Pyralid answered 3/7, 2012 at 5:22 Comment(3)
same error: Method not found: bean.assignItemValue(java.lang.Long)Saccular
Why don´t you change the method argument to Long?Ashlieashlin
Why should I be forced to use Long, since the default for Java is Integer?Saccular
B
0

Apologies for resurrecting this ancient thread. If you are still using Jboss 7.11 or hit similar issues and don't want to go the EL (1).intValue() route, you can also jippo your way around it in your managed bean as follows :-

public String setItemValue(Long longVal) {
    return setItemValue(longVal.intValue());
}
Bijugate answered 4/3, 2016 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.