JSF/Primefaces span onClick自动调用Backing Bean
问题描述:
我在做一个JSF项目,遇到以下问题。JSF/Primefaces span onClick自动调用Backing Bean
当我创建以下范围:
<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>
它会调用这个方法退出()
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门多萨
改组成以:
<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:
#clearCommand {
cursor: pointer;
}
左登录,因为它是,一切现在工作了。
答
您不应将任何表达式语言直接调用到非JSF组件中。什么你要找的是一个<h:commandLink>
代替:
<h:form>
<h:commandLink action="#{Login.logout()}" styleClass="foo">
<span style="...">logout</span>
</h:commandLink>
</h:form>
哪里foo
是你清除一个<a>
默认格式CSS类。然后,您可以使用常见的HTML <span>
组件将所需的CSS应用于注销文本。