jsf - 是否有可能有 h :commandLink (or similar) to navigate to an URI with a fragment?

标签 jsf jsf-2

例如,假设我想运行一些逻辑,然后点击/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/

相关文章:

java - JSF View id 作为请求 token

jsf - 执行 p :commandLink action before onclick event?

java - 无法将 HTML 添加到 <ui :define>

java - 在 CDI 实现项目中包含空 beans.xml 的目的是什么?

java - jsf隐式对象cc和组件的区别

JSF/Facelets : why is it not a good idea to mix JSF/Facelets with HTML tags?

jsf - JSF 数据表 var 属性字段中的空 EL 表达式始终为 false

jsf - 仅显示图像的命令按钮

发生异常时的 JSF 响应代码

jsf - h :commandLink not invoked 的 oncomplete 属性