按钮JSF中的绝对URL

enf*_*fix 2 javascript button jsf-2

我在标题中有这个按钮,我在很多页面中包含:

<input type="button" value="My Button" onclick="window.location.href='mypage.xhtml'"
Run Code Online (Sandbox Code Playgroud)

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

/mypage.xhtml
Run Code Online (Sandbox Code Playgroud)

不工作(404错误)

Bal*_*usC 5

让EL 动态打印上下文路径.它将变为相对于域根.

<input type="button" value="My Button" onclick="window.location.href='#{request.contextPath}/mypage.xhtml'">
Run Code Online (Sandbox Code Playgroud)

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

<h:button value="My Button" outcome="/mypage.xhtml" />
Run Code Online (Sandbox Code Playgroud)

如果您需要传递任何请求参数,您可以使用<f:param>它.

<h:button value="My Button" outcome="/mypage.xhtml">
    <f:param name="foo" value="bar" />
</h:button>
Run Code Online (Sandbox Code Playgroud)

不要使用<h:commandButton>.它不会发送正常的GET请求,而是发送一个您需要重定向的POST请求,从而有效地结束了两个HTTP请求,其中所有请求参数都在第二个请求参数中丢失.