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:

jenue said...

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

Pierre Wargnier said...

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");
}
}

Unknown said...

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

Francisca, Chile.

Firat said...

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.

Firat said...

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

Destro born said...

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

i tried it and got worked

Unknown said...

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

Destro born said...

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

Unknown said...

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");

}

wooden said...

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

Gerrardz said...

Thank you for sharing this amazing tutorial!
Rental Forklift

Jacinthe said...

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

Related Posts with Thumbnails