以下是我的观点:
<?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>
任何我的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;
}
}
因此,当我在Name字段中输入内容并单击submit时,我希望看到带有id“greeting”的outputText,因为user.nameRendered()应该返回true。
但是,我需要在提交名称后刷新页面才能看到它。为什么可能是这个原因?为什么需要页面刷新?如果没有页面刷新,我怎么能这样做?
答案 0 :(得分:2)
您无法刷新未在JSF树中呈现的组件,您需要对始终存在的组件内的输出进行编码:
<h:panelGroup id="greeting">
<h:outputText value="Welcome + #{user.name}" rendered="#{user.nameRendered()}"/>
</h:panelGroup>
更多信息: