0

i want update a bean property from the same jsf wo contains this bean property by getting the new value from a method inside this bean.

here is the jsf sample :

<h3>JSF Login Logout</h3>
    <h:outputText value="Username" />
    <h:inputText id="username" value="#{login.user}"></h:inputText>
<h:outputText value="Password" />
    <h:inputSecret id="password" value="#{login.pwd}"></h:inputSecret>
    <h:message for="password"></h:message>
    <br /><br />

    <h:commandButton action="#{login.validateUsernamePassword}"
        value="Login"></h:commandButton>

here is the bean method :

//validate login
public String validateUsernamePassword() {
...
    this.user = "admin";
...

}

i want return back to the same jsf and fill user field by the new value.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Anass Boukalane
  • 451
  • 4
  • 20

1 Answers1

4

If you want to stay on the same page then you don't have to return String, just create void method. If you want to update fields after form submit you can do it in JSF component by ajax with render attribute and specify these fields with ID or by selectors like @form:

<h:commandButton action="#{login.validateUsernamePassword}" value="Login">
    <f:ajax execute="formId" render="username otherComponentId"/>
</h:commandButton>

or update from backing bean:

public String validateUsernamePassword() {
...
    this.user = "admin";
    RequestContext.getCurrentInstance().update("formId:username"); //PrimeFaces solution since you have PrimeFaces tag on your question
...

}

Better option is to use render attribute and probably you would like to also update h:message for any occurred errors (give it ID and update it in render).

Community
  • 1
  • 1
Geinmachi
  • 1,231
  • 1
  • 8
  • 20