EL Expression parsing integer as long

2019-07-21 17:01发布

问题:

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 :(

回答1:

The method expression type for action is

String action()

So use

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

See also:

  • Passing parameter to JSF action

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">


回答2:

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())}"/>


回答3:

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...
}


回答4:

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());
}