<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>maxheapsize.com &#187; jsf2</title>
	<atom:link href="http://maxheapsize.com/tag/jsf2/feed/" rel="self" type="application/rss+xml" />
	<link>http://maxheapsize.com</link>
	<description>Oliver Wehrens on Programming and Agile</description>
	<lastBuildDate>Tue, 14 Jun 2011 20:41:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Getting started with JSF 2 (and Maven)</title>
		<link>http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/</link>
		<comments>http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 21:21:39 +0000</pubDate>
		<dc:creator>Oliver Wehrens</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[jsf2]]></category>

		<guid isPermaLink="false">http://maxheapsize.com/?p=270</guid>
		<description><![CDATA[I use Apache Trinidad at work and since JSF 2 is now final I decided to play with it a bit. Of course this is going to be the classic Hello World example (as there are many other like this around the web). First you need to setup a simple Maven project. The file layout [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fmaxheapsize.com%2F2009%2F07%2F03%2Fgetting-started-with-jsf-2-and-maven%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fmaxheapsize.com%2F2009%2F07%2F03%2Fgetting-started-with-jsf-2-and-maven%2F&amp;source=owehrens&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I use Apache Trinidad at work and since JSF 2 is now final I decided to play with it a bit.</p>
<p>Of course this is going to be the classic Hello World example (as there are many other like this around the web).</p>
<p>First you need to setup a simple Maven project. </p>
<p>The file layout should be something like that.</p>
<div class="wp-caption alignnone" style="width: 300px"><img alt="Directory Layout for JSF 2 Ajax Demo" src="http://maxheapsize.com/static/JSF2AjaxDemoFileLayout.png" title="Directory Layout for JSF 2 Ajax Demo" width="290" height="234" /><p class="wp-caption-text">Directory Layout for JSF 2 Ajax Demo</p></div>
<p>After that change your pom.xml to include the servlet and jsf2 libs.</p>
<pre class="brush: xml; title: ;">
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;com.maxheapsize&lt;/groupId&gt;
  &lt;artifactId&gt;jsf2demo&lt;/artifactId&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;jsf2demo Maven Webapp&lt;/name&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;com.sun.faces&lt;/groupId&gt;
      &lt;artifactId&gt;jsf-api&lt;/artifactId&gt;
      &lt;version&gt;2.0.0-b13&lt;/version&gt;
      &lt;scope&gt;compile&lt;/scope&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;com.sun.faces&lt;/groupId&gt;
      &lt;artifactId&gt;jsf-impl&lt;/artifactId&gt;
      &lt;version&gt;2.0.0-b13&lt;/version&gt;
      &lt;scope&gt;compile&lt;/scope&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
      &lt;artifactId&gt;jstl&lt;/artifactId&gt;
      &lt;version&gt;1.2&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
      &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
      &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
      &lt;version&gt;2.5&lt;/version&gt;
    &lt;/dependency&gt;

  &lt;/dependencies&gt;
  &lt;build&gt;
    &lt;finalName&gt;jsf2demo&lt;/finalName&gt;
  &lt;/build&gt;

  &lt;repositories&gt;
    &lt;repository&gt;
      &lt;id&gt;maven2-repository.dev.java.net&lt;/id&gt;
      &lt;name&gt;Java.net Repository for Maven&lt;/name&gt;
      &lt;url&gt;http://download.java.net/maven/2&lt;/url&gt;
    &lt;/repository&gt;
  &lt;/repositories&gt;

&lt;/project&gt;
</pre>
<p>First we are going to create the managed bean to hold the name we are going to enter.</p>
<pre class="brush: java; title: ;">
package com.maxheapsize.jsf2demo;

import javax.faces.bean.*;
import java.io.Serializable;

@ManagedBean
@SessionScoped
public class HelloWorldBean implements Serializable {

  private String name = &quot;&quot;;

  @ManagedProperty(value = &quot;#{demoService}&quot;)
  private Service service;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setService(Service service) {
    this.service = service;
  }

  public String getReverseName() {
    return service.reverse(name);
  }
}
</pre>
<p>To demonstrate the dependency injection with JSF 2 I use a service to reverse the name. Simply add the @ManagedProperty with the name of the service and it will get injected. Of course we need the service. Here it is:</p>
<pre class="brush: java; title: ;">
package com.maxheapsize.jsf2demo;

import javax.faces.bean.*;

@ManagedBean(name=&quot;demoService&quot;)
@ApplicationScoped
public class Service {

  public String reverse(String name) {
    return new StringBuffer(name).reverse().toString().toLowerCase();
  }
}
</pre>
<p>Very simple&#8230;isn&#8217;t it ?</p>
<p>The only thing what&#8217;s missing is the web.xml and the webpage itself.</p>
<p>The web.xml is pretty straight forward and should not show any surprises.</p>
<pre class="brush: xml; title: ;">
&lt;!DOCTYPE web-app PUBLIC
    &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot;
    &quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot; &gt;
&lt;web-app&gt;
  &lt;display-name&gt;Web Application&lt;/display-name&gt;

  &lt;servlet&gt;
    &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;

  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;

  &lt;context-param&gt;
    &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt;
    &lt;param-value&gt;Development&lt;/param-value&gt;
  &lt;/context-param&gt;
&lt;/web-app&gt;
</pre>
<p>The index.xhtml has some differences from earlier versions of JSF. First, JSF now supports Ajax out of the box. To have this enabled you need to load the jsf.js javascript library. The inputText tag binds to our managed bean and includes an f:ajax tag. This tag determines which part should be rerendered (in this case the element with the id reverseName). F:ajax will execute on value holding components always with an &#8216;onchange&#8217; event and on action components (like commandButtons) with an &#8216;onclick&#8217; event.<br />
The output will trigger the injected service in our managed bean and return the reversed input string.</p>
<pre class="brush: xml; title: ;">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
        &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;

&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
      xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot;
      xmlns:f=&quot;http://java.sun.com/jsf/core&quot;
      xmlns:h=&quot;http://java.sun.com/jsf/html&quot;&gt;

&lt;h:head&gt;
  &lt;title&gt;JSF Demo&lt;/title&gt;
&lt;/h:head&gt;
&lt;h:body&gt;
  &lt;h:outputScript name=&quot;jsf.js&quot; library=&quot;javax.faces&quot; target=&quot;body&quot;&gt;
&lt;/h:outputScript&gt;
  &lt;h1&gt;Ajax JSF 2 Demo&lt;/h1&gt;
  &lt;h:form&gt;

    &lt;h:inputText id=&quot;name&quot; value=&quot;#{helloWorldBean.name}&quot;&gt;
      &lt;f:ajax render=&quot;reverseName&quot;/&gt;
    &lt;/h:inputText&gt;

    &lt;h:commandButton value=&quot;Say reverse Hi via Ajax&quot;&gt;
      &lt;f:ajax execute=&quot;name&quot; render=&quot;reverseName&quot;/&gt;
    &lt;/h:commandButton&gt;
    &lt;h:outputText id=&quot;reverseName&quot; value=&quot;#{helloWorldBean.reverseName}&quot;/&gt;
  &lt;/h:form&gt;

&lt;/h:body&gt;
&lt;/html&gt;
</pre>
<div class="wp-caption alignnone" style="width: 406px"><img alt="JSF 2 Ajax Demo" src="http://maxheapsize.com/static/JSF2AjaxDemo.png" title="JSF 2 Ajax Demo" width="396" height="107" /><p class="wp-caption-text">JSF 2 Ajax Demo</p></div>
<p>And that&#8217;s it. Sure it&#8217;s a simple example, but so far I do like the minimal configuration and the ajax integration.</p>
<div class="google_plusone_widget"><g:plusone 
      count="true" href="http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/" size="medium"></g:plusone></div><!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/&amp;title=Getting+started+with+JSF+2+%28and+Maven%29" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/&amp;title=Getting+started+with+JSF+2+%28and+Maven%29" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Getting+started+with+JSF+2+%28and+Maven%29&amp;url=http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/&amp;title=Getting+started+with+JSF+2+%28and+Maven%29" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/&amp;title=Getting+started+with+JSF+2+%28and+Maven%29" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/&amp;bm_description=Getting+started+with+JSF+2+%28and+Maven%29" rel="nofollow" title="Add to&nbsp;Mister Wong"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Add to&nbsp;Mister Wong" alt="Add to&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/&amp;title=Getting+started+with+JSF+2+%28and+Maven%29" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/&amp;title=Getting+started+with+JSF+2+%28and+Maven%29" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.sphere.com/sphereit/http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/" rel="nofollow" title="Add to&nbsp;SphereIt"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/sphereit.png" title="Add to&nbsp;SphereIt" alt="Add to&nbsp;SphereIt" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/&amp;title=Getting+started+with+JSF+2+%28and+Maven%29" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Getting+started+with+JSF+2+%28and+Maven%29+@+http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

