2

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

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

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

/mypage.xhtml

不工作(404 错误)

4

2 回答 2

5

Let EL print the context path dynamically. It'll become relative to the domain root.

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

Or, better, just use JSF's own <h:button> component which generates the same HTML anyway wherein you don't need to worry about the context path.

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

If you need to pass any request parameters, you can use <f:param> for that.

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

Don't use the <h:commandButton>. It doesn't send a normal GET request, but a POST request which you'd need to redirect and thus you effectively end up with two HTTP requests wherein all request parameters are lost in the second one.

于 2012-05-21T18:07:09.223 回答
1

因此,如果您想使用 JSF commandButton 前往项目中的某个页面,您需要使用 action 属性,您甚至可以传递参数:

<h:commandButton action="/details.jsf?faces-redirect=true" value="details">
    <f:attribute name="id" value="#{bean.id}" />
</h:commandButton>

如果您想访问应用程序之外的页面,则需要在支持 bean 的某处添加一些额外的 java 代码,然后从前端触发它。

FacesContext.getCurrentInstance().getExternalContext().redirect("http://google.com";);

注意:此任务不需要 javascript

于 2012-05-21T18:24:58.983 回答