例如,假设我想运行一些逻辑,然后点击/page.html#elementid
<h:commandLink action="#{myBean.action}" value="Go"/>
和
public String action()
{
// Some logic here
return "/page.xhtml#elementid";
}
我找不到任何与此相关的示例,想知道是否有解决方案?
最佳答案
#elementid
URI 片段也必须发送到客户端。这并没有发生在这里。您基本上是在执行服务器端转发。您应该执行客户端重定向。
public void action() throws IOException {
// ...
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/page.xhtml#elementid");
}
或者,您可以有条件地呈现一些 JavaScript 来设置 URI 片段:
public String action() {
// ...
hash = "elementid";
return "/page.xhtml";
}
在 page.xhtml
:
<h:outputScript target="body" rendered="#{not empty bean.hash}">
location.hash = "#{bean.hash}";
</h:outputScript>
<h:link>
顺便说一下,它明确支持 URI 片段。
<h:link value="Go to page" outcome="page" fragment="elementid" />
但是它会触发 GET 请求,因此任何预初始化业务操作都需要在与基于(后)构造函数或 <f:viewParam>
的目标页面关联的 bean 中执行。 .
关于jsf - 是否有可能有 h :commandLink (or similar) to navigate to an URI with a fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12705189/