我在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"?我确实需要它是一个标签,这样我就可以正确地使用引导图标元素。我知道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;
}
让登录保持原样,现在一切都正常了。
您不应该将任何表达式语言直接调用到非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应用于注销文本。