user2953119
user2953119

Reputation:

Rendered after ActionEvent

I want that inputText will be rendered on page after click on the button without entire reload of the page. Consider the facelet 1.xhtml with following components:

<h:commandButton id="show" value="Show" actionListener="#{f.rend}"/>
<h:inputText id="usrname" binding="#{f.usrname}" rendered="false"/>

and managed bean

@Named("f")
@RequestScoped
private HtmlInputText usrname;
//getter,setter
public void rend(ActionListener e){
//How this method must be implemented?
}

Upvotes: 1

Views: 413

Answers (1)

BalusC
BalusC

Reputation: 1108742

You need to send an ajax (partial) request instead of a full request. You can use <f:ajax> inside the command component for this. It has a render attribute taking a spaceseparated collection of client IDs which needs to be updated. Then, the input component's rendered attribute must be a dynamic value expression referring a bean property instead of a hardcoded value of false. E.g. a boolean property. You just have to set that property to true then. You don't and shouldn't need to bind the whole component to the bean. That's poor practice.

All in all, this should do:

<h:commandButton id="show" value="Show" action="#{bean.setShow(true)}">
    <f:ajax render="group" />
</h:commandButton>
<h:panelGroup id="group">
    <h:inputText id="username" rendered="#{bean.show}" />
</h:panelGroup>

with (assuming JSF 2.2, with @ViewScoped for reasons mentioned here)

import javax.inject.Named;
import javax.faces.view.ViewScoped;

@Named
@ViewScoped
public class Bean {

    private boolean show;

    public boolean isShow() {
        return show;
    }

    public void setShow(boolean show) {
        this.show = show;
    }

}

Upvotes: 1

Related Questions