Enterprise Java

JSF: Passing parameters to JSF action methods from page directly, a nice feature of JavaEE 6+

One of the JSF 2+ nice features presented in Java enterprise edition JavaEE 6+, is that you can pass parameters to any action components action method like commandButton or commandLink components.


Based on that you can minimize the number of methods inside your managed bean.

In addition, to minimize many parameters to set inside the bean to be used by action to decide navigation logic, which reduce memory consumption if your beans in scopes greater than request scope.

How it works:

  1. Open your favorite IDE, I am going to use Netbeans 7.4.1. (Use any IDE that supports JavaEE 6 or 7).
  2. Create web project.
    File –> New Project –> Java Web (left pan) –> Web Application (right pan) –> Next.
  3. Name it whatever you like (JSFeatures for me)–> Next –> Server Glassfish4 –> Java EE 7 Web profile –> Context path “/JSFeatures” –> Next.

    JSF-FEAT-01

  4. From frameworks chose “Java Server Faces–> Finish.

    JSF-FEAT-02

  5. You should have project structure like this:

    JSF-FEAT-03

  6. Right click on the JSFeatures project –> new “JSF Managed Bean“. and its name and config like this:

    JSF-FEAT-04

  7. With the bean opened in the editor page, copy and paste the following code after package statement:
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
     
    /**
     * @author mohamed_taman
     */
    @ManagedBean(name = "jSFeatBean")
    @RequestScoped
    public class JSFeatursBean {
     
        private String result;
     
        public String getResult() {
            return result;
        }
     
        public void setResult(String result) {
            this.result = result;
        }
     
        public JSFeatursBean() {
        }
     
        public Object addNumbers(int num1,int num2) {
                setResult("Hi there I am an action method to add numbers.");
            return null;
        }
    }
  8. Open the index.xhtml which is generated by default, and copy the following code and past it in your file:
    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html">
        <h:head>
            <title>Facelet Title</title>
        </h:head>
        <h:body>
            Hello from Facelets
            <h:form>
                <p>
                    <h:outputText value="#{jSFeatBean.result}"/>
                </p>
    <h:commandButton type="submit"
                     value="Submit"
                     action="#{jSFeatBean.addNumbers}"/>
            </h:form>
        </h:body>
    </html>
  9. Right click on the index.xhtml then –> Run; you should see something like this:

    JSF-FEAT-05

  10. Click submit button and watch h:outputText value, its value should be “Hi there I am an action method.
  11. Now everything is working fine. Let us do the actual work I need to demonstrate.
  12. Change addNumbers() method signature to the following (without restarting the application server):
    1. Bean method:
      public Object addNumbers(int num1) {
            int res = 0;
            res = num1;
            if (res == 0) {
                setResult("Hi there I am an action method to add numbers.");
            } else {
                setResult("Final result is: " + res);
            }
            return null;
        }
    2. Then your button call to:
      <h:commandButton type="submit"
                       value="Submit" 
                       action="#{jSFeatBean.addNumbers(1)}"/>
    3. Press the button and output text value should be:
      Final result is: 1
  13. Do it one more time:
    1. Bean method:
      public Object addNumbers(int num1, int num2) {
            int res = 0;
            res = num1 + num2;
            if (res == 0) {
                setResult("Hi there I am an action method to add numbers.");
            } else {
                setResult("Final result is: " + res);
            }
            return null;
        }
    2. Then your button call to:
      <h:commandButton type="submit"
                       value="Submit" 
                       action="#{jSFeatBean.addNumbers(1,2)}"/>
    3. Press the button and output text value should be:
      Final result is: 3

Note: the parameter could be with different parameters type not expected to be the same type, also method could return a value for navigation to other pages, in this example it returns null to be on the same page.

I liked this feature too much enjoy it, very helpful and handy, and happy coding.

Resources:

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ammianus
ammianus
8 years ago

JSF is new to me. Does the return value always have to be a member variable of the first backing bean that was submitted?

I was trying to think of a way to structure my application with two beans

1) InputBean – takes input values from form, on submit does some transformation
2) ResultBean – gets the results of the transformation (complex object with multiple properties) and is used to display the output

I couldn’t figure out how to wire this together.

Back to top button