Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8434431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:39:11+00:00 2026-06-10T06:39:11+00:00

I’m trying to add JSF <h:commandButtons> dynamically to my webpage, and so far I

  • 0

I’m trying to add JSF <h:commandButtons> dynamically to my webpage, and so far I have them displaying, but I cannot set the action with parameters, like I could in a static page:
action="#{bean.function(parameter)}". (this is of course using EL-2.2)
Looking around I find that I have to create a MethodExpression, but this is obscure to me and I haven’t been able to find much information on this. If someone could shine a light through the fog and explain how this can be done, it would be greatly appreciated.

EDIT: so now I have this

public void displayNode( String childName ){
//lots of messy code instantiating JSF components

if( activeEmployee.getParent() != null ){
        HtmlCommandButton parent = new HtmlCommandButton();
        HtmlOutputText parentLabel = new HtmlOutputText();

        parentLabel.setId("label" + count++);  //I really hate having to use count
        parentLabel.setValue( "Parent: " );

        parent.setId("Parent" + count++); 
        String parentName = activeEmployee.getParent().getName();
        parent.setValue( parentName );
        MethodExpression expression = createMethodExpression("#{tree.displayNode('" + parentName + "')}",
                                                                null, String.class);
        parent.setActionExpression( expression );

        newDiv.getChildren().add( parentLabel );
        newDiv.getChildren().add( parent );
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:39:13+00:00Added an answer on June 10, 2026 at 6:39 am

    Use ExpressionFactory#createMethodExpression().

    public abstract MethodExpression createMethodExpression(
                                          ELContext context,
                                          java.lang.String expression,
                                          java.lang.Class<?> expectedReturnType,
                                          java.lang.Class<?>[] expectedParamTypes)
    

    Here’s a convenience method:

    public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext.getApplication().getExpressionFactory().createMethodExpression(
            facesContext.getELContext(), expression, returnType, parameterTypes);
    }
    

    The following action method examples:

    public void submit1()
    public String submit2()
    public void submit3(String argument)
    public String submit4(String argument)
    public void submit5(String argument1, Long argument2)
    public String submit6(Long argument1, String argument2)
    

    can then be created as follows:

    createMethodExpression("#{bean.submit1}", null);
    createMethodExpression("#{bean.submit2}", String.class);
    createMethodExpression("#{bean.submit3('foo')}", null, String.class);
    createMethodExpression("#{bean.submit4('foo')}", String.class, String.class);
    createMethodExpression("#{bean.submit5('foo', 0)}", null, String.class, Long.class);
    createMethodExpression("#{bean.submit6(0, 'foo')}", String.class, Long.class, String.class);
    

    Note that the EL expression is exactly the same as you would use in the normal view file.


    Update here’s an SSCCE which works fine for me with Mojarra 2.1.12 on Tomcat 7.0.27.

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
    >
        <h:head>
            <title>SO question 12098611</title>
        </h:head>
        <h:body>
            <h:form binding="#{bean.form}">
                <h:commandButton value="add" action="#{bean.add}" />
            </h:form>
        </h:body>
    </html>
    
    @ManagedBean
    @RequestScoped
    public class Bean {
    
        private UIForm form;
    
        public void add() {
            String id = "button" + form.getChildCount();
            UICommand button = new HtmlCommandButton();
            button.setId(id);
            button.setValue(id);
            button.setActionExpression(createMethodExpression(String.format("#{bean.submit('%s')}", id), null, String.class));
            form.getChildren().add(button);
        }
    
        public void submit(String arg) {
            System.out.println("submit: " + arg);
        }
    
        public UIForm getForm() {
            return form;
        }
    
        public void setForm(UIForm form) {
            this.form = form;
        }
    
        public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            return facesContext.getApplication().getExpressionFactory().createMethodExpression(
                facesContext.getELContext(), expression, returnType, parameterTypes);
        }
    
    }
    

    Unrelated to the concrete problem, all of above is a poor practice. See also How does the 'binding' attribute work in JSF? When and how should it be used?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.