In JSF 2.0, both <h:button /> and <h:commandButton /> tags are used to render HTML input element of type button, with different mechanism to handle the navigation.

1. JSF h:commandButton example

The “h:commandButton” tag is released since JSF 1.x, you can declare the bean, which return the navigation outcome in the “action” attribute. If browser’s with JavaScript disabled, the navigation is still working, because the navigation is handled via form post.

1. Submit button

//JSF
<h:commandButton value="submit" type="submit" action="#{user.goLoginPage}" />
//HTML output
<input type="submit" name="xxx" value="submit" />

2. Reset button

//JSF
<h:commandButton value="reset" type="reset" />
//HTML output
<input type="reset" name="xxx" value="reset" />

3. Normal button

//JSF
<h:commandButton value="button" type="button" />
//HTML output
<input type="button" name="xxx" value="button" />

4. Normal button with onclick event

//JSF
<h:commandButton value="Click Me" type="button" onclick="alert('h:commandButton');" />
//HTML output
<input type="button" name="xxx" value="Click Me" onclick="alert('h:commandButton');" />

2. JSF h:button example

The “h:button” is a new tag in JSF 2.0, you can declared the navigation outcome directly in the “outcome” attribute, no need to call a bean to return an outcome like “h:commandButton” above. But, if browser’s with JavaScript disabled, the navigation will failed, because the “h:button” tag is generate an “onclick” event to handle the navigation via “window.location.href”. See examples :

1. Normal button without outcome

//JSF
<h:button value="buton" />
//HTML output
<input type="button"
onclick="window.location.href='/JavaServerFaces/faces/currentpage.xhtml; return false;"
value="buton" />

P.S if the outcome attribute is omitted, the current page URL will treat as the outcome.

2. Normal button with an outcome

//JSF
<h:button value="buton" outcome="login" />
//HTML output
<input type="button"
onclick="window.location.href='/JavaServerFaces/faces/login.xhtml; return false;"
value="buton" />

3. Normal button with JavaScript.

//JSF
<h:button value="Click Me" onclick="alert('h:button');" />
//HTML output
<input type="button"
onclick="alert('h:button');window.location.href='/JavaServerFaces/faces/page.xhtml;return false;"
value="Click Me" />

My thought…

No really sure why JSF 2.0 released this “h:button” tag, the JavaScript redirection is not practical, especially in JavaScript disabled browser. The best is integrate the “outcome” attribute into the “h:commandButton” tag, hope it can be done in future release.

JSF 2 button and commandButton example的更多相关文章

  1. JSF 与 HTML 标签的联系

    *页面的开头 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ t ...

  2. Ajax4Jsf 简单介绍

    Ajax4jsf 允许开发人员将 Ajax 功能添加到 JSF 应用程序中,而不需要 JavaScript 或用 Ajax 图形部件替换现有的组件.这个包还允许在使用 Java 2D 库时动态地生成图 ...

  3. VSTO在幻灯片里面添加按钮对象

    //添加Form窗体,窗体中添加Image控件,单击弹出"PPT"信息提示 //命名引用:using MF = Microsoft.Vbe.Interop.Forms; priva ...

  4. 关于JSF中immediate属性的总结(二)

    The immediate attribute in JSF is commonly misunderstood. If you don't believe me, check out Stack O ...

  5. Parameter Passing / Request Parameters in JSF 2.0 (转)

    This Blog is a compilation of various methods of passing Request Parameters in JSF (2.0 +) (1)  f:vi ...

  6. JSF页面中使用js函数回调后台bean方法并获取返回值的方法

    由于primefaces在国内使用的并不是太多,因此,国内对jsf做系统.详细的介绍的资料很少,即使有一些资料,也仅仅是对国外资料的简单翻译或者是仅仅讲表面现象(皮毛而已),它们的语句甚至还是错误的, ...

  7. JSF 2 dropdown box example

    In JSF, <h:selectOneMenu /> tag is used to render a dropdown box – HTML select element with &q ...

  8. JSF 2 multiple select listbox example

    In JSF, <h:selectManyListbox /> tag is used to render a multiple select listbox – HTML select ...

  9. JSF 2 listbox example

    In JSF, <h:selectOneListbox /> tag is used to render a single select listbox – HTML select ele ...

随机推荐

  1. WinCE5.0中文模拟器SDK(VS2005)的配置

    WinCE5.0中文模拟器SDK的安装过程不细说了,一路默认即可,下面主要介绍如何配置,使其能在VS2005中正常使用. 安装完成后,打开VS2005,点击菜单“工具”——“选项”——“设备工具”—— ...

  2. R语言多重共现性的检测

    1.kappa值 2. library(car)vif(lm.sol) 得到各个系数的方差膨胀因子,当0<VIF<10的时候,不存在多重共线性,当10<=VIF<100,存在较 ...

  3. HDU (线段树 单点更新) I Hate It

    和上一道题没什么变化,只不过把单点增减变成了单点替换,把区间求和变成了区间求最大值. #include <cstdio> #include <algorithm> using ...

  4. HDU 1074 Doing Homework

    第一次做这道题大概是半个月前了吧,状压DP一个很新鲜的名词 当时看题解怎么也看不懂,现在看懂了以后还是很简单的 所谓状态压缩就是用一个整数的二进制来表示一个状态,比如有三个作业 000表示一科作业也没 ...

  5. 51nod1055 最长等差数列

    完全一脸懵逼!.dp[i][j]表示i,j为相邻的两项的最大值.两个指针两边扫的思想好劲啊这个!%%% #include<cstdio> #include<cstring> # ...

  6. SQL语句方法语法总结(二)

    1.给表插入数据. (1)INSERT INTO TBL_NAME VALUES (VALUE_1,VALUE_2,...) (2)INSERT INTO TBL_NAME (COL_1,COL_2, ...

  7. 所在实习公司的JS笔试题

    在班上无聊的时候看到了一份JS笔试题(我是电面进去的,没做过这份题~~),开始还觉得蛮简单......后来觉得还是很有意思的,贴出来一起看看. 题目一: if(!("a" in w ...

  8. 两个异步处理AsyncTask和Handler的优缺点

    AsyncTask和Handler对比 1 ) AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操 ...

  9. JVM内存结构之三--持久代

    本文会介绍一些JVM内存结构的基本概念,然后很快会讲到持久代,来看下Java SE 8发布后它究竟到哪去了. 基础知识 JVM只不过是运行在你系统上的另一个进程而已,这一切的魔法始于一个java命令. ...

  10. 使用命令行设置svn忽略列表

    Windows 上的 TortoiseSVN 设置 svn 的忽略列表是非常方便的,但是在Mac OS X上,好用的图形化 svn 客户端都有点儿贵,比如 Versions 和 CornerStone ...