按钮和h::小时之间差异的commandButton [英] Difference between h:button and h:commandButton

查看:611
本文介绍了按钮和h::小时之间差异的commandButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSF 2,是 H中的区别:按钮 H:的commandButton

In JSF 2, what is the difference between h:button and h:commandButton ?

推荐答案

的<一个href=\"http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/h/button.html\"><$c$c><h:button>生成一个HTML &LT;输入类型=按钮&GT; 。所生成的元素使用JavaScript导航到由属性结果给出的页面,使用HTTP GET请求。

<h:button>

The <h:button> generates a HTML <input type="button">. The generated element uses JavaScript to navigate to the page given by the attribute outcome, using a HTTP GET request.

例如

<h:button value="GET button" outcome="otherpage" />

将产生

<input type="button" onclick="window.location.href='/contextpath/otherpage.xhtml'; return false;" value="GET button" />

尽管这在浏览器地址栏(可收藏)URL变化结束了,这不是搜索引擎友好。 Searchbots不会跟随URL在的onclick 。你最好使用&LT; H:outputLink的&GT; &LT; H:链接&GT; 如果SEO是重要的给定的URL。你可以,如果在一些CSS必要扔生成的HTML &LT;一个方式&gt; 元素,使它看起来像一个按钮

Even though this ends up in a (bookmarkable) URL change in the browser address bar, this is not SEO-friendly. Searchbots won't follow the URL in the onclick. You'd better use a <h:outputLink> or <h:link> if SEO is important on the given URL. You could if necessary throw in some CSS on the generated HTML <a> element to make it to look like a button.

请注意,虽然你可以把一个EL前pression在指结果的方法如下属性,

Do note that while you can put an EL expression referring a method in outcome attribute as below,

<h:button value="GET button" outcome="#{bean.getOutcome()}" />

它会不可以当您单击该按钮来调用。相反,当含有该按钮的呈现页面的唯一目的,以获得导航结果它已调用要被嵌入在生成的onclick code。如果你曾经试图使用操作方法的语法在的结果=#{} bean.action,你已经被这个错误/误解由面临暗示<一个href=\"http://stackoverflow.com/questions/7950628/javax-el-elexception-could-not-find-property-actionmethod-in-class-com-example\">javax.el.ELException:找不到财产actionMethod类com.example.Bean 。

it will not be invoked when you click the button. Instead, it is already invoked when the page containing the button is rendered for the sole purpose to obtain the navigation outcome to be embedded in the generated onclick code. If you ever attempted to use the action method syntax as in outcome="#{bean.action}", you would already be hinted by this mistake/misconception by facing a javax.el.ELException: Could not find property actionMethod in class com.example.Bean.

如果您打算调用一个方法作为POST请求的结果,使用&LT; H:的commandButton&GT; 代替,见下文。或者,如果你打算调用一个方法作为GET请求的结果,头的调用JSF托管在页面加载豆动作,或者您也可以通过&LT有GET请求参数; F:参数&GT; ,<一个href=\"http://stackoverflow.com/questions/10724428/how-do-i-process-get-query-string-url-parameters-in-backing-bean-on-page-load/\">How我在处理页面加载备份豆GET查询字符串的URL参数?

If you intend to invoke a method as result of a POST request, use <h:commandButton> instead, see below. Or if you intend to invoke a method as result of a GET request, head to Invoke JSF managed bean action on page load or if you also have GET request parameters via <f:param>, How do I process GET query string URL parameters in backing bean on page load?

的<一个href=\"http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/h/commandButton.html\"><$c$c><h:commandButton>生成一个HTML &LT;输入类型=提交&GT; 按钮在默认情况下提交父&LT; H:形式&GT; 使用HTTP POST方法,并调用连接到动作的ActionListener 的行动和/或 &LT; F:AJAX听者GT; ,如果有的话。在&LT; H:形式&GT; 要求

The <h:commandButton> generates a HTML <input type="submit"> button which submits by default the parent <h:form> using HTTP POST method and invokes the actions attached to action, actionListener and/or <f:ajax listener>, if any. The <h:form> is required.

例如

<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将在浏览器地址栏中显示)。它将事后的转发的目标页面,无需在浏览器地址栏中的URL任何变化。 ?您可以添加面临重定向=真正参数的结果值触发POST之后重定向(根据的后重定向-获取模式),使目标URL成为可收藏。

Note that it thus submits to the current page (the form action URL will show up in the browser address bar). It will afterwards forward to the target page, without any change in the URL in the browser address bar. You could add ?faces-redirect=true parameter to the outcome value to trigger a redirect after POST (as per the Post-Redirect-Get pattern) so that the target URL becomes bookmarkable.

&LT; H:的commandButton&GT; 通常是专门用于提交一个POST形式,不执行页到页面导航。通常情况下,动作指出了一些商业动作,比如保存在数据库,它返回一个字符串结果表单数据

The <h:commandButton> is usually exclusively used to submit a POST form, not to perform page-to-page navigation. Normally, the action points to some business action, such as saving the form data in DB, which returns a String outcome.

<h:commandButton ... action="#{bean.save}" />

public String save() {
    // ...
    return "otherpage";
}

返回无效将带给你回到了同样的看法。返回一个空字符串也有,但是它会重新创建任何视图作用域bean。这些天来,随着现代JSF2和&LT; F:AJAX&GT; ,超过通常的动作只是回到了同样的看法(因此,无效),其中结果有条件通过AJAX渲染。

Returning null or void will bring you back to the same view. Returning an empty string also, but it would recreate any view scoped bean. These days, with modern JSF2 and <f:ajax>, more than often actions just return to the same view (thus, null or void) wherein the results are conditionally rendered by ajax.

public void save() {
    // ...
}


参见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆