我在JSF做一个项目,我遇到了以下问题。
当我创建以下范围时:
<ui:composition>
<div id="header" class="header">
<p style="float: right; padding-right: 20px">
Welcome, #{infoGet.username} <span class="glyphicon glyphicon-off" style="color: darkred; cursor: pointer;" onclick="#{Login.logout()}" />
</p>
</div>
</ui:composition>
它调用此方法logout()
public void logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
try {
FacesContext.getCurrentInstance().getExternalContext().redirect("Landing.xhtml");
} catch(IOException e) {
e.printStackTrace();
}
}
然而,在页面加载时,如果我这样做,则会自动调用跨度内的onClick:
public void logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
}
刷新页面会话将失效。
有没有办法从一个范围调用方法“onClick”?我确实需要它作为标签,所以我可以正确使用Bootstrap图标元素。我知道onClick通常用于Javascript,但它与JSF一起工作似乎是合乎逻辑的。
使用@ luiggi-mendoza的解决方案进行编辑
将作文改为:
<ui:composition>
<div id="header" class="header">
<h:form style="float: right; padding-right: 20px">
<h:commandLink action="#{Login.logout()}" styleClass="clearCommand">
<span class="glyphicon glyphicon-off" style="color: darkred; cursor: pointer;" />
</h:commandLink>
</h:form>
<p style="float: right; padding-right: 20px">
Welcome, #{infoGet.username}
</p>
</div>
</ui:composition>
明确命令:
#clearCommand {
cursor: pointer;
}
保持登录状态,现在一切正常。
答案 0 :(得分:1)
您不应将任何表达式语言直接调用到非JSF组件中。您正在寻找的是<h:commandLink>
代替:
<h:form>
<h:commandLink action="#{Login.logout()}" styleClass="foo">
<span style="...">logout</span>
</h:commandLink>
</h:form>
其中foo
是一个CSS类,用于清除<a>
的默认格式。然后,您可以使用常见的HTML <span>
组件将所需的CSS应用于您的注销文本。