0
  • I am building a MethodExpression directly via java code.
  • It will represents a call to a bean method with parameter like follow .
#{bean.method(TheObjectInstance)}
  • The object is a simple custom pojo object
public class TheObject
{
   public String value0 = "value0";
}
  • We now create the MethodExpression like follow.
    TheObject object = new TheObject();

    FacesContext context = FacesContext.getCurrentInstance();
    Application application = context.getApplication();
    ExpressionFactory factory = application.getExpressionFactory();

    //Create method expression
    MethodExpression methodExpression = factory.createMethodExpression(
       context.getELContext(), 
       "#{bean.method(" + object + ")}", 
       null, 
       new Class<?>[] {TheObject.class});
  • It generates the following error.
javax.servlet.ServletException: Encountered "@" at line 1, column 87.
Was expecting one of:
    "." ...
    "(" ...
    ")" ...
    "[" ...
    "," ...
    ";" ...
    ">" ...
    "gt" ...
    "<" ...
    "lt" ...
    ">=" ...
    "ge" ...
    "<=" ...
    "le" ...
    "==" ...
    "eq" ...
    "!=" ...
    "ne" ...
    "&&" ...
    "and" ...
    "||" ...
    "or" ...
    "*" ...
    "+" ...
    "-" ...
    "?" ...
    "/" ...
    "div" ...
    "%" ...
    "mod" ...
    "+=" ...
    "=" ...
  • I tried the same code with a String as parameter and a Boolean object and it works fine but using a custom Object is generating the same error, as well if we pass a complex object for instance a UIComponent.

  • I am using JSF 2.2, any helps is welcome.

fra
  • 83
  • 1
  • 11
  • 1
    Do `System.out.println(inputText)` and look at its result. You're attempting to pass exactly that. Why would it be the correct syntax for a component as parameter? Take a step back. Get a proof of concept ready in plain vanilla XHTML file first. It's so much easier to do define the component tree and behavior in XHTML+XML. Once you have figured out that, then you can safely rewrite the Java code in such way that it creates exactly the same expression format (or just stick to clean XHTML because this Java binding and all is pretty poor practice and makes the code harder to read and maintain). – BalusC May 14 '19 at 08:44
  • @BalusC Thank you for your answer. Actually, i just want a commandButton to take in parameter of actionListenner a html component to make it disappear on click. Something like this – fra May 14 '19 at 19:45
  • @BalusC in fact if i dont use Html its because its a special case i want to generate a custom generic datatable that adapt on the bean and the entity object we pass as parameter. so no choice but have to do generic java code rather than pure html. :( – fra May 14 '19 at 19:51
  • There's *nothing* which is impossible in XHTML and only possible in Java. – BalusC May 15 '19 at 10:15
  • @BalusC I have edited my question to simplify the problem and make understand where is located the exact problem. Have you encounter this type of problem when creating a MethodExpression for the method of a bean with an object as parameter like this "#{bean.method(myObject)}". – fra May 15 '19 at 19:31
  • 2
    Do `System.out.println(object)` and look at its result. You're attempting to pass exactly that. Why would it be the correct syntax for an object as parameter? Take a step back. Get a proof of concept ready in plain vanilla XHTML file first. It's so much easier to do define the component tree and behavior in XHTML+XML. Once you have figured out that, then you can safely rewrite the Java code in such way that it creates exactly the same expression format (or just stick to clean XHTML because this Java binding and all is pretty poor practice and makes the code harder to read and maintain). – BalusC May 16 '19 at 08:45
  • @BalusC thank you for your answer. You do make me realize there is a better way to achieve this but my question is not about what is the best practice. My question is clearly about passing an object as parameter of a Method expression like this #{bean.method(myObject)}. But i have the feeling that JAVA does not support this basic functionality am i right ? – fra May 16 '19 at 19:24
  • @BalusC i just tried again now passing a list as parameter of the MethodExpression and it works just fine why passing the Object itself won't as well ? the error generated is very confusing and does not gives any good idea on what is truly happening ? Now if it is not doable in JAVA i prefer to have a simple answer saying so, so at least i know. – fra May 16 '19 at 19:37
  • @BalusC also when using a Datatable you can use the "var=entity" as a parameter of a method called by a commandbutton actionalistenner such as "#bean.method(entity)}" the signature of the method in the bean is public void method(EntityType object). So doing it via a DataTable is ok but doing it manually by creating a MethodExpression via JAVA code is not ? – fra May 16 '19 at 19:43
  • 2
    In the data table example you would create the `MethodExpression` with `"#{bean.method(entity)}"` and not `"#{bean.method(" + entity + ")}"`, right? Because in normal XHTML file it's also really `entity` and not `com.example.Entity@12345678`. – BalusC May 17 '19 at 08:24
  • @BalusC thanks you. I understand by passing the object directly as a string the object is converted via the class toString method and result in giving back the object string signature com.example.Entity@12345678. This is why we are getting an error Encountered "@" at line 1 . If i want to make it works, i should use a var name as the parameter of our MethodExpression. Like in the case of a DataTable we declare a var=entity then we could use it in EL expression to represent the object. – fra May 17 '19 at 21:20
  • @BalusC a big thank you for being patient and trying to explain things. I agree with you this case could be simplified by using XHTML with some var in a datatable and during columns/rows generation. Thank you so much for your efforts. – fra May 17 '19 at 21:22

1 Answers1

0
  • To create a MethodExpression with a bean method containing an object as parameter #{bean.method(object)}, we should use the name of a var declared in the HTML page var=object.
    <h:form>
     <h:datatable var="object" value="#{bean.objects}">
      <h:commandbutton value="test" actionlistenner="#{bean.method(object)}"/>
    </h:datatable>
    </h:form>
  • If we want to generate the same MethodExpression #{bean.method(object)} we will have to generate the full html elements including a parent html element in our case a datatable containing the reference to the object var=object then in the code of the MethodExpression
    //Wrong implementation: the object is converted as object.getClass().toString()
    MethodExpression methodExpression = factory.createMethodExpression(
       context.getELContext(), 
       "#{bean.method(" + object + ")}", 
       null, 
       new Class<?>[] {TheObject.class});

    //Right implementation: we refer to object referenced by the datatable var. 
    MethodExpression methodExpression = factory.createMethodExpression(
       context.getELContext(), 
       "#{bean.method(object)}", 
       null, 
       new Class<?>[] {TheObject.class});
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
fra
  • 83
  • 1
  • 11