0

Here is my view:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Welcome</title>
</h:head>
<h:body>
    <h:form prependId="false" id="myForm">

        <h3>Please enter your name and password.</h3>

        Name: <h:inputText id="name" value="#{user.name}"/>

        Password: <h:inputSecret id="password" value="#{user.password}"/>

        <h:commandButton value="Login">
            <f:ajax execute="name password" render="greeting"/>
        </h:commandButton>

        <h:outputText id="greeting" value="Welcome + #{user.name}" rendered="#{user.nameRendered()}"/>
    </h:form>
</h:body>
</html>

Any my User.java :

@Named
@SessionScoped
public class User implements Serializable {

    private String name;
    private String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean nameRendered() {
        return this.name != null;
    }
}

So when I type something in Name field and click submit, I expect to see outputText with id"greeting" because, user.nameRendered() should return true.

However, I will need to refresh the page after I submit name to see it. Why might be the reason? Why does it require a page refresh? How can I do it without a page refresh?

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

1 Answers1

2

You can't refresh components that are not rendered in the JSF tree, you need to encalsulate the output inside a component that will always exists :

<h:panelGroup id="greeting">
    <h:outputText value="Welcome + #{user.name}" rendered="#{user.nameRendered()}"/>
</h:panelGroup>

More info :

Community
  • 1
  • 1
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72