gpt4 book ai didi

javascript - 按钮 JSF 中的绝对 URL

转载 作者:行者123 更新时间:2023-11-29 16:20:43 25 4
gpt4 key购买 nike

我在许多页面中包含的页眉中都有这个按钮:

<input type="button" value="My Button" onclick="window.location.href='mypage.xhtml'"

它使用相对路径,如果调用者页面在同一目录中,它就可以工作。如何使用绝对路径或使用 commandButton 标签?我试过:

/mypage.xhtml

不工作(404 错误)

最佳答案

让 EL 打印 context path动态地。它将成为相对于域根的。

<input type="button" value="My Button" onclick="window.location.href='#{request.contextPath}/mypage.xhtml'">

或者,更好的是,只使用 JSF 自己的 <h:button> 无论如何生成相同 HTML 的组件,您无需担心上下文路径。

<h:button value="My Button" outcome="/mypage.xhtml" />

如果需要传递任何请求参数,可以使用<f:param>为此。

<h:button value="My Button" outcome="/mypage.xhtml">
<f:param name="foo" value="bar" />
</h:button>

不要使用 <h:commandButton> .它不发送普通的 GET 请求,而是发送一个您需要重定向的 POST 请求,因此您最终得到两个 HTTP 请求,其中所有请求参数都在第二个请求中丢失。

关于javascript - 按钮 JSF 中的绝对 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10690203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com