当我单击commandLink时,侦听器工作,表单重新呈现,显示另一个commandLink。然后,如果单击该命令链接,则仅重新呈现表单,并且不调用侦听器。刷新后,一切正常。
我的JSF页面:
<h:form>
<h:panelGroup layout="block" styleClass="navLink"
rendered="#{userBean.loggedUser ne null and not userBean.userLogged and not userBean.loggedUserFriend}">
<h:commandLink value="Add to friends">
<f:ajax render="@form" listener="#{userBean.addToFriends}"/>
</h:commandLink>
</h:panelGroup>
<h:panelGroup layout="block" styleClass="navLink" rendered="#{userBean.loggedUser ne null and userBean.loggedUserFriend}">
<h:commandLink value="Remove from friends">
<f:ajax render="@form" listener="#{userBean.removeFromFriends}"/>
</h:commandLink>
</h:panelGroup>
</h:form>
的UserBean:
@Named
@SessionScoped
public class UserBean implements Serializable {
private User loggedUser;
private User user;
@EJB
private UserDAO dao;
public boolean isUserLogged() {
return loggedUser != null && user.equals(loggedUser);
}
public boolean isLoggedUserFriend() {
return loggedUser != null && loggedUser.getFriends().contains(user);
}
public void addToFriends() {
if (loggedUser != null && user != null) {
loggedUser.addFriend(user);
dao.update(loggedUser);
}
}
public void removeFromFriends() {
if (loggedUser != null && user != null) {
loggedUser.getFriends().remove(user);
dao.update(loggedUser);
}
}
}
编辑:也尝试使用无条件渲染的链接,但无济于事