Saturday, December 02, 2006

Parameter passing in JSF

My part of the interns' project is to develop a web portal using JSF. However I'm new to JSF and have to search and learn. The hardest point I struggled with is passing parameters to the backing bean from the UI. I was stuck a whole day on this. Finally I got it and I'm wondering how hard this simple thing to be found and this is that.

<h:form>
    <h:commandButton value="Show" actionListener="#{MyBean.setStr}" action="#{MyBean.go}" >
        <f:attribute name="name" value="Hello"/>
    </h:commandButton>
</h:form>

public class MyBean {
    private String str;
    .....
    public void setStr(ActionEvent event){
        String name = (String) event.getComponent().getAttributes().get("name");
        this.str=name;
    }

    public String go(){
        return ("success");
    }
}

This may help those who are new to JSF like me:)



12 comments:

  1. hi! i've tried your code but still doesnt work in mine

    ReplyDelete
  2. Just do like this:

    <h:form>
    <h:commandButton value="Show" action="#{MyBean.go}" >
    <f:param name="name" value="Hello"/>
    </h:commandButton>
    </h:form>

    public class MyBean {
    private String str;

    public String go(){
    FacesContext context = FacesContext.getCurrentInstance();
    this.str = (String)
    context.getExternalContext
    ().getRequestParameterMap().get("name");
    return ("success");
    }
    }

    ReplyDelete
  3. Hey, your post save my life! I was really stuck with a homework! Thank you! You really have helped me :)

    Francisca, Chile.

    ReplyDelete
  4. excellent.

    <managed-bean>
    <managed-bean-name>content</managed-bean-name>
    <managed-bean-class>ContentBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
    <property-name>addressTitle</property-name>
    <value>#{param.gpa}</value>
    </managed-property>
    </managed-bean>

    i used this. but it was not useful for session bean. Your solution is better.

    ReplyDelete
  5. another solution:
    http://balusc.blogspot.com/2006/06/communication-in-jsf.html

    ReplyDelete
  6. hey pals we can also use backing beans for passing the parameter...

    i tried it and got worked

    ReplyDelete
  7. Hey, try prettyfaces... it injects parameters into beans and also lets you call action methods when pages are loaded.

    ReplyDelete
  8. tat's wat v call as bean injection....ya it's a good method for param passing...which prevents us URL rewriting....and promotes easiest way for further enhancement

    ReplyDelete
  9. Or u can do like this




    In Ur bean U have to write this Code
    public void action(ActionEvent event){
    String comapnyname = (String)event.getComponent(). getAttributes().get("name");

    }

    ReplyDelete
  10. thank you for that great blog.. saved my much time :)

    ReplyDelete
  11. Thank you for sharing this amazing tutorial!
    Rental Forklift

    ReplyDelete
  12. I enjoy reading through your article post, I wanted to write a little comment to support you and wish you a good continuation All the best for all your blogging efforts.
    Mesin Fotocopy

    ReplyDelete