5

Using primeFaces I have the following button:

<p:commandButton value="Submit" action="#{createDeal.saveDeal}" update="myPanel" />

This works just fine. However I want to generate that button using java. I have the following code:

CommandButton submit = new CommandButton();
submit.setValue("Submit");
submit.setUpdate("myPanel");
FacesContext facesCtx = FacesContext.getCurrentInstance();
Application app = facesCtx.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesCtx.getELContext();
MethodExpression methodExpression =null;
methodExpression = elFactory.createMethodExpression(elContext,"#{createDeal.saveDeal}",String.class, new Class[]{});
submit.addActionListener(new MethodExpressionActionListener(methodExpression));
submit.setActionExpression(methodExpression);
createButtons.getChildren().add(submit);

When I click submit, my form validates (which is done using the setRequired function on the input), but the form never hits my createDeal class. What am I doing wrong that the inline button works, but the java generated one does not.

One note. The button created inline using primefaces, is there on page load. The button attempted to be added with java is not done until after an Ajax call is made to generate both the form AND button.

Any assistance would be helpful.

Thanks.

1 Answer 1

2

Thanks to BalusC for all your help. I am still not sure how I missed this! The following works:

CommandButton submit = new CommandButton();
submit.setValue("Submit");
submit.setUpdate("myPanel");
submit.setId("create"+panelClass);
FacesContext facesCtx = FacesContext.getCurrentInstance();
ELContext elContext = facesCtx.getELContext();
Application app = facesCtx.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
MethodExpression methodExpression =null;
methodExpression = elFactory.createMethodExpression(elContext,"#    {createDeal.saveDeal}",null, new Class[]{});
submit.setActionExpression(methodExpression);
createButtons.getChildren().add(submit);

The null pointer exception was unrelated. It looks like simply adding the ID and removing the ActionListener did the trick. Much obliged!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.