Thodoris Bais

About Thodoris Bais

Thodoris Bais is currently working as a Junior Java Developer while on parallel, pursuing a diploma at Informatics & Telecommunications Engineering. He specializes in Java Core, Android and J2EE and looks forward to being a professional Software Engineer. He is also a quick learner and curious about new technologies.

Button and CommandButton Example with JSF 2.0

Hello Java code geeks! Today we ‘re gonna take a look at navigation handling, using buttons. Regarding JSF 2.0, both <h:button /> and <h:commandButton /> are used to render HTML input element mechanisms that can guide the navigation through a web application.

1. h:commandButton tag

We first have to declare the bean, which returns the navigation’s outcome in the action attribute. This navigation is handled through a form post, so it will also work in a browser with disabled Javascript.

Let’s have a look at a sample submit button:

<h:commandButton value="Submit" type="submit" action="#{user.login}" />

And here is what is generated to HTML:

<input type="submit" name="xxx" value="Submit" />

Pretty cool, huh?! Ok, let’s see a reset button now:

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

Regarding basic understanding efforts, let’s investigate the difference between a normal button and a same one, with an event listener attached.

Normal button

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

Normal button with onClick event listener

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

2. h:button tag

While on JSF 2.0, we can use the latest button tags, such as <h:commandButton>, so generally, there is no need for the deprecated one, <h:commandButton>, which is available, since JSF 1.x. And if you ‘re still wondering about last sentence’s italics format, this comes from our tag’s attributes:

  1. We can have a direct navigation declaration, without the need to call a bean first.
  2. If our browser has disabled its Javascript feature, navigation may fail, as the <h:button> tag generates an onclick event listener, that handles the navigation through window.location.href.

Just to ensure that noone has questionmarks, here are some very basic examples, using the fore-mentioned tag:

Normal button – no outcome

<h:button value="Click Me!" />			
//HTML output
<input type="button" 
   onclick="window.location.href='/JavaServerFaces/faces/index.xhtml; return false;" 
   value="Click Me!" />

Normal button – with outcome

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

Normal button – with Javascript

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

Happy Easter to you and your families!
This was an example of Button and CommandButton in JSF 2.0.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books