<h:button>
The <h:button> http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/h/button.html生成一个 HTML<input type="button">
。生成的元素使用 JavaScript 导航到属性给定的页面outcome
,使用 HTTP GET 请求。
E.g.
<h:button value="GET button" outcome="otherpage" />
将生成
<input type="button" onclick="window.location.href='/contextpath/otherpage.xhtml'; return false;" value="GET button" />
尽管这最终会导致浏览器地址栏中的(可添加书签的)URL 发生变化,但这并不利于 SEO。搜索机器人不会跟踪onclick
。你最好使用一个<h:outputLink>
or <h:link>
SEO 对于给定的 URL 是否很重要。如果需要,您可以在生成的 HTML 上添加一些 CSS<a>
元素使其看起来像一个按钮。
请注意,虽然您可以将引用方法的 EL 表达式放入outcome
属性如下,
<h:button value="GET button" outcome="#{bean.getOutcome()}" />
it will not单击按钮时将被调用。相反,当呈现包含按钮的页面时,它已经被调用,其唯一目的是获取要嵌入到生成的导航结果中。onclick
代码。如果您曾经尝试使用操作方法语法,如下所示outcome="#{bean.action}"
,通过面对一个问题,你已经被这个错误/误解所暗示javax.el.ELException:在 com.example.Bean 类中找不到属性 actionMethod https://stackoverflow.com/questions/7950628/javax-el-elexception-could-not-find-property-actionmethod-in-class-com-example.
如果您打算通过 POST 请求调用方法,请使用<h:commandButton>
相反,请参见下文。或者,如果您打算通过 GET 请求调用方法,请前往在页面加载时调用 JSF 托管 Bean 操作 https://stackoverflow.com/questions/2451154/invoke-jsf-managed-bean-action-on-page-load或者如果您还有 GET 请求参数<f:param>
, 如何在页面加载时处理支持 bean 中的 GET 查询字符串 URL 参数? https://stackoverflow.com/questions/10724428/how-do-i-process-get-query-string-url-parameters-in-backing-bean-on-page-load/
<h:commandButton>
The <h:commandButton> http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/h/commandButton.html生成一个 HTML<input type="submit">
默认情况下提交父级的按钮<h:form>
使用 HTTP POST 方法并调用附加到的操作action
, actionListener
and/or <f:ajax listener>
,如果有的话。这<h:form>
是必须的。
E.g.
<h:form id="form">
<h:commandButton id="button" value="POST button" action="otherpage" />
</h:form>
将生成
<form id="form" name="form" method="post" action="/contextpath/currentpage.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="form" value="form" />
<input type="submit" name="form:button" value="POST button" />
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="...." autocomplete="off" />
</form>
请注意,它因此提交到当前页面(表单操作 URL 将显示在浏览器地址栏中)。之后就会forward到目标页面,无需更改浏览器地址栏中的 URL。你可以添加?faces-redirect=true
参数到结果值以在 POST 后触发重定向(根据重定向后获取模式 http://en.wikipedia.org/wiki/Post/Redirect/Get) 使目标 URL 成为可添加书签的。
The <h:commandButton>
通常专门用于提交 POST 表单,而不是执行页面到页面的导航。通常情况下,action
指向一些业务操作,例如将表单数据保存在DB中,它返回一个String
结果。
<h:commandButton ... action="#{bean.save}" />
with
public String save() {
// ...
return "otherpage";
}
返回null
or void
将会带您回到相同的视图。也返回一个空字符串,但它会重新创建任何视图作用域 bean。如今,随着现代 JSF2 和<f:ajax>
,通常情况下,动作只是返回到相同的视图(因此,null
or void
) 其中结果由 ajax 有条件地呈现。
public void save() {
// ...
}
也可以看看:
- 如何在 JSF 中导航?如何使 URL 反映当前页面(而不是上一页) https://stackoverflow.com/questions/15521451/how-to-navigate-in-jsf-how-to-make-url-reflect-current-page-and-not-previous-o
- 什么时候应该使用 h:outputLink 而不是 h:commandLink? https://stackoverflow.com/questions/4317684/when-should-i-use-houtputlink-instead-of-hcommandlink
- 动作和动作监听器之间的区别 https://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener