0

This is my sample code

CommandButton save = new CommandButton();
        save.setValue("Save");
        save.setActionExpression(createActionExpression("#{controller.saveData}", String.class));



private MethodExpression createActionExpression(String actionExpression, Class<?> returnType) {

     FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext.getApplication().getExpressionFactory().createMethodExpression(
            facesContext.getELContext(), actionExpression, returnType, new Class[0]);
}

public void saveData(){

  }

My question is how to pass parameter to this function like ?

save.setActionExpression(createActionExpression("#{controller.saveData('str')}", String.class));


 public void saveData(String str){

  }

I have this error

javax.el.ELException: Error Parsing: #{controller.saveData('str')}

Many Thanks

Developer
  • 1
  • 1
  • How exactly does your attempt fail? – BalusC Dec 31 '16 at 10:25
  • first case without parameters is working ok no problem but when adding parameters it does not work – Developer Dec 31 '16 at 10:28
  • 1
    I didn't ask that. How exactly does it "not work"? Describe "does not work" elaborately. What exactly happens instead? You're acting like your car has a flat tire and you call a car mechanic and you only say "my car does not work". This is just unhelpful in order to diagnose the root cause of the problem. – BalusC Dec 31 '16 at 10:30
  • ok this exception during load com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on managed bean – Developer Dec 31 '16 at 10:41
  • at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257) – Developer Dec 31 '16 at 10:42
  • My exact question is how to call function with parameters using MethodExpression ?? – Developer Dec 31 '16 at 10:44
  • *"ManagedBeanCreationException: An error occurred performing resource injection on managed bean"* Finally something useful. It appears that you're not only new to JSF, but also to basic Java and can't read Java's exception stack traces yet. You need to look at the bottomost "Caused By" part of the stack trace. That part explains the root cause of your problem. See also a.o. http://stackoverflow.com/q/6192320 – BalusC Dec 31 '16 at 11:40
  • *"My exact question is how to call function with parameters using MethodExpression"* There's nothing wrong with your attempt as shown in the question, provided that you're using up to date software and can do exactly the same in plain XHTML/JSP instead of Java, so something else is at matter and that can only be understood by carefully investigating the unexpected behavior. – BalusC Dec 31 '16 at 11:41
  • Thanks for help but i think that you did not see my update in the question getting this error – Developer Dec 31 '16 at 12:03
  • javax.el.ELException: Error Parsing: #{controller.saveData('str')} – Developer Dec 31 '16 at 12:03
  • so if i did the right why such error is caused – Developer Dec 31 '16 at 12:04
  • Are you implying that it works just fine in XHTML or JSP or whatever view technology you're using? It's important to realize that there's basically **nothing** which is impossible in XHTML/JSP and only possible in verbose and brittle Java code this way. – BalusC Dec 31 '16 at 12:06

0 Answers0