<?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>JavaBeat &#187; JBoss Seam</title>
	<atom:link href="http://www.javabeat.net/category/web-frameworks/jboss-seam/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javabeat.net</link>
	<description>Java Technology News</description>
	<lastBuildDate>Sun, 16 Jun 2013 11:17:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>JBoss Seam Interview Questions</title>
		<link>http://www.javabeat.net/2010/08/jboss-seam-interview-questions/</link>
		<comments>http://www.javabeat.net/2010/08/jboss-seam-interview-questions/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 00:49:13 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[JBoss Seam]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=474</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>Q: What version of JBoss AS do I need to run Seam? A: For Seam 1.3: Seam was developed against JBoss 4.2. Seam can still be run against JBoss 4.0. The seam documentation contains instructions for configuring JBoss 4.0. A: For Seam 1.2: Since Seam requires the latest edition of EJB3, you need to install [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><a id="dd_start"></a><h3><strong><a name="jbossas"></a>Q: What version of JBoss AS do I need to run Seam?</strong></h3>
<p><strong><a name="jbossas"></a></strong></p>
<p><a name="jbossas"></a>A: For Seam 1.3: Seam was developed against <a href="http://sourceforge.net/project/showfiles.php?group_id=22866&amp;package_id=16942&amp;release_id=507793">JBoss 4.2</a>. Seam can still be run against JBoss 4.0. The <a href="http://docs.jboss.com/seam/1.3.0.ALPHA/reference/en/html/gettingstarted.html#d0e2434">seam documentation</a> contains instructions for configuring JBoss 4.0.</p>
<p>A: For Seam 1.2: Since Seam requires the latest edition of EJB3, you need to install JBoss AS from the latest<br />
<a href="http://sourceforge.net/project/showfiles.php?group_id=22866&amp;package_id=193295">JEMS installer</a>. Make sure that you select the &#8220;ejb3&#8243; or &#8220;ejb3+clustering&#8221; profile to include EJB3 support. Also, the <code>jboss-seam.jar</code> library file from the Seam distribution must be included in each Seam application you deploy. Refer to examples in <a href="http://labs.jboss.com/portal/jbossseam/download/index.html">Seam distribution</a> (inside the <code>examples</code> directory) to see how to build and package Seam applications.</p>
<h3><strong><a name="tomcat"></a>Q: Can I run Seam outside of JBoss AS?</strong></h3>
<p><strong><a name="tomcat"></a></strong></p>
<p><a name="tomcat"></a>A: Yes, you can run Seam applications in plain Tomcat 5.5+ or in the Sun GlassFish application server. To run Seam application in Tomcat, you need a number of additional library files and a few configuration files to bootstrap the JBoss EJB3 inside Tomcat. Please refer to the <code>deploy.tomcat</code> ANT build target for the Seam <code>booking</code> example (in the <code>examples/booking</code> directory of the<br />
<a href="http://labs.jboss.com/portal/jbossseam/download/index.html">Seam distribution</a>) for more on how to build a Tomcat WAR for Seam applications. Refer to this <a href="http://weblogs.java.net/blog/bleonard/archive/2006/06/running_jboss_s.html">blog post</a> on how to run Seam in Sun&#8217;s Glassfish application server.</p>
<h3><strong><a name="j2ee"></a>Q: Can I run Seam in a J2EE environment?</strong></h3>
<p> <strong><a name="j2ee"></a></strong></p>
<p><a name="j2ee"></a>A: Yes, as of Seam 1.1, you can use Seam in any J2EE application server, with one caveat: you will not be able to use EJB 3.0 session beans. However, you can use either Hibernate or JPA for persistence,<br />
and you can use Seam JavaBean components instead of session beans. <a name="j2ee"></a><br />
<h3><a name="j2ee"></a><strong><a name="jdk"></a>Q: Can I run Seam with JDK 1.4 and earlier?</strong></h3>
<p><strong><a name="jdk"></a></strong></p>
<p><a name="jdk"></a>A: No, Seam only works on JDK 5.0 and above. It uses annotations and other JDK 5.0 features.</p>
<p><a name="jdk"></a><br />
<h3><a name="jdk"></a><strong><a name="docs"></a>Q: Where can I find Seam examples and<br />
documentation?</strong></h3>
<p> <strong><a name="docs"></a></strong></p>
<p><a name="docs"></a>A: The source code and build script of all Seam example applications are included in the <code>examples</code> directory of the <a href="http://labs.jboss.com/portal/jbossseam/download/index.html">Seam<br />
distribution</a>. Seam documentation is <a href="http://labs.jboss.com/portal/jbossseam/docs">available here</a>.</p>
<h3><strong><a name="forum"></a>Q: Where can I ask questions and make suggestions about<br />
Seam?</strong></h3>
<p><strong><a name="forum"></a></strong></p>
<p><a name="forum"></a>Please use the <a href="http://www.jboss.com/index.html?module=bb&amp;op=viewforum&amp;f=231">Seam User&#8217;s discussion forum</a> for user questions.</p>
<h3><strong><a name="books"></a>Q: Are there books about Seam?</strong></h3>
<p><strong><a name="books"></a></strong></p>
<p><a name="books"></a>A: Yes, Prentice Hall&#8217;s <a href="http://my.safaribooksonline.com/013241273X">&#8220;JBoss Seam:<br />
Simplicity and Power Beyond Java EE 5.0&#8243;</a> is a comprehensive guide for Seam written by JBoss insiders.</p>
<h3><strong><a name="jsf"></a>Q: Is it true that Seam only works with JSF?</strong></h3>
<p> <strong><a name="jsf">/a></strong></p>
<p><a name="jsf"></a>Seam only supports JSF as a view framework at this time. We plan to support other web rameworks in the future. We like JSF because it is a component-based UI framework, which fits really well with Seam&#8217;s  component-based approach to business objects and persistence objects. <strong>Seam made a major improvement to JSF by eliminating almost all XML configuration for backing beans &#8212; you can now define back beans from POJOs or EJB3 components using simple annotations.</strong> We recommend you use <a href="https://facelets.dev.java.net/">Facelets</a>, instead of JSP, with JSF. Facelets provide a powerful templating framework, better appplication performance, and allows us to write much simpler JSF pages. Please see the Seam <code>booking</code> example application for an example on how to use Facelets.</p>
<h3><strong><a name="ajax"></a>Q: Can I use AJAX with Seam?</strong></h3>
<p> <a name="ajax"></a>A: Yes, Seam provides excellent support for AJAX. First, Seam supports the ICEfaces and Ajax4JSF Ajax component libraries for JSF. If you prefer a more &#8220;old fashioned&#8221; approach, Seam provides a complete <a href="http://docs.jboss.com/seam/latest/reference/en/html/remoting.html">JavaScript remoting framework</a> which lets you call Seam components and subscribe to JMS topics directly from the client. Please refer to the Seam <code>remoting</code> example application on how to use AJAX remoting to implement a chat room.</p>
<p>Finally, Seam&#8217;s concurrency model is designed especially for use with Ajax. </p>
<h3><strong><a name="test"></a>Q: Can I unit test Seam applications without starting the Application Server?</strong></h3>
<p> <strong><a name="test"></a></strong></p>
<p><a name="test"></a>Yes, Seam provides its own <a href="http://docs.jboss.com/seam/latest/reference/en/html/testing.html">integration test framework based on TestNG</a>. You can easily mock all Seam services<br />
using those facilities without ever loading an application server or a database. Refer to the  <code>testexample</code> ANT target in the Seam <code>booking</code> example application for more details.</p>
<div class='dd_outer'><div class='dd_inner'><div id='dd_ajax_float'><div class='dd_button_v'><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Fwww.javabeat.net%2Fcategory%2Fweb-frameworks%2Fjboss-seam%2Ffeed%2F" send="false" show_faces="false"  layout="box_count" width="50"  ></fb:like></div><div style='clear:left'></div><div class='dd_button_v'><script type='text/javascript' src='https://apis.google.com/js/plusone.js'></script><g:plusone size='tall' href='http://www.javabeat.net/category/web-frameworks/jboss-seam/feed/'></g:plusone></div><div style='clear:left'></div><div class='dd_button_v'><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.javabeat.net/category/web-frameworks/jboss-seam/feed/" data-count="vertical" data-text="JBoss Seam" data-via="javabeat" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style='clear:left'></div><div class='dd_button_extra_v'><script type="text/javascript">jQuery(document).load(function(){ stLight.options({publisher:'bab47279-62c9-46af-addc-79fd1fe8fee0'}); });</script><div class="st_email_custom"><span id='dd_email_text'>email</span></div></div><div style='clear:left'></div><div class='dd_button_extra_v'><div id='dd_print_button'><span id='dd_print_text'><a href='javascript:window:print()'>print</a></span></div></div><div style='clear:left'></div></div></div></div><script type="text/javascript">var dd_offset_from_content = 44; var dd_top_offset_from_content = 0;</script><script type="text/javascript" src="http://www.javabeat.net/wp-content/plugins/digg-digg//js/diggdigg-floating-bar.js?ver=5.3.0"></script><div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2010/08/jboss-seam-interview-questions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to develop applications using JBoss Seam?</title>
		<link>http://www.javabeat.net/2009/09/seam-2-0-web-development/</link>
		<comments>http://www.javabeat.net/2009/09/seam-2-0-web-development/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 14:32:16 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[JBoss Seam]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=2066</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>Seam 2.x Web DevelopmentThis book provides a complete walk-through of developing Web applications using Seam, Facelets, and RichFaces, and explains how to deploy them to the JBoss Application Server. This book introduces you to the fundamentals of Seam applications, describing topics such as Injection, Outjection, and Bijection. You will understand the Facelets framework, AJAX, database persistence, and advanced Seam [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><center><strong>Seam 2.x Web Development</strong></center>This book provides a complete walk-through of developing Web applications using Seam, Facelets, and RichFaces, and explains how to deploy them to the JBoss Application Server.</p>
<p>This book introduces you to the fundamentals of Seam applications, describing topics such as Injection, Outjection, and Bijection. You will understand the Facelets framework, AJAX, database persistence, and advanced Seam concepts, through the many examples<br />
explained in the book.</p>
<p>A practical approach is taken throughout the book to describe the technologies and tools involved. You will add functionality to Seam applications after you learn how to use the Seam Generator RAD tools and how to customize and fully test application functionality.<br />
Hints and tips on how to use Seam and the JBoss Application Server are provided along the way.</p>
<p><center><strong>What This Book Covers</strong></center><i>Chapter 1</i> provides an overview of the Seam Framework, and describes the benefits that we, as Java developers, will gain by using the framework. We learn that Seam is much more than a framework, and that it comes packed with support for numerous other features.</p>
<p><i>Chapter 2</i> teaches you how to develop applications using Seam, and explains some of the features discussed in previous chapter. In this chapter, we will learn the basic structure of a Seam application and learn more about Seam components. We will also see exactly<br />
how Seam bridges the gap between the Web tier and the Server tier.</p>
<p><i>Chapter 3</i> takes you through page navigation using Seam page flows. We also see how we can define page flow within the pages.xml file of our applications by defining both static rules and rules encompassing JSF expression language.</p>
<p><i>Chapter 4</i> shows you why Facelets is recommended, and what it offers over JSP. We&#8217;ll also look at the SeamGen tool and see why this is a much better way of creating Seam applications and building scripts. Finally, we bring both SeamGen and Facelets together and re-create the Vacation Planner project that we wrote in Chapter 3, (realizing in the course of doing so how much easier and more advanced it is), using these technologies</p>
<p>In <i>Chapter 5</i>, we take a look at TestNG and see how it can be used to build sets of test suites. We have a look at how Seam provides excellent facilities by allowing us to perform testing of our applications, at the class, Seam component, and user interface levels.</p>
<p>In <i>Chapter 6</i>, we take a look at RichFaces and learn that it&#8217;s a JSF component library that allows us to easily build rich user interfaces within web applications. This chapter explains how Seam is fully integrated with RichFaces, and that applications generated by<br />
SeamGen require no special configuration to allow RichFaces to be used. This chapter also lists all of the components available within RichFaces.</p>
<p><i>Chapter 7</i> introduces database persistence with Seam. We take a closer look at how SeamGen helps us to create and configure our applications, allowing test, development, and production profiles to be configured.</p>
<p><i>Chapter 8</i> takes a closer look at what exactly a conversational application is and why they make web application development easier and more functional. We also take a look at different component scopes and see why they are important to Seam.</p>
<p>In <i>Chapter 9</i>, we take a look at AJAX and how it can be applied to Seam applications. We also take a closer look at the two different AJAX technologies within the Seam framework: Seam Remoting and AJAX4JSF.</p>
<p><i>Chapter 10</i> discusses how security is implemented within a Seam application highlighting the most common features, such as logon pages, user access rights, CAPTCHAs, and so on, which are used to secure Seam applications. We also highlight some of the more advanced security features that can be used.</p>
<p><i>Chapter 11</i> walks through some of the more advanced features of Seam, such as internationalization, URL rewriting, PDF document generation, and so on, and also discusses how these can be used within our web applications.</p>
<h2 style="text-align: center;"><span style="text-decoration: underline;"><span style="color: #000080; text-decoration: underline;">Developing Seam Applications</span></span></h2>
<p>In the previous chapter, we learned what the Seam Framework is, and what it offers to Java developers. In this chapter, we are going to start learning how to develop applications using Seam, and we will see some of the features we had discussed previously. In this chapter, we will learn the basic structure of a Seam application. We will see in practice how Seam Injection and Outjection work, and we will learn more about Seam components. We will also see exactly how Seam bridges the gap between the Web tier (using Java Server Faces) and the Server tier (using Enterprise Java Beans).</p>
<h2>Seam application architecture</h2>
<pre class="brush: java; title: ; notranslate">	As most enterprise Java developers are probably familiar with JSF and
	JSP, we will be using this as the view technology for our sample
	applications, until we have a solid understanding of Seam. In Chapter 4,
	we will introduce Facelets as the recommended view technology for
	Seam-based applications.
</pre>
<p>In a standard Java EE application, <b>Enterprise Application Resource</b> (EAR) files contain one or more <b>Web Application Resource</b> (WAR) files and one or more sets of <b>Java Archive</b> (JAR) files containing <b>Enterprise JavaBeans</b> (EJB) functionality.<br />
Seam applications are generally deployed in exactly the same manner as depicted in the following diagram. It is possible to deploy Seam onto a servlet-only container (for example, Tomcat) and use POJOs as the server-side Seam components. However, in this situation, we don&#8217;t get any of the benefits that EJBs provide, such as security, transaction handling, management, or pooling.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-1.gif"><img class="aligncenter size-full wp-image-6655" alt="seam-1" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-1.gif" width="264" height="179" /></a></center></p>
<h2>Seam components</h2>
<p>Within Seam, components are simple POJOs. There is no need to implement any interfaces or derive classes from a Seam-specific base class to make Seam components classes. For example, a Seam component could be:</p>
<ul>
<li>A simple POJO</li>
<li>a stateless Session Bean</li>
<li>a stateful Session Bean</li>
<li>a JPA entity and so on</li>
</ul>
<p>Seam components are defined by adding the @Name annotation to a class definition. The @Name annotation takes a single parameter to define the name of the Seam component. The following example shows how a stateless Session Bean is defined as a Seam component called calcAction.</p>
<pre class="brush: java; title: ; notranslate">	package com.davidsalter.seamcalculator;
	@Stateless
	@Name(&quot;calcAction&quot;)
	public class CalcAction implements Calc {
		...
	}
</pre>
<p>When a Seam application is deployed to JBoss, the log output at startup lists what Seam components are deployed, and what type they are. This can be useful for diagnostic purposes, to ensure that your components are deployed correctly. Output similar to the following will be shown in the JBoss console log when the CalcAction class is deployed:</p>
<pre class="brush: java; title: ; notranslate">&lt;b&gt;	21:24:24,097 INFO [Initialization] Installing components...
	21:24:24,121 INFO [Component] Component: calcAction, scope:
	STATELESS, type: STATELESS_SESSION_BEAN, class: com.davidsalter.
	seamcalculator.CalcAction, JNDI: SeamCalculator/CalcAction/local
&lt;/b&gt;</pre>
<h2>Object Injection and Outjection</h2>
<p>One of the benefits of using Seam is that it acts as the &#8220;glue&#8221; between the web technology and the server-side technology. By this we mean that the Seam Framework allows us to use enterprise beans (for example, Session Beans) directly within the Web tier without having to use <b>Data Transfer Object (DTO)</b> patterns and without worrying about exposing server-side functionality on the client.</p>
<p>Additionally, if we are using Session Beans for our server-side functionality, we don&#8217;t really have to develop an additional layer of JSF backing beans, which are essentially acting as another layer between our web page and our application logic.</p>
<p>In order to fully understand the benefits of Seam, we need to first describe what we mean by Injection and Outjection.</p>
<p>Injection is the process of the framework setting component values before an object is created. With injection, the framework is responsible for setting components (or injecting them) within other components. Typically, Injection can be used to allow component values to be passed from the web page into Seam components.</p>
<p>Outjection works in the opposite direction to Injection. With Outjection, components are responsible for setting component values back into the framework. Typically, Outjection is used for setting component values back into the Seam Framework, and these values can then be referenced via JSF <b>Expression Language (EL)</b> within JSF pages. This means that Outjection is typically used to allow data values to be passed from Seam components into web pages.</p>
<p>Seam allows components to be injected into different Seam components by using the @In annotation and allows us to outject them by using the @Out annotation. For example, if we have some JSF code that allows us to enter details on a web form, we may use an tag such as this:</p>
<pre class="brush: java; title: ; notranslate">	&lt;h:inputText value=&quot;#{calculator.value1}&quot; required=&quot;true&quot;/&gt;
</pre>
<p>The Seam component calculator could then be injected into a Seam component using the @In annotation as follows:</p>
<pre class="brush: java; title: ; notranslate">	@In
	private Calculator calculator;
</pre>
<p>With Seam, all of the default values on annotations are the most likely ones to be used. In the preceding example therefore, Seam will look up a component called calculator and inject that into the calculator variable. If we wanted Seam to inject a variable with a different name to the variable that it is being injected into, we can adorn the @In annotation with the value parameter.</p>
<pre class="brush: java; title: ; notranslate">	@In (value=&quot;myCalculator&quot;)
	private Calculator calculator;
</pre>
<p>In this example, Seam will look up a component called myCalculator and inject it into the variable calculator.</p>
<p>Similarly, if we want to outject a variable from a Seam component into a JSF page, we would use the @Out annotation.</p>
<pre class="brush: java; title: ; notranslate">	@Out
	private Calculator calculator;
</pre>
<p>The outjected calculator object could then be used in a JSF page in the following manner:</p>
<pre class="brush: java; title: ; notranslate">	&lt;h:outputText value=&quot;#{calculator.answer}&quot;/&gt;
</pre>
<h2>Example application</h2>
<p>To see these concepts in action, and to gain an understanding of how Seam components are used instead of JSF backing beans, let us look at a simple calculator web application. This simple application allows us to enter two numbers on a web page. Clicking the <b>Add</b> button on the web page will cause the sum of the numbers to be displayed.</p>
<p>This basic application will give us an understanding of the layout of a Seam application and how we can inject and outject components between the business layer and the view. The application functionality is shown in the following screenshot.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-2.gif"><img class="aligncenter size-full wp-image-6656" alt="seam-2" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-2.gif" width="469" height="165" /></a></center>&nbsp;</p>
<pre class="brush: java; title: ; notranslate">
	The sample code for this application can be downloaded from the
	Packt web site, at http://www.packtpub.com/support.
</pre>
<p>For this sample application, we have a single JSF page that is responsible for:</p>
<ul>
<li>Reading two numeric values from the user</li>
<li>Invoking business logic to add the numbers together</li>
<li>Displaying the results of adding the numbers together</li>
</ul>
<pre class="brush: java; title: ; notranslate">
	&lt;%@ taglib uri=&quot;http://java.sun.com/jsf/html&quot; prefix=&quot;h&quot; %&gt;
	&lt;%@ taglib uri=&quot;http://java.sun.com/jsf/core&quot; prefix=&quot;f&quot; %&gt;
	&lt;html&gt;
		&lt;head&gt;
			&lt;title&gt;Seam Calculator&lt;/title&gt;
		&lt;/head&gt;
		&lt;body&gt;
			&lt;f:view&gt;
				&lt;h:form&gt;
					&lt;h:panelGrid columns=&quot;2&quot;&gt;
						Value 1: &lt;h:inputText value=&quot;#{calculator.
							value1}&quot; /&gt;
						Value 2: &lt;h:inputText value=&quot;#{calculator.
							value2}&quot; /&gt;
						Add them together gives: &lt;h:outputText value=&quot;
							#{calculator.answer} &quot;/&gt;
					&lt;/h:panelGrid&gt;
					&lt;h:commandButton value=&quot;Add&quot; action=
						&quot;#{calcAction.calculate}&quot;/&gt;
				&lt;/h:form&gt;
			&lt;/f:view&gt;
		&lt;/body&gt;
	&lt;/html&gt;
</pre>
<p>We can see that there is nothing Seam-specific in this JSF page. However, we are binding two inputText areas, one outputText area, and a button action to Seam components by using standard JSF Expression Language.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-3.gif"><img class="aligncenter  wp-image-6657" alt="seam-3" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-3.gif" width="567" height="224" /></a></center>Our business logic for this sample application is performed in a simple POJO class called Calculator.java.</p>
<pre class="brush: java; title: ; notranslate">
	package com.davidsalter.seamcalculator;

	import java.io.Serializable;

&lt;b&gt;	import org.jboss.seam.annotations.Name;

	@Name(&quot;calculator&quot;)&lt;/b&gt;

	public class Calculator {
		private double value1;
		private double value2;
		private double answer;
		public double getValue1() {
			return value1;
		}
		public void setValue1(double value1) {
			this.value1 = value1;
		}
		public double getValue2() {
			return value2;
		}
		public void setValue2(double value2) {
			this.value2 = value2;
		}
		public double getAnswer() {
			return answer;
		}
		public void add() {
			this.answer = value1 + value2;
		}
	}
</pre>
<p>This class is decorated with the @Name(&#8220;calculator&#8221;) annotation, which causes it to be registered to Seam with the name, &#8220;calculator&#8221;. The @Name annotation causes this object to be registered as a Seam component that can subsequently be used within other Seam components via Injection or Outjection by using the @In and @Out annotations.</p>
<p>Finally, we need to have a class that is acting as a backing bean for the JSF page that allows us to invoke our business logic. In this example, we are using a Stateless Session Bean. The Session Bean and its local interface are as follows.</p>
<p>In the Java EE 5 specification, a Stateless Session Bean is used to represent a single application client&#8217;s communication with an application server. A Stateless Session Bean, as its name suggests, contains no state information; so they are typically used as transaction façades. A <b>Façade</b> is a popular design pattern, which defines how simplified access to a system can be provided. For more information about the Façade pattern, check out the following link:</p>
<pre class="brush: java; title: ; notranslate">http://java.sun.com/blueprints/corej2eepatterns/Patterns/
SessionFacade.html
</pre>
<p>Defining a Stateless Session Bean using Java EE 5 technologies requires an interface and an implementation class to be defined. The interface defines all of the methods that are available to clients of the Session Bean, whereas the implementation class contains a concrete implementation of the interface. In Java EE 5, a Session Bean interface is annotated with either the @Local or @Remote or both annotations. An @Local interface is used when a Session Bean is to be accessed locally within the same JVM as its client (for example, a web page running within an application server). An @Remote interface is used when a Session Bean&#8217;s clients are remote to the application server that is running within a different JVM as the application server.</p>
<p>There are many books that cover Stateless Session Beans and EJB 3 in depth, such as <i>EJB 3 Developer&#8217;s Guide</i> by Michael Sikora, published by Packt Publishing. For more information on this book, check out the following link:</p>
<pre class="brush: java; title: ; notranslate">http://www.packtpub.com/developer-guide-for-ejb3
</pre>
<p>In the following code, we are registering our CalcAction class with Seam under the name calcAction. We are also Injecting and Outjecting the calculator variable so that we can use it both to retrieve values from our JSF form and pass them back to the form.</p>
<pre class="brush: java; title: ; notranslate">	package com.davidsalter.seamcalculator;
	import javax.ejb.Stateless;
	import org.jboss.seam.annotations.In;
	import org.jboss.seam.annotations.Out;
	import org.jboss.seam.annotations.Name;

	@Stateless
	&lt;b&gt;@Name(&quot;calcAction&quot;)&lt;/b&gt;
	public class CalcAction implements Calc {
		&lt;b&gt;@In
		@Out&lt;/b&gt;
		private Calculator calculator;
		public String calculate() {
			calculator.add();
			return &quot;&quot;;
		}
	}
	package com.davidsalter.seamcalculator;
	import javax.ejb.Local;
	@Local
	public interface Calc {
		public String calculate();
	}
</pre>
<p>That&#8217;s all the code we need to write for our sample application. If we review this code, we can see several key points where Seam has made our application development easier:</p>
<ul>
<li>All of the code that we have developed has been written as POJOs, which will make unit testing a lot easier.</li>
<li>We haven&#8217;t extended or implemented any special Seam interfaces.</li>
<li>We&#8217;ve not had to define any JSF backing beans explicitly in XML.</li>
<li>We&#8217;re using Java EE Session Beans to manage all of the business logic and web-tier/business-tier integration.</li>
<li>We&#8217;ve not used any DTO objects to transfer data between the web and the business tiers. We&#8217;re using a Seam component that contains both state and behavior.</li>
</ul>
<p>If you are familiar with JSF, you can probably see that adding Seam into a fairly standard JSF application has already made our development simpler.</p>
<p>Finally, to enable Seam to correctly find our Seam components and deploy them correctly to the application server, we need to create an empty file called seam. properties and place it within the root of the classpath of the EJB JAR file. Because this file is empty, we will not discuss it further here. We will, however, see the location of this file in the section <i>Application layout</i>, later in this chapter.</p>
<p>To deploy the application as a WAR file embedded inside an EAR file, we need to write some deployment descriptors.</p>
<h2>WAR file deployment descriptors</h2>
<p>To deploy a WAR file with Seam, we need to define several files within the \WEB-INF directory of the archive:</p>
<ul>
<li>web.xml</li>
<li>faces-config.xml</li>
<li>components.xml</li>
</ul>
<p>First let&#8217;s take a look at the web.xml file. This is a fairly standard file that specifies the SeamListener class, which aids internal Seam initialization and cleanup. This file also specifies that all pages with the extension .seam will be run as JSF pages.</p>
<pre class="brush: java; title: ; notranslate">	&lt;?xml version=&quot;1.0&quot; ?&gt;
	&lt;web-app xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
			xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
			xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;

			version=&quot;2.5&quot;&gt;
		&lt;listener&gt;
			&lt;listener-class&gt;org.jboss.seam.servlet.
				SeamListener&lt;/listener-class&gt;
		&lt;/listener&gt;
		&lt;context-param&gt;
			&lt;param-name&gt;javax.faces.DEFAULT_SUFFIX&lt;/param-name&gt;
			&lt;param-value&gt;.jsp&lt;/param-value&gt;
		&lt;/context-param&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;*.seam&lt;/url-pattern&gt;
		&lt;/servlet-mapping&gt;
	&lt;/web-app&gt;
</pre>
<p>The faces-config.xml file allows Seam to be integrated into the JSF page life cycle, by adding the org.jboss.seam.jsf.SeamPhaseListener into the life cycle events chain.</p>
<pre class="brush: java; title: ; notranslate">	&lt;?xml version='1.0' encoding='UTF-8'?&gt;
	&lt;faces-config version=&quot;1.2&quot;
			xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
			xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
			xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd&quot;&gt;

		&lt;lifecycle&gt;
			&lt;phase-listener&gt;org.jboss.seam.jsf.
				SeamPhaseListener&lt;/phase-listener&gt;
		&lt;/lifecycle&gt;
	&lt;/faces-config&gt;
</pre>
<p>Finally, the components.xml file allows any Seam components that we may be using to be configured. In our sample application, the only configuration that we are performing is specifying the JNDI pattern to allow EJB components to be looked up by Seam.</p>
<pre class="brush: java; title: ; notranslate">	&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
	&lt;components xmlns=&quot;http://jboss.com/products/seam/components&quot;
			xmlns:core=&quot;http://jboss.com/products/seam/core&quot;
			xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
			xsi:schemaLocation=&quot;http://jboss.com/products/seam/core

http://jboss.com/products/seam/core-2.1.xsd


http://jboss.com/products/seam/components


http://jboss.com/products/seam/components-2.1.xsd&quot;&gt;

		&lt;core:init jndi-pattern=&quot;@jndiPattern@&quot;/&gt;
	&lt;/components&gt;
</pre>
<p>In the later chapters, we will look in detail at the components.xml file, as it is in this file that we will specify Seam-specific information such as security and web page viewing options.</p>
<h2>EAR file deployment descriptors</h2>
<p>To deploy an EAR file with Seam on JBoss, we need to define several deployment descriptors within the \META-INF directory of the archive:</p>
<ul>
<li>application.xml</li>
<li>ejb-jar.xml</li>
<li>jboss-app.xml</li>
</ul>
<p>The application.xml file allows us to define the modules that are to be deployed to JBoss. In this file, we need to specify the WAR file for our application and the JAR file that contains our Session Beans. We must also include a reference to the jboss-seam.jar file.</p>
<pre class="brush: java; title: ; notranslate">	&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
	&lt;application xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
			xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
			xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/application_5.xsd&quot;

			version=&quot;5&quot;&gt;
		&lt;display-name&gt;SeamCalculator&lt;/display-name&gt;
		&lt;module&gt;
			&lt;web&gt;
				&lt;web-uri&gt;SeamCalculator.war&lt;/web-uri&gt;
				&lt;context-root&gt;/SeamCalculator&lt;/context-root&gt;
			&lt;/web&gt;
		&lt;/module&gt;
		&lt;module&gt;
			&lt;ejb&gt;SeamCalculator.jar&lt;/ejb&gt;
		&lt;/module&gt;
		&lt;module&gt;
			&lt;ejb&gt;jboss-seam.jar&lt;/ejb&gt;
		&lt;/module&gt;
	&lt;/application&gt;
</pre>
<p>In the ejb-jar.xml file, we must attach the org.jboss.seam.ejb.SeamInterceptor class onto all our EJBs.</p>
<pre class="brush: java; title: ; notranslate">	&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
	&lt;ejb-jar xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
			xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
			xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd&quot;

			version=&quot;3.0&quot;&gt;
		&lt;interceptors&gt;
			&lt;interceptor&gt;
				&lt;interceptor-class&gt;org.jboss.seam.ejb.
					SeamInterceptor&lt;/interceptor-class&gt;
			&lt;/interceptor&gt;
		&lt;/interceptors&gt;
		&lt;assembly-descriptor&gt;
			&lt;interceptor-binding&gt;
				&lt;ejb-name&gt;*&lt;/ejb-name&gt;
				&lt;interceptor-class&gt;org.jboss.seam.ejb.
					SeamInterceptor&lt;/interceptor-class&gt;
			&lt;/interceptor-binding&gt;
		&lt;/assembly-descriptor&gt;
	&lt;/ejb-jar&gt;
</pre>
<p>Finally, we can specify a class loader for our Seam application in jboss-app.xml so that there are no confl icts between the classes deployed to the JBoss Application Server and the ones deployed to our application.</p>
<pre class="brush: java; title: ; notranslate">	&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
	&lt;!DOCTYPE jboss-app
		PUBLIC &quot;-//JBoss//DTD J2EE Application 5.0//EN&quot;
		&quot;http://www.jboss.org/j2ee/dtd/jboss-app_5_0.dtd&quot;&gt;
		&lt;jboss-app&gt;
			&lt;loader-repository&gt;
				seam.jboss.org:loader=SeamCalculator
			&lt;/loader-repository&gt;
		&lt;/jboss-app&gt;
</pre>
<h2>Application layout</h2>
<p>Before we deploy and run the test application, let&#8217;s take a look at the layout of our sample application. As you can see in the following screenshot, the layout of the application follows the standard layout of a Java EE application. The application is deployed as a single EAR file. Within the EAR file, we have the Seam JAR file, our EJB JAR file, and our web application EAR file. This is the standard layout that all of our Seam applications are going to follow.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-4.gif"><img class="aligncenter size-full wp-image-6658" alt="seam-4" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-4.gif" width="324" height="359" /></a></center></p>
<h2>Testing the application</h2>
<p>Now that we have looked at the code for both the application and the deployment descriptors, let&#8217;s build the application and test it before deploying it to the JBoss Application Server.</p>
<p>It is traditionally quite difficult to perform unit tests on enterprise applications because of their nature. Without using Seam, the testing of web pages and Java EE resources (for example, Session Beans) requires that the application be deployed to an application server. This makes testing more difficult and can vastly increase the development time of applications.</p>
<p>However, Seam offers great support for testing applications outside the application server, including support for testing web pages. The standard testing framework used with Seam is <b>TestNG</b> (http://www.testng.org). However, there are some Seam-specific extensions that make testing easier. We&#8217;ll cover the full testing of Seam applications in a later chapter, but for now, let&#8217;s see how to test the business logicof our application, and indeed any POJOs that we can write, by using a standard TestNG unit test.</p>
<p>TestNG uses Java 5 annotations to allow methods to be declared as tests. To declare a method as a unit test, we simply need to adorn the method with the @Test annotation. Within a test, we can then assert different conditions to test whether our code is working as expected.</p>
<p>We could write a simple test for our calculator application as follows:</p>
<pre class="brush: java; title: ; notranslate">	package com.davidsalter.seamcalculator.test;
	import com.davidsalter.seamcalculator.Calculator;
	import org.testng.annotations.Test;
	public class CalculatorTest {
		private static double tolerance = 0.001;
		@Test
		public void testAdd() throws Exception {
			Calculator testCalculator = new Calculator();
			testCalculator.setValue1(10.0);
			testCalculator.setValue2(20.0);
			testCalculator.add();
			assert (testCalculator.getAnswer() - 30.0 &lt; tolerance);
		}
	}
</pre>
<p>This test simply instantiates an instance of the Calculator class, sets up some test data, and invokes the add() method before asserting that the answer is as expected.</p>
<p>Before we can run this unit test, we must also define an XML file that details which tests to run. For Seam tests, this file is typically called AllTests. xml. The contents of this file, for our sample application, are as follows:</p>
<pre class="brush: java; title: ; notranslate">	&lt;!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot; &gt;
	&lt;suite name=&quot;Calculator - All&quot; verbose=&quot;1&quot;&gt;
		&lt;test name=&quot;Calculator&quot;&gt;
			&lt;packages&gt;
				&lt;package name=&quot;com.davidsalter.seamcalculator.test&quot;/&gt;
			&lt;/packages&gt;
		&lt;/test&gt;
	&lt;/suite&gt;
</pre>
<p>Finally, before we can build and test the application, we need to copy a few JAR files from the Seam installation into our build path. To build and test a basic Seam application, we need to build against some of the Seam JAR files:</p>
<ul>
<li>jboss-seam.jar – This contains the main Seam Framework such as the @In and @Out annotations</li>
<li>ejb-api.jar – This contains the references we need to build EJB 3 Session Beans</li>
<li>testng.jar – This contains the TestNG framework that allows us to test our application</li>
</ul>
<p>All of these JAR files can be found in the &lt;jboss_seam&gt;/lib directory, where &lt;jboss_seam&gt; is the directory that you have installed Seam into. If you have taken the sample application from the download bundle for this chapter, copy these files into the /lib directory of the project.</p>
<p>To test the sample application, we need to execute the Test target in the ant build file, as shown in the following screenshot.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-5.gif"><img class="aligncenter  wp-image-6659" alt="seam-5" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-5.gif" width="521" height="258" /></a></center>Executing the tests should show that one test has been run, and that there were no failures and no skipped tests. Assuming that the test was completed successfully, we can be fairly confident that our application will run correctly when built and deployed to the application server.</p>
<h2>Building and deploying the application</h2>
<p>In the previous section, we saw how to perform a basic unit test to verify that the application logic is functioning correctly. Once all of our application&#8217;s unit tests succeed (one of them in this case), we can build the application and deploy it to JBoss.</p>
<p>To build and deploy the application, we use the ant script again. The ant script provides several different targets, allowing the project to be compiled, deployed, and archived into an EAR file. Executing the Info target provides the following help screen, which shows which targets are available.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-6.gif"><img class="aligncenter  wp-image-6660" alt="seam-6" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-6.gif" width="527" height="217" /></a></center>&nbsp;</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-7.gif"><img class="aligncenter  wp-image-6661" alt="seam-7" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-7.gif" width="494" height="241" /></a></center>&nbsp;</p>
<pre class="brush: java; title: ; notranslate">
	Within the JBoss Application Server, applications can be hot deployed
	by copying them into the &lt;jboss_home&gt;/server/default/deploy
	directory. Similarly, to undeploy an application, just delete it from the
	&lt;jboss_home&gt;/server/default/deploy directory.
</pre>
<p>To build and deploy the application to JBoss, execute the deploy task.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-8.gif"><img class="aligncenter  wp-image-6662" alt="seam-8" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-8.gif" width="482" height="327" /></a></center>If the JBoss Application Server is running, we should see some log output appear on the JBoss console, ending with a line saying that the application has been deployed successfully, as shown below:</p>
<pre class="brush: java; title: ; notranslate">	[EARDeployer] Started J2EE application: file:/Applications/
	jboss-5.0.0.GA/server/default/deploy/SeamCalculator.ear

			If we don't see the application deployed to the JBoss Application Server
			and there were no errors during compilation, check the value of the
			jboss.home property within the build.properties file.
</pre>
<p>Once we see the preceding line on the JBoss console, it means that JBoss has successfully deployed the application and we can test it out.</p>
<p>To run the application, visit the web site http://localhost:8080/SeamCalculator/calc.seam and try adding some numbers together to prove that the application is functioning as expected.</p>
<h2>Seam data validation</h2>
<p>So far we&#8217;ve looked at Seam and learned how Seam can act as JSF backing beans and how it can act as the glue between our server-tier Session Beans and our web-tier JSF pages. Unfortunately, though, users could easily break our sample application by entering invalid data (for example, entering blank values or non-numeric values into the edit boxes). Seam provides validation tools to help us to make our application more robust and provide feedback to our users. Let&#8217;s look at these tools now.</p>
<h2>Data validation</h2>
<p>In order to perform consistent data validation, we would ideally want to perform all data validation within our data model. We want to perform data validation in our data model so that we can then keep all of the validation code in one place, which should then make it easier to keep it up-to-date if we ever change our minds about allowable data values.</p>
<p>Seam makes extensive use of the <b>Hibernate validation</b> tools to perform validation of our domain model. The Hibernate validation tools grew from the <b>Hibernate </b>project (http://www.hibernate.org) to allow the validation of entities before they are persisted to the database. To use the Hibernate validation tools in an application, we need to add hibernate-validator.jar into the application&#8217;s class path, after which we can use annotations to define the validation that we want to use for our data model. We&#8217;ll discuss data validation and the Hibernate validator further in Chapter 7, when we talk about persisting objects to the database. But for now, let&#8217;s look at a few validations that we can add to our sample Seam Calculator application.</p>
<p>In order to implement data validation with Seam, we need to apply annotations either to the member variables in a class or to the getter of the member variables. It&#8217;s good practice to always apply these annotations to the same place in a class. Hence, throughout this book, we will always apply our annotation to the getter methods within classes.</p>
<p>In our sample application, we are allowing numeric values to be entered via edit boxes on a JSF form. To perform data validation against these inputs, there are a few annotations that can help us.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-9.gif"><img class="aligncenter  wp-image-6663" alt="seam-9" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-9.gif" width="500" height="327" /></a></center>At this point, you may be wondering why we need to have an @Range validator, when by combining the @Min and @Max validators, we can get a similar effect. If you want a different error message to be displayed when a variable is set above its maximum value as compared to the error message that is displayed when it is set below its minimum value, then the @Min and @Max annotations should be used. If you are happy with the same error message being displayed when a variable is set outside its minimum or maximum values, then the @Range validator should be used. Effectively, the @Min and @Max validators are providing a finer level of error message<br />
provision than the @Range validator.</p>
<p>The following code sample shows how these annotations can be applied to the sample application that we have written so far in this chapter, to add basic data validation to our user inputs.</p>
<pre class="brush: java; title: ; notranslate">	package com.davidsalter.seamcalculator;
	import java.io.Serializable;
	import org.jboss.seam.annotations.Name;
	import org.jboss.seam.faces.FacesMessages;
&lt;b&gt;	import org.hibernate.validator.Max;
	import org.hibernate.validator.Min;
	import org.hibernate.validator.Range;&lt;/b&gt;

	@Name(&quot;calculator&quot;)
	public class Calculator implements Serializable {
		private double value1;
		private double value2;
		private double answer;

		&lt;b&gt;@Min(value=0)
		@Max(value=100)&lt;/b&gt;
		public double getValue1() {
			return value1;
		}

		public void setValue1(double value1) {
			this.value1 = value1;
		}

		&lt;b&gt;@Range(min=0, max=100)&lt;/b&gt;
		public double getValue2() {
			return value2;
		}

		public void setValue2(double value2) {
			this.value2 = value2;
		}

		public double getAnswer() {
			return answer;
		}
		…
	}
</pre>
<h3>Displaying errors to the user</h3>
<p>In the previous section, we saw how to add data validation to our source code to stop invalid data from being entered into our domain model. Now that we have reached a level of data validation, we need to provide feedback to the user to inform them of any invalid data that they have entered.</p>
<p>JSF applications have the concept of messages that can be displayed associated with different components. For example, if we have a form asking for a date of birth to be entered, we could display a message next to the entry edit box if an invalid date were entered. JSF maintains a collection of these error messages, and the simplest way of providing feedback to the user is to display a list of all of the error messages that were generated as a part of the previous operation.</p>
<p>In order to obtain error messages within the JSF page, we need to tell JSF which components we want to be validated against the domain model. This is achieved by using the or tags. These are Seam-specific tags and are not a part of the standard JSF runtime. In order to use these tags, we need to add the following taglib reference to the top of the JSF page.</p>
<pre class="brush: java; title: ; notranslate">	&lt;%@ taglib uri=&quot;http://jboss.com/products/seam/taglib&quot; prefix=&quot;s&quot; %&gt;
</pre>
<p>In order to use this tag library, we need to add a few additional JAR files into the WEB-INF/lib directory of our web application, namely:</p>
<ul>
<li>jboss-el.jar</li>
<li>jboss-seam-ui.jar</li>
<li>jsf-api.jar</li>
<li>jsf-impl.jar</li>
</ul>
<p>This tag library allows us to validate all of the components () within a block of JSF code, or individual components () within a JSF page.</p>
<p>To validate all components within a particular scope, wrap them all with the  tag as shown here:</p>
<pre class="brush: java; title: ; notranslate">	&lt;h:form&gt;
		&lt;s:validateAll&gt;
			&lt;h:inputText value=&quot;…&quot; /&gt;
			&lt;h:inputText value=&quot;…&quot; /&gt;
		&lt;/s:validateAll&gt;
	&lt;/h:form&gt;
</pre>
<p>To validate individual components, embed the tag within the component, as shown in the following code fragment.</p>
<pre class="brush: java; title: ; notranslate">	&lt;h:form&gt;
		&lt;h:inputText value=&quot;…&quot; &gt;
			&lt;s:validate/&gt;
		&lt;/h:inputText&gt;
		&lt;h:inputText value=&quot;…&quot; &gt;
			&lt;s:validate/&gt;
		&lt;/h:inputText&gt;
	&lt;/h:form&gt;
</pre>
<p>After specifying that we wish validation to occur against a specified set of controls, we can display error messages to the user. JSF maintains a collection of errors on a page, which can be displayed in its entirety to a user via the tag.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-10.gif"><img class="aligncenter  wp-image-6664" alt="seam-10" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-10.gif" width="462" height="162" /></a></center>It can sometimes be useful to show a list of all of the errors on a page, but it isn&#8217;t very useful to the user as it is impossible for them to say which error relates to which control on the form. Seam provides some additional support at this point to allow us to specify the formatting of a control to indicate error or warning messages to users.</p>
<p>Seam provides three different JSF facets () to allow HTML to be specified both before and after the offending input, along with a CSS style for the HTML. Within these facets, the tag can be used to output the message itself. This tag could be applied either before or after the input box, as per requirements.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-11a.gif"><img class="aligncenter  wp-image-6665" alt="seam-11a" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-11a.gif" width="495" height="209" /></a></center><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-12.gif"><img class="aligncenter  wp-image-6666" alt="seam-12" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-12.gif" width="436" height="293" /></a></center>In order to specify these facets for a particular field, the tag must be specified outside the facet scope.</p>
<pre class="brush: java; title: ; notranslate">	&lt;s:decorate&gt;
		&lt;f:facet name=&quot;aroundInvalidField&quot;&gt;
			&lt;s:span styleClass=&quot;invalidInput&quot;/&gt;
		&lt;/f:facet&gt;
		&lt;f:facet name=&quot;beforeInvalidField&quot;&gt;
			&lt;f:verbatim&gt;**&lt;/f:verbatim&gt;
		&lt;/f:facet&gt;
		&lt;f:facet name=&quot;afterInvalidField&quot;&gt;
			&lt;s:message/&gt;
		&lt;/f:facet&gt;
		&lt;h:inputText value=&quot;#{calculator.value1}&quot; required=&quot;true&quot; &gt;
			&lt;s:validate/&gt;
		&lt;/h:inputText&gt;
	&lt;/s:decorate&gt;
</pre>
<p>In the preceding code snippet, we can see that a CSS style called invalidInput is being applied to any error or warning information that is to be displayed regarding the field. An erroneous input field is being adorned with a double asterisk (**) preceding the edit box, and the error message specific to the inputText field after is displayed in the edit box.</p>
<p>Applying these features to our sample application, the calc.jsp file will appear as follows:</p>
<pre class="brush: java; title: ; notranslate">	&lt;%@ taglib uri=&quot;http://java.sun.com/jsf/html&quot; prefix=&quot;h&quot; %&gt;
	&lt;%@ taglib uri=&quot;http://java.sun.com/jsf/core&quot; prefix=&quot;f&quot; %&gt;
	&lt;%@	taglib uri=&quot;http://jboss.com/products/seam/taglib&quot; prefix=&quot;s&quot; %&gt;

	&lt;html&gt;
		&lt;head&gt;
			&lt;title&gt;Seam Calculator&lt;/title&gt;
			&lt;link href=&quot;styles.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
		&lt;/head&gt;
		&lt;body&gt;
			&lt;f:view&gt;
				&lt;h:form&gt;
					&lt;h:panelGrid columns=&quot;2&quot;&gt;
						Value 1:
						&lt;s:decorate&gt;
							&lt;f:facet name=&quot;aroundInvalidField&quot;&gt;
								&lt;s:span styleClass=&quot;invalidInput&quot;/&gt;
							&lt;/f:facet&gt;
							&lt;f:facet name=&quot;beforeInvalidField&quot;&gt;
								&lt;f:verbatim&gt;**&lt;/f:verbatim&gt;
							&lt;/f:facet&gt;
							&lt;f:facet name=&quot;afterInvalidField&quot;&gt;
								&lt;s:message/&gt;
							&lt;/f:facet&gt;
							&lt;h:inputText value=&quot;#{calculator.value1}&quot;
									required=&quot;true&quot; &gt;
								&lt;s:validate/&gt;
							&lt;/h:inputText&gt;
						&lt;/s:decorate&gt;
						Value 2:
						&lt;s:decorate&gt;
							&lt;f:facet name=&quot;aroundInvalidField&quot;&gt;
								&lt;s:span styleClass=&quot;invalidInput&quot;/&gt;
							&lt;/f:facet&gt;
							&lt;f:facet name=&quot;beforeInvalidField&quot;&gt;
								&lt;f:verbatim&gt;**&lt;/f:verbatim&gt;
							&lt;/f:facet&gt;
							&lt;f:facet name=&quot;afterInvalidField&quot;&gt;
								&lt;s:message/&gt;
							&lt;/f:facet&gt;
							&lt;h:inputText value=&quot;#{calculator.value2}&quot;
									required=&quot;true&quot;&gt;
								&lt;s:validate/&gt;
							&lt;/h:inputText&gt;
						&lt;/s:decorate&gt;
						Adding them together gives:
						&lt;h:outputText value=&quot;#{calculator.answer}&quot;/&gt;
					&lt;/h:panelGrid&gt;
					&lt;h:commandButton value=&quot;Add&quot;
							action=&quot;#{calcAction.calculate}&quot;/&gt;
					&lt;h:messages/&gt;
				&lt;/h:form&gt;
			&lt;/f:view&gt;
		&lt;/body&gt;
	&lt;/html&gt;
</pre>
<p>To allow validation information to be displayed, we made several changes to this file and to our web application. These changes are listed here:</p>
<ul>
<li>Adding the Seam UI tag library to the header of the JSF page.</li>
<li>Specifying which objects need to be validated, by using the  or tags.</li>
<li>Defining the three different facets that specify the formatting and output of the error message for each field. These facets must be surrounded by the  tag.</li>
<li>Adding the Seam and third-party JARs into the WEB-INF/lib directory of the web application.</li>
</ul>
<h2>The JSF messages collection</h2>
<p>So far, we&#8217;ve only added messages to the JSF messages collection via Hibernate validators that are applied to entities within our domain model. Sometimes, it can be useful to add a message that can be displayed irrespective of any errors or input controls on a form to the messages collection. The JSF messages collection is one of Seam&#8217;s in-built components and is named org.jboss.seam.faces.facesMessages.</p>
<pre class="brush: java; title: ; notranslate">
	@Name(&quot;org.jboss.seam.faces.facesMessages&quot;)
</pre>
<p>This in-built component can easily be accessed within Seam applications by using the FacesMessages().instance() method. For example:</p>
<pre class="brush: java; title: ; notranslate">	FacesMessages.instance().add(&quot;Phew, that was a lot of maths !!&quot;);
</pre>
<p>If we add all of our Hibernate validators to our Calculator.java class, the code will look as follows:</p>
<pre class="brush: java; title: ; notranslate">	package com.davidsalter.seamcalculator;
	import java.io.Serializable;
	import org.jboss.seam.annotations.Name;
	import org.jboss.seam.faces.FacesMessages;
	import org.hibernate.validator.Max;
	import org.hibernate.validator.Min;
	import org.hibernate.validator.Range;

	@Name(&quot;calculator&quot;)
	public class Calculator implements Serializable {
		private double value1;
		private double value2;
		private double answer;
		@Min(value=0)
		@Max(value=100)
		public double getValue1() {
			return value1;
		}

		public void setValue1(double value1) {
			this.value1 = value1;
		}
		@Range(min=0, max=100)
		public double getValue2() {
			return value2;
		}
		public void setValue2(double value2) {
			this.value2 = value2;
		}
		public double getAnswer() {
			return answer;
		}
		public void add() {
			this.answer = value1 + value2;
			//Access the &quot;org.jboss.seam.faces.facesMessages&quot; component
				and add a message into it.
			FacesMessages.instance().add(&quot;Phew, that was a lot of
				maths !!&quot;);
		}
	}
</pre>
<p>In this code, we have added validators to the input data and then added a custom message into the JSF messages collection, to be displayed when a successful calculation was performed.</p>
<h2>Building and testing the validating Seam calculator</h2>
<p>In order to build and test the validating Seam calculator, we need to copy the additional JAR files (namely, hibernate-validator.jar, jboss-el.jar, jboss-seam-ui.jar, jsf-api.jar, and jsf-impl.jar) into the /lib directory of the sample project.</p>
<pre class="brush: java; title: ; notranslate">	A second sample project&lt;b&gt;—Seam Validating Calculator—&lt;/b&gt;is available
	in the code bundle for this chapter.
</pre>
<p>To build and deploy the project onto the JBoss Application Server, we need to execute the deploy ant target.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-13.gif"><img class="aligncenter size-full wp-image-6667" alt="seam-13" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-13.gif" width="450" height="180" /></a></center>To view the application in the browser, navigate to http://localhost:8080/SeamCalculator/calc.seam.</p>
<p>If any invalid data is entered into the web application, this is picked up by the Hibernate validators and displayed on the form, both next to the field containing the error and in a list at the bottom of the page, which shows all of the errors on the page.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-13.gif"><img class="aligncenter size-full wp-image-6667" alt="seam-13" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-13.gif" width="450" height="180" /></a></center>In the previous screenshot, you can see that the Hibernate validator has done its job correctly and added an error message informing the user that <b>Value 2</b> is invalid as <b>abc</b> is not a valid number. While this is technically correct, the error message is very<br />
user-unfriendly and would probably result with many unhappy customers! Later on in this book, we will look at how this error message can easily be changed in a localizable fashion so that an appropriate error message can be displayed in different languages for different users. For the moment, the important thing to note is that we have performed validation on user input and provided an error message when the validation fails.</p>
<p>When valid information is entered into the fields and the <b>Add</b> button is clicked, the two inputs are added together and displayed along with a custom error message that was added to the JSF messages collection.</p>
<p><center><a href="http://www.javabeat.net/wp-content/uploads/2009/09/seam-14.gif"><img class="aligncenter  wp-image-6668" alt="seam-14" src="http://www.javabeat.net/wp-content/uploads/2009/09/seam-14.gif" width="463" height="148" /></a></center></p>
<h1>Summary</h1>
<p>In this chapter, we&#8217;ve taken our first real steps into developing Seam applications. We&#8217;ve looked into Seam components and learned that they are declared with the @Name annotation. We&#8217;ve seen how we can inject and outject Seam components by using the @In and @Out annotations respectively. After learning about Seam components, we wrote a simple Seam application using these concepts, which shows the layout for Seam applications.</p>
<p>In the second half of this chapter, we looked at how to add validation to Seam components and how to display validation errors and special messages on web pages, both around input components and in a list on the page.</p>
<p>In the sample application that we built in this chapter, everything took place within a single JSF page. In the next chapter, we&#8217;ll look at page fl ow within Seam applications, and show how we can easily build up complex routing mechanisms to navigate between pages.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2009/09/seam-2-0-web-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introduction to JBoss Seam</title>
		<link>http://www.javabeat.net/2007/06/introduction-to-jboss-seam/</link>
		<comments>http://www.javabeat.net/2007/06/introduction-to-jboss-seam/#comments</comments>
		<pubDate>Thu, 21 Jun 2007 01:43:03 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[JBoss Seam]]></category>
		<category><![CDATA[Web Frameworks]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=52</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>1) Introduction Yet another Web Application Framework! This time it is from JBoss Community. JBoss provides a new Web Application Framework called &#8220;JBoss Seam&#8221; which combines the advantages from the two rapidly growing technologies Enterprise Java Beans 3.0 and Java Server Faces. JBoss Seam, by sitting on top of J2EE provides a nice way of [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><h2>1) Introduction</h2>
<p align="justify">Yet another Web Application Framework! This time it is from <strong><em>JBoss Community</em></strong>. JBoss provides a new Web Application Framework called <strong><em>&#8220;JBoss Seam&#8221;</em></strong> which combines the advantages from the two rapidly growing technologies <strong><em>Enterprise Java Beans 3.0</em></strong> and <strong><em>Java Server Faces</em></strong>. JBoss Seam, by sitting on top of J2EE provides a nice way of integration between JSF and EJB Components with other great functionalities. This article is an introductory article only and it covers the idea that gave birth to JBoss Seam, its advantages, the various modules involved along with a Sample Application. This article assumes the readers to have some bit of knowledge and programming in areas like Java Server Faces and Enterprise Java Beans 3.0. For more information about these technologies, visit <a href="http://www.javabeat.net/2007/05/introduction-to-java-server-faces/">JSF</a> and <a href="http://www.javabeat.net/2010/11/managing-transactions-in-ejb-3-0/">EJB 3.0</a>.</p>
<h2>2) The Need for JBoss Seam</h2>
<p align="justify">Let us exactly define what JBoss seam is. JBoss Seam provides a <strong><em>Light-weight Container</em></strong> for J2EE standards and it addresses the long-standing issues in any Typical Web Application like <strong><em>State Management</em></strong> and <strong><em>Better Browser Navigation</em></strong>. It also neatly provides an integration between the two popular technologies, <strong><em>Java Server Faces</em></strong> (in the UI tier) and <strong><em>Enterprise Java Beans</em></strong> (EJB 3 in the Server Side). Before getting into the various details about JBoss Seam let us see the common set of problems that are being faced in a Development and the Usability of a typical Web Application using Java Server Faces and Enterprise Java Beans.</p>
<p align="justify">For this, let us assume an imaginary application. Let us keep the requirements of the imaginary Web Application we are going to consider very small. The Web Application is a simple Registration Application, which provides the user with a View which contains username and password text-fields. User can click the submit button after filling both the fields. If the username password information given by the user is not found in the database, the user is assumed to be a new user and he is greeted with a welcome message; else an error page is displayed with appropriate message.</p>
<p align="justify">Let us analysis the roles of <strong><em>JSF</em></strong> and <strong><em>EJB 3.0</em></strong> in this Web Application. More specifically, we will analysis the various components in both the client as well the server tier along with their responsibilities.</p>
<p align="justify">In the Client side, for designing and presenting the form with the input controls (text-field and button) to the user, we may have written a userinput.jsp page with the set of JSF core tag libraries like &lt;f:view&gt; and &lt;h:form&gt;. And then, a JSF Managed Bean called UserBean encapsulated with properties(username and password) may have been coded which serves as a Model. The UI components values within the JSP page would have got bound to the properties of the Managed Bean with the help of JSF Expression Language. Since the logic is to query from the database for the existence of the username and the password, a Stateless Session Facade Bean would have been written whose sole purpose is to persistence the client information to the database. For persistence the information, the Session Bean may depend on the EntityManager API for querying and persisting entities.</p>
<p align="justify">The <strong><em>Data Traversal Logic</em></strong> from JSF to EJB Session Bean should have be taken care by the <strong><em>Managed Bean</em></strong> only. The Managed Bean apart from representing a Model may also act as a <strong><em>Listener</em></strong> in handling the button click events. Say, after clicking the register button, one of the action methods within the Managed Bean would have been called by the framework, and here the Bean might have a <strong><em>JNDI Look-up</em></strong> to get an instance of the Session Bean to persisting or querying the user information. If we look at carefully, the <strong><em>JSF Managed Bean</em></strong> is serving as an <strong><em>intermediary</em></strong> between the transfer of Data from the Client to the Server. Within this Managed Bean is the code for getting a reference to the Session Bean for doing various other functionalities. Wouldn&#8217;t a direct commnication between <strong><em>JSF UI Components</em></strong> and the <strong><em>Enterprise Bean Components</em></strong> be nice? There is no purpose of the intermediate Managed Bean in this case. JBoss Seam provides a very good solution for this. Not only this many of the outstanding problems that are faced in a Web Application are also addressed and given solution in this Framework.</p>
<h2>3) Advantages of JBoss Seam</h2>
<p align="justify">Following are the major advantages that a Web Application may enjoy if it uses <strong><em>JBoss Seam Framework</em></strong>. They are</p>
<ul>
<li>Integration of JSF and EJB</li>
<li>Stateful Web Applications</li>
<li>Dependency Bijection Support</li>
</ul>
<p align="justify">Let us look into the various advantages of JBoss Seam in the subsequent sections.</p>
<h3>3.1) Integration of JSF and EJB</h3>
<p align="justify">Today the Web Application World see more and more matured technologies that are focusing to establish an easy-to-use development by reducing lots and lots of boiler-plate code along with some other added functionalities in their own domains. Let us consider JSF and EJB technologies to extend further discussion regarding this.</p>
<p align="justify">EJB 3.0 which is a specification given from Sun has gained much popularity because of its simplified yet robust programming model. Much of the middle-ware related services like <strong><em>Security</em></strong>, <strong><em>Transactions</em></strong>, <strong><em>Connection Pooling</em></strong> etc is delegated to the container itself. Comparing to its predecessors, EJB 3.0 offers a <strong><em>POJO programming Model</em></strong>. No need for your beans to extend or implement EJB specific classes or interfaces. And also, along with the new specification <a href="http://www.javabeat.net/2007/04/jpa/">Java Persistence API (JPA)</a>, an unified programming model to access the underlying database along with rich set of features are now possible thereby completely eliminating the heavy-headed entity beans.</p>
<p><strong><span style="text-decoration: underline;"><a href="http://www.javabeat.net/2007/04/jpa/">What is JPA?</a></span></strong>:Java Persistence API (JPA) provides POJO (Plain Old Java Object) standard and object relational mapping (OR mapping) for data persistence among applications. Persistence, which deals with storing and retrieving of application data, can now be programmed with Java Persistence API starting from EJB 3.0 as a result of JSR 220. This API has borrowed many of the concepts and standards from leading persistence frameworks like Toplink (from Oracle) and Hibernate (from JBoss). One of the great benefits of JPA is that it is an independent API and can nicely integrate with J2EE as well as J2SE applications.</p>
<p align="justify"><strong><em>Java Server Faces</em></strong> provides a <strong><em>Component-based approach</em></strong> for developing User Interface Components in a Web Application. It hides most of the boiler-plate code and provides a higher-level abstraction over the client request and the server response objects. Using Java Server Faces, a Web Application can be viewed by components making events and listeners executing the appropriate logic for the Events. No need for the traditional HttpServletRequest and HttpServletResponse object to extract the client input parameters and to generate the response.</p>
<p align="justify">JBoss provides a framework in which the <strong><em>Events</em></strong> that are emitted by the <strong><em>JSF UI Components</em></strong> can be directly handled by the <strong><em>Enterprise Beans</em></strong>. No need for the intermediate Managed Beans which establishes the data transfer from and to JSF and EJB.</p>
<h3>3.2) Dependency Bijection Support</h3>
<p align="justify">Before getting into <strong><em>Dependency Bijection</em></strong>, it is wise to look at the two types: namely <strong><em>Dependency Injection </em></strong> and <strong><em>Dependency Outjection</em></strong>. These two are the popular patterns and modern Frameworks and Containers makes use of this abundantly. Let us see these two techniques</p>
<h5>3.2.1) Dependency Injection</h5>
<p align="justify">This model is used when a <strong><em>Component</em></strong> or a <strong><em>Service</em></strong> which is running inside some <strong><em>Framework</em></strong> or a <strong><em>Container</em></strong> is well known in the early stages so that the Framework/Container can create instances of them thereby taking the burden from the Clients. These type of model is used heavily in most of the J2EE Components, to name a few, EJB, Servlets, JMS etc.</p>
<p align="justify">For example, consider the following piece of code,</p>
<p><strong>MySessionBean.java</strong></p>
<pre class="brush: java; title: ; notranslate">@Stateless
public class MySessionBean{

    @PersistentContext
    private EntityManager entityManager;

    public void query(){
        // Do something with EntityManager reference.
    }
}</pre>
<p align="justify">In the above code, the Session Bean (<code>MySessionBean</code>) is depending on the services of <code>EntityManager</code>. So, before the session bean starts using the <code>EntityManager</code> object, the <code>EntityManager</code> instance should be available. This is made simple because the EJB Container will creates a new instance of the <code>EntityManager</code> object based on the various parameters taken from the Configuration File and injects this instance to the <code>EntityManager</code> reference. This way of doing is called Dependency Injection.</p>
<p align="justify">In Seam, injection of a Component by the Seam framework is done by the use of <code>@In</code> Annotation. For example, consider the following piece of code,</p>
<p><strong>MyComponent.java</strong></p>
<pre class="brush: java; title: ; notranslate">public class MyComponent{

    @In
    private MyInjectorComponent compToBeInjected;

}</pre>
<p align="justify">When the Application gets deployed, Seam Framework will keep track of these Annotated Components and during run-time, upon creation of <code>MyComponent</code> class and before the invocation of any other methods, the Container will inject and instance of an object of type <code>MyInjectorComponent</code> to the <code>compToBeInjected</code> reference.</p>
<h5>3.2.2) Dependency Outjection</h5>
<p align="justify">In Dependency Injection, usually the Framework injects the services to components that are in need of, whereas the reverse happens in <strong><em>Dependency Outjection</em></strong>. Consider the following piece of code,</p>
<p><strong>MyComponent.java</strong></p>
<pre class="brush: java; title: ; notranslate">public class MyComponent{

    @Out
    private MyService myService;

    public MyComponent(){
    }

    public MyService getMyService(){
        return createMyService();
    }

    private MyService createMyService(){
        // My own way of creating
    }
}</pre>
<p><strong>MyServiceClient.java</strong></p>
<pre class="brush: java; title: ; notranslate">public class MyServiceClient{

    @In
    private MyService myService;

}</pre>
<p align="justify">In the above code, <code>MyComponent</code> class is acting as a factory class for creating <code>MyService</code> objects. Assume that this <code>MyComponent</code> class is a controlled class meaning that is being managed by the Container or the Framework wherever it is embedded. Situations may arise such that the <code>MyService</code> object might be needed by the Container so that the Container can use this <code>MyService</code> to inject dependencies on other components or clients. For instance, <code>MyServiceClient</code> is in need of <code>MyService</code> object and the Container can inject this reference from the object that it has taken from MyComponent class. This model is <strong><em>Dependency Outjection</em></strong> where a Service is taken by the Container from the Component.</p>
<p align="justify">Seam supports both these models in the form of <code>@In</code> and <code>@Out</code> annotations. For example consider the following class,</p>
<p><strong>MyClass.java</strong></p>
<pre class="brush: java; title: ; notranslate">public class MyClass{

    @In
    private MyServiceOne service1;

    @out
    private MyServiceTwo service2;
}</pre>
<p align="justify">The above code can be read like this. The container injects the reference to service1 because it is annotated with <code>@In</code> (meaning for Injection). And at a later stage, the Container may taken the reference of service2 (because of <code>@Out</code>) thereby serving it to other needful Components.</p>
<h3>3.3) Stateful Web Applications</h3>
<p align="justify">Since the underlying protocol used in a Web Application is <strong><em>Http</em></strong>, all Web Application are <strong><em>Stateless in nature</em></strong>. Precisely it means that all the requests that are coming from the Client Browser are treated as individual requests only. The Server shows no partiality for the Client Requests. It is up to the Application or the Framework to identify whether requests are coming from the same Client or not. Session Management in Web Application is a time-consuming job and typically Servlets/Jsp provides various ways to manage sessions. Even in this case, Application Developers still have to depend on HttpSession like classes for creating session objects and storing/retrieving objects from the session.</p>
<p align="justify">JBoss Seam provides an excellent way to <strong><em>Manage States</em></strong> amongst multiple client requests. The <strong><em>State Management Facility</em></strong> is tightly integrated with Seam Components in the form of various <strong><em>Contexts</em></strong>. Following are the most commonly used Contexts in Seam.</p>
<ul>
<li>Conversation – A <strong><em>Conversation</em></strong> generally represents multiple requests that come from the same client. Components that come under this category can remember the state of the client and this forms the heart of <strong><em>State Management</em></strong> in Seam.</li>
<li>Application – This context generally holds information that is used by various components within the same Application.</li>
<li>Session – Components that fall under this category simply uses <strong><em>Http Session Api</em></strong> for state management.</li>
<li>Page – The scope of the components managing the information is restricted to the <strong><em>Current Page</em></strong> only. All the stored information is lost when the user leaves this current page.</li>
<li>Stateless – Components that falls under this category don&#8217;t manage state between multiple Client Requests.</li>
</ul>
<h2>4) Seam Components</h2>
<p align="justify">Components in Seam take over the functionality as well the Business Logic in a Web Application. The good thing is that all Seam Components are <strong><em>POJO Objects</em></strong> only meaning that Components don&#8217;t have to extend or implement any Seam specific Classes or Interfaces. The Seam Component Model follows the standard <strong><em>Java Bean Component Architecture</em></strong>. Even it is possible to <strong><em>Annotate Enterprise Beans</em></strong> (Session Beans, Entity Beans and Message Driven Beans) as Components. Seam provides an excellent framework for <strong><em>Integrating Enterprise Beans into the Seam Framework</em></strong>.</p>
<p align="justify">As mentioned before, Components in Seam carries the business process involved in a typical Web Application. Seam Components may also act as <strong><em>Listeners</em></strong> for JSF UI Components or they may even interact with the Database. Since Seam provides a good deal of Integration with <strong><em>EJB Components</em></strong>, Developers generally prefer having <strong><em>Stateful Session Beans</em></strong> as Listeners and the <strong><em>Entity Beans</em></strong> for interacting with the Database.</p>
<p align="justify">Usually Seam Components are identified by <strong><em>Name</em></strong>, <strong><em>Scope</em></strong> and <strong><em>Role</em></strong>. These are <strong><em>Declarative Identifications</em></strong> meaning that they are represented directly in the Source Code with the help of Annotations. For example, consider the following Seam Component,</p>
<p><strong>MyStatefulBeanImpl.java</strong></p>
<pre class="brush: java; title: ; notranslate">@Stateful
@Name(&quot;MyStatefulBean&quot;)
@Scope(ScopeType.SESSION)
public MyStatefulBeanImpl implements MyStatefulBean{

    public String statefulMethod(){
    }
}</pre>
<p align="justify">In the above code, the <code>@Name</code> Annotation represents the name of a Seam Component using which other Managed Seam Components can access it. <code>@Scope</code> defines how life this Component will survive during the Web Application. Since the above class is actually a Session Bean, it has been marked with <code>@Stateful</code> Annotation.</p>
<h2>5) Support for Annotations</h2>
<p align="justify">Most of the boiler-plate needed for the various parts of the Application is taken care by the Seam because of the declarative style of programming available in the form of Annotations. Most of the Annotations that we see in Seam are a mix of EJB 3.0 and Seam specific Annotations. They are a number of Annotations defined in the Seam Api and this section just provides an overview of the most commonly used Annotations. For a complete list of Annotations and their purpose, have a look at the Seam Documentation.</p>
<h3>5.1) Annotations for Seam Components</h3>
<p align="justify">Components in Seam are really POJO&#8217;s and they play a major role in keeping the <strong><em>Application stateful</em></strong>. Following are the most commonly used Annotations related to Seam Components are given below.</p>
<ul>
<li>@Name</li>
<li>@Scope</li>
<li>@JndiName</li>
</ul>
<h5>5.1.1) @Name Annotation</h5>
<p align="justify">This Annotation specifies the name of a <strong><em>Seam Component</em></strong> so that other Components (Seam Components or JSF Pages) can refer the Component by its name. This Annotation is mandatory for a class that is going to act as a Seam Component. Following is an example of the Seam Component.</p>
<p><strong>MyComponent.java</strong></p>
<pre class="brush: java; title: ; notranslate">@Name(&quot;MyComponent&quot;)
public class MyComponent{
    // Some Functionalities here.
}</pre>
<h5>5.1.2) @Scope Annotation</h5>
<p align="justify">This Annotation, if given, tells to the Seam Framework about the scope (or the life-time) for a Seam Component. The values for this Annotation are taken from the <code>org.jboss.seam.ScopeType</code> Enum and can be any of the following values: <code>APPLICATION</code>, <code>CONVERSATION</code>, <code>SESSION</code>, <code>PAGE</code>, <code>STATELESS</code> etc. Following is a sample code snippet for using the <code>@Scope</code> Annotation.</p>
<p><strong>MyComponent.java</strong></p>
<pre class="brush: java; title: ; notranslate">@Scope(ScopeType.SESSION)
@Name(&quot;MyComponent&quot;)
public class MyComponent{
    // Some Functionalities here.
}</pre>
<h5>5.1.3) @JndiAnnotation Annotation</h5>
<p align="justify">This Annotation should not be applied to normal Seam Components but to Components that represent any of <strong><em>EJB</em></strong> or <strong><em>JMS Service</em></strong>. Following is an example of one such component.</p>
<p><strong>MySessionBean.java</strong></p>
<pre class="brush: java; title: ; notranslate">@JndiName(&quot;ejb/session/MySessionBean&quot;)
Public class MySessionBean{

}</pre>
<h3>5.2) Annotations for Components Lifecycle</h3>
<p align="justify">All Components in Seam have well defined <strong><em>Life-cycle Management</em></strong> as defined by the Seam Framework. Management of Component Life-cycle is important as it has dependencies over the state Management of Web Applications. Following are the most commonly used Annotations for Life-cycle Management.</p>
<ul>
<li>@Create</li>
<li>@Destroy</li>
</ul>
<h5>5.2.1) @Create Annotation</h5>
<p align="justify">When a method inside a Seam Component is marked with <code>@Create</code> Annotation, then this method will be called immediately after the creation of Seam Component. Most of the costly Resources like <strong><em>Initializing</em></strong> File or Database can be coded in this method. Following is the sample code snippet for the same,</p>
<p><strong>MyDatabaseUtils.java</strong></p>
<pre class="brush: java; title: ; notranslate">@Name(&quot;MyDatabaseUtils&quot;)
Public class MyDatabaseUtils{

    @Create()
    public void initDatabase(){
    }
}</pre>
<h5>5.2.2) @Destroy Annotation</h5>
<p align="justify">If <code>@Create</code> Annotation for a Seam Component deals with initialization Stuffs for a object, then the code inside the <code>@Destroy</code> Annotation can be used to <strong><em>Release Resources or References</em></strong> to other objects. This method will be called before a Component is going to get removed from any of the Context as mentioned by its Scope.</p>
<p><strong>MyDatabaseUtils.java</strong></p>
<pre class="brush: java; title: ; notranslate">@Name(&quot;MyDatabaseUtils&quot;)
Public class MyDatabaseUtils{

    @Destroy()
    public void closeDbResources(){
    }
}</pre>
<h2>6) Events</h2>
<p align="justify"><strong><em>Events</em></strong> can occur in a Seam Application through various <strong><em>Sources</em></strong>. And the events emitted by the Sources are captured and handled by the <strong><em>Listeners</em></strong>. Let us look into the various Sources that are responsible for emitting Events. Source can be any one of the following items.</p>
<ul>
<li>Seam Page Events</li>
<li>Seam Component Driven Events</li>
<li>JSF Components Emitting Events</li>
</ul>
<p align="justify">Let us look into the details one by one.</p>
<h3>6.1) Seam Page Events</h3>
<p align="justify">Before Seam renders a page to Display, it checks whether the page has registered itself for firing any Events. If that&#8217;s the case, then the Framework will fire Events on behalf of the Web Pages. For example, consider the following situation. If the Home Page of a Web-site is eager to display the hits to this page, then the following could be possible,</p>
<p><strong>pages.xml</strong></p>
<pre class="brush: java; title: ; notranslate">
&lt;pages&gt;
    &lt;page view-id = &quot;/hit-count.jsp&quot; action = &quot;#{Hitter.initHits}&quot;&gt;
    &lt;/page&gt;
&lt;/pages&gt;
</pre>
<p>The above is the <code>pages.xml</code> file and it should be placed in the <code>WEB-INF</code> directory of the Web Application module. The above code essentially tells that whenever a request comes for <code>/hit-count.jsp</code>, then call the method <code>initHits()</code> available in the <code>Hitter</code> class.</p>
<h3>6.2) Seam Component Driven Events</h3>
<p>It is possible by the Seam Components also to emit Events. There is a <strong><em>Low-degree of Coupling</em></strong> between the Initiation and the Handling of Events. Even <strong><em>Custom Events</em></strong> can be declared specified either in the Xml File or through Annotations. For example, consider the following code snippet,</p>
<p><strong>components.xml</strong></p>
<pre class="brush: java; title: ; notranslate">
&lt;components&gt;
    &lt;event type = &quot;myEvent&quot;&gt;
        &lt;action expression = &quot;#{MyComponent.myCallBack1}&quot;/&gt;
        &lt;action expression = &quot;#{MyComponent.myCallBack2}&quot;/&gt;
    &lt;/event&gt;
&lt;/components&gt;
</pre>
<p>The above Xml File must be placed in the <code>WEB-INF</code> directory of the Web Application Module. If anyone raises an Event of type (<code>'myEvent'</code> is just a string), then the following methods defined inside the action element will be fired. For, example, consider the following code,</p>
<p><strong>MyEventInitiator.java</strong></p>
<pre class="brush: java; title: ; notranslate">public class MyEventInitiator{

    public void initEvent(){
        Events.instance().raiseEvent(&quot;myEvent&quot;);
    }
}</pre>
<p><strong>MyComponent.java</strong></p>
<pre class="brush: java; title: ; notranslate">public MyComponent{

    public void myCallBack1(){
        System.out.println(&quot;My Call back method 1 called&quot;);
    }

    public void myCallBack2(){
        System.out.println(&quot;My Call back method 2 called&quot;);
    }
}</pre>
<p align="justify">If some one initiates the Event by calling <code>MyEventInitiator.initEvent()</code>, then a event of type myEvent gets raised. Since this Listeners (Actions) of this Event are defined in the components.xml file in the form of <code>MyComponent.myCallBack1()</code> and <code>MyComponent.myCallback2()</code>, both these methods will be fired in the order in which they are defined.</p>
<h3>6.3) JSF Components Emitting Events</h3>
<p align="justify">As discussed previously, Seam components can act as <strong><em>Listeners</em></strong> for the <strong><em>JSF UI Components</em></strong> emitting Events. For example, consider the following piece of Code,</p>
<p><strong>calculate.jsp</strong></p>
<pre class="brush: java; title: ; notranslate">
&lt;h:commandButton value = &quot;Calculate Tax&quot; action = &quot;#{TaxCalculator.calculateTax}&quot;&gt;
&lt;/h:commandButton&gt;
</pre>
<p>In the above snippet code, whenever the User clicks the Calculate Tax button, the <code>calculateTax()</code> method defined inside the <code>TaxCalculator</code> class will be called. Following is how the <code>TaxCalculator</code> component looks like,</p>
<p><strong>TaxCalculator.java</strong></p>
<pre class="brush: java; title: ; notranslate">
@Name(&quot;TaxCalculator&quot;)
@Scope(ScopeType.PAGE)
public class TaxCalculator{

    public void calculateTax(){
    }
}</pre>
<h2>7) Sample Application</h2>
<p align="justify">With the concepts and theories in mind, let us develop a minimal functionality Shopping Application using JBoss Seam Framework. The following are the list of softwares/products needed for the Sample Application to work.</p>
<ul>
<li>JBoss Application Server -<br />
<a href="http://labs.jboss.com/jbossas/downloads" target="_blank">http://labs.jboss.com/jbossas/downloads</a></li>
<li>JBoss Seam Framework -<br />
<a href="http://labs.jboss.com/jbossseam/download" target="_blank">http://labs.jboss.com/jbossseam/download</a></li>
<li>Facelets -<br />
<a href="https://facelets.dev.java.net/" target="_blank">https://facelets.dev.java.net/</a></li>
</ul>
<p align="justify">The functionality of the <strong><em>Shopping Application</em></strong> is simple. It initially provides a page prompting for the user name before beginning the Shopping Application. Then a <strong><em>List of products</em></strong> along with <strong><em>Product Id</em></strong>, <strong><em>Name</em></strong>, <strong><em>Description</em></strong>, <strong><em>Price</em></strong>, <strong><em>Number of Items</em></strong> to be checked-out are shown to the user. The user can select the product items he wants and continues to check-out. After that a brief summary of the Product Information is shown to the User .</p>
<p align="justify">Since the Shopping Application it is an <strong><em>Enterprise application</em></strong>, it involves the <strong><em>Creation of Jar and War files</em></strong> and then packaging them as an <strong><em>Ear File</em></strong>. Let us look into the various files and the directory structure required for completing this Application.</p>
<h3>7.1) View Files</h3>
<p align="justify">These set of files represents the view files rendered as a result of client making the client. Let us look into the various files involved one by one.</p>
<h5>7.1.1) index.html</h5>
<p><strong>index.html</strong></p>
<pre class="brush: java; title: ; notranslate">
&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Refresh&quot; content=&quot;0; URL=user.seam&quot;&gt;
    &lt;/head&gt;
&lt;/html&gt;
</pre>
<p align="justify">This is the file that will be request when the client starts the Application in the Web Browser. This file immediately redirects to <code>user.seam</code>. Later, we will see that the extension seam is mapped to xhtml (for Facelets). Here is the content of <code>user.xhtml.</code></p>
<h5>7.1.2) user.xhtml</h5>
<pre class="brush: java; title: ; notranslate">
&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:h=&quot;http://java.sun.com/jsf/html&quot;
    xmlns:f=&quot;http://java.sun.com/jsf/core&quot;&gt;

    &lt;body&gt;

    &lt;center&gt;
        &lt;h2&gt;Welcome to Shopping.&lt;/h2&gt;
    &lt;/center&gt;

    &lt;h:form&gt;
    &lt;center&gt;

    To contine shopping, Please enter your name:
    &lt;br/&gt;

     &lt;h:inputText value=&quot;#{User.name}&quot; size=&quot;20&quot;/&gt;&lt;br/&gt;
     &lt;h:commandButton type=&quot;submit&quot; value=&quot;Continue Shopping&quot; action=&quot;product-list&quot;/&gt;

     &lt;/center&gt;
     &lt;/h:form&gt;

     &lt;/body&gt;
&lt;/html&gt;
</pre>
<p align="justify">The things to note that is there is a Component called <code>User</code> with a property called <code>name</code>. As mentioned in the preceding sections, the user will be prompted to enter his name before continue with the shopping. The entered name will be mapped to the name property in the User class. Also note that when the clicks the &#8220;Continue Shopping&#8221; button, the action is re-directed to &#8220;product-list&#8221; which actually maps to <code>product-list.xhtml</code> (which is defined the <code>navigation.xml</code>) file. Given below are the contents of <code>product-list.xhtml</code> file.</p>
<h5>7.1.3) product-list.xhtml</h5>
<pre class="brush: java; title: ; notranslate">
&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:h=&quot;http://java.sun.com/jsf/html&quot;
    xmlns:f=&quot;http://java.sun.com/jsf/core&quot;&gt;

    &lt;body&gt;

    &lt;center&gt;
        &lt;h2&gt;List of available products&lt;/h2&gt;

        &lt;p&gt;Hi, &lt;b&gt;#{User.name}&lt;/b&gt;&lt;/p&gt;
        &lt;p&gt;Following are the available producst in our shopping repository&lt;/p&gt;
    &lt;/center&gt;

    &lt;h:form&gt;

    &lt;center&gt;
        &lt;h:dataTable  columnClasses=&quot;list-column-center, list-column-right,
            list-column-center, list-column-right&quot; headerClass=&quot;list-header&quot;
            rowClasses=&quot;list-row&quot; styleClass=&quot;list-background&quot;
            value=&quot;#{ShoppingManager.products}&quot; var=&quot;aProduct&quot;&gt;

            &lt;h:column&gt;
                &lt;h:outputText value = &quot;#{aProduct.id}&quot;/&gt;
            &lt;/h:column&gt;

            &lt;h:column&gt;
                &lt;h:outputText value = &quot;#{aProduct.name}&quot;/&gt;
            &lt;/h:column&gt;

            &lt;h:column&gt;
                &lt;h:outputText value = &quot;#{aProduct.description}&quot;/&gt;
            &lt;/h:column&gt;

            &lt;h:column&gt;
                &lt;h:outputText value = &quot;#{aProduct.price}&quot;/&gt;
            &lt;/h:column&gt;

            &lt;h:column&gt;
                &lt;h:inputText value = &quot;#{aProduct.noOfItems}&quot; size = &quot;5&quot;/&gt;
            &lt;/h:column&gt;
        &lt;/h:dataTable&gt;
    &lt;/center&gt;

    &lt;center&gt;
        &lt;h:commandButton action=&quot;#{ShoppingManager.checkout}&quot;
            value=&quot;Checkout Products&quot;/&gt;
        &lt;h:commandButton action=&quot;user&quot; value=&quot;Go to user page&quot; type = &quot;submit&quot;/&gt;
    &lt;/center&gt;

    &lt;/h:form&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre>
<p align="justify">Note that a class called Product with properties <strong><em>name</em></strong>, <strong><em>description</em></strong>, <strong><em>price</em></strong>, <strong><em>noOfItems</em></strong>, <strong><em>amount</em></strong>, <strong><em>id (Primary key)</em></strong> is defined which represent of the products in the shopping repository. Access to the product objects such as displaying is facilitated with the help of ShoppingManager Session Bean. Note that this Session Bean is configured to act as a <strong><em>Seam Component</em></strong> in this Application. As soon as this page loads, all the <strong><em>Product information</em></strong> from the Products Table, is fetched and it is displayed in this page.</p>
<p align="justify">We have two buttons at the bottom of the page. One is to navigate to the Home page and the other is to checkout the products. As soon as the user clicks the &#8220;Checkout Products&#8221; button, the <code>checkOut()</code> method in the <code>ShoppingManager</code> bean is fired which will calculate the amount of the individual products the user has selected. Upon completion of the method, checkout action is returned which is mapped with <code>product-summary.xhtml</code> page as defined in the <code>navigation.xml</code> file.</p>
<h5>7.1.4) product-summary.xhtml</h5>
<p align="justify">Given below are the contents of <code>product-summary.xhtml</code> file which will display the summary information of the user selected products along with the total price information.</p>
<p><strong>product-summary.xhtml</strong></p>
<pre class="brush: java; title: ; notranslate">
&lt;pre&gt;&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:h=&quot;http://java.sun.com/jsf/html&quot;
    xmlns:f=&quot;http://java.sun.com/jsf/core&quot;&gt;

    &lt;body&gt;

    &lt;center&gt;
    &lt;h2&gt;Shopping Summary&lt;/h2&gt;
    &lt;p&gt;Hi, &lt;b&gt;#{User.name}&lt;/b&gt;&lt;/p&gt;
    &lt;/center&gt;

    &lt;h:form&gt;
    &lt;center&gt;
    &lt;h:dataTable  columnClasses=&quot;list-column-center, list-column-right,
        list-column-center, list-column-right&quot; headerClass=&quot;list-header&quot;
        rowClasses=&quot;list-row&quot; styleClass=&quot;list-background&quot;
        value=&quot;#{ShoppingManager.products}&quot; var=&quot;aProduct&quot;&gt;

        &lt;h:column&gt;
            &lt;h:outputText value = &quot;#{aProduct.id}&quot;/&gt;
        &lt;/h:column&gt;

        &lt;h:column&gt;
            &lt;h:outputText value = &quot;#{aProduct.name}&quot;/&gt;
        &lt;/h:column&gt;

        &lt;h:column&gt;
            &lt;h:outputText value = &quot;#{aProduct.description}&quot;/&gt;
        &lt;/h:column&gt;

        &lt;h:column&gt;
           &lt;h:outputText value = &quot;#{aProduct.price}&quot;/&gt;
        &lt;/h:column&gt;

        &lt;h:column&gt;
           &lt;h:outputText value = &quot;#{aProduct.noOfItems}&quot; size = &quot;5&quot;/&gt;
        &lt;/h:column&gt;

        &lt;h:column&gt;
            &lt;h:outputText value = &quot;#{aProduct.amount}&quot; size = &quot;5&quot;/&gt;
        &lt;/h:column&gt;

    &lt;/h:dataTable&gt;
    &lt;/center&gt;

    &lt;center&gt;
    Total Price:
    &lt;b&gt;&lt;h:outputText value = &quot;#{ShoppingManager.totalPrice}&quot; /&gt;&lt;/b&gt;
    &lt;p&gt;Happy Shopping.&lt;/p&gt;
    &lt;h:commandButton action=&quot;user&quot; value=&quot;Go to user page&quot; type = &quot;submit&quot;/&gt;
    &lt;/center&gt;

    &lt;/h:form&gt;
    &lt;/body&gt;
&lt;/html&gt;&lt;/pre&gt;
</pre>
<h3>7.2) Resource Files</h3>
<p align="justify">In Seam terms, all the <strong><em>Configuration Information</em></strong> (<code>web.xml</code>, <code>application.xml</code>) and the various <strong><em>Resources Messages</em></strong> are defined in terms of resources. Let us have a look into the various Configuration file.</p>
<h5>7.2.1) application.xml</h5>
<p><strong>application.xml</strong></p>
<pre class="brush: java; title: ; notranslate">
&lt;application&gt;
    &lt;display-name&gt;Seam Hello World&lt;/display-name&gt;

    &lt;module&gt;
        &lt;web&gt;
            &lt;web-uri&gt;app.war&lt;/web-uri&gt;
            &lt;context-root&gt;/Shopping&lt;/context-root&gt;
        &lt;/web&gt;
    &lt;/module&gt;

    &lt;module&gt;
        &lt;ejb&gt;app.jar&lt;/ejb&gt;
    &lt;/module&gt;

    &lt;module&gt;
        &lt;java&gt;jboss-seam.jar&lt;/java&gt;
    &lt;/module&gt;

    &lt;module&gt;
        &lt;java&gt;el-ri.jar&lt;/java&gt;
    &lt;/module&gt;

    &lt;module&gt;
        &lt;java&gt;el-api.jar&lt;/java&gt;
    &lt;/module&gt;

&lt;/application&gt;
</pre>
<p align="justify">This is the <strong><em>Application Level Configuration File</em></strong> which contains entries for the <strong><em>Web</em></strong> and the <strong><em>Java Module</em></strong> along with the libraries that are specific to JBoss and Seam.</p>
<h5>7.2.2) ejb-jar.xml</h5>
<p><strong>ejb-jar.xml</strong></p>
<pre class="brush: java; title: ; notranslate">
&lt;ejb-jar&gt;
    &lt;assembly-descriptor&gt;
        &lt;interceptor-binding&gt;
        &lt;ejb-name&gt;*&lt;/ejb-name&gt;
        &lt;interceptor-class&gt;org.jboss.seam.ejb.SeamInterceptor&lt;/interceptor-class&gt;
        &lt;/interceptor-binding&gt;
    &lt;/assembly-descriptor&gt;

    &lt;interceptors&gt;
        &lt;interceptor&gt;
        &lt;interceptor-class&gt;org.jboss.seam.ejb.SeamInterceptor&lt;/interceptor-class&gt;
    &lt;/interceptor&gt;
    &lt;/interceptors&gt;
&lt;/ejb-jar&gt;
</pre>
<p align="justify">These entries are necessary to make all the Ejb Components look like Seam Components. This is achieved by <strong><em>intercepting</em></strong> the functionality of the Ejb Components with the help of <strong><em>Interceptors</em></strong>.</p>
<h5>7.2.3) persistence.xml</h5>
<p><strong>persistence.xml</strong></p>
<pre class="brush: java; title: ; notranslate">
&lt;persistence&gt;
    &lt;persistence-unit name=&quot;ShoppingManager&quot;&gt;
    &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
    &lt;jta-data-source&gt;java:/DefaultDS&lt;/jta-data-source&gt;

    &lt;properties&gt;
        &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.HSQLDialect&quot;/&gt;
        &lt;property name=&quot;hibernate.transaction.flush_before_completion&quot; value=&quot;true&quot;/&gt;
        &lt;property name=&quot;hibernate.hbm2ddl.auto&quot; value=&quot;create-drop&quot;/&gt;
        &lt;property name=&quot;hibernate.show_sql&quot; value=&quot;true&quot;/&gt;
    &lt;/properties&gt;
    &lt;/persistence-unit&gt;
&lt;/persistence&gt;
</pre>
<p align="justify">The above file represents the <strong><em>Persistence Unit</em></strong> that the Session Bean may use to store and retrieve information about the product Objects from the Product table. A persistence Unit generally contains information about its Database, Driver Name, Dialect etc.</p>
<h5>7.2.4) components.xml</h5>
<p align="justify">Since we are using Session Bean for interacting with the Entity Object, we must specify the <strong><em>Jndi Name</em></strong> of the Session Bean so that Seam framework can lookup and create an instance of the Session Bean. This is achieved with the help of the following Xml File.</p>
<p><strong>components.xml</strong></p>
<pre class="brush: java; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;components xmlns=&quot;http://jboss.com/products/seam/components&quot;
    xmlns:core=&quot;http://jboss.com/products/seam/core&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xsi:schemaLocation=
    &quot;http://jboss.com/products/seam/core

http://jboss.com/products/seam/core-1.1.xsd


http://jboss.com/products/seam/components


http://jboss.com/products/seam/components-1.1.xsd&quot;&gt;

    &lt;core:init jndi-pattern=&quot;Shopping/#{ejbName}/local&quot; debug=&quot;true&quot;/&gt;
    &lt;core:manager conversation-timeout=&quot;120000&quot;/&gt;

&lt;/components&gt;
</pre>
<h5>7.2.5) navigation.xml</h5>
<p align="justify">All the navigation rules in this Shopping Application are logically expressed in terms of action in this <code>navigation.xml</code> file. The advantage of providing this kind of declarative mapping is that all the components will refer only to the <strong><em>Logical Outcome of the Action</em></strong> and not the <strong><em>Physical Location of the Page</em></strong>.</p>
<p><strong>A.a</strong></p>
<pre class="brush: java; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE faces-config
    PUBLIC &quot;-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN&quot;
    &quot;http://java.sun.com/dtd/web-facesconfig_1_0.dtd&quot;&gt;

    &lt;faces-config&gt;

        &lt;navigation-rule&gt;
            &lt;from-view-id&gt;*&lt;/from-view-id&gt;

        &lt;navigation-case&gt;
            &lt;from-outcome&gt;user&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/user.xhtml&lt;/to-view-id&gt;
            &lt;redirect/&gt;
        &lt;/navigation-case&gt;

        &lt;navigation-case&gt;
            &lt;from-outcome&gt;product-list&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/product-list.xhtml&lt;/to-view-id&gt;
            &lt;redirect/&gt;
        &lt;/navigation-case&gt;

        &lt;navigation-case&gt;
             &lt;from-outcome&gt;checkout&lt;/from-outcome&gt;
             &lt;to-view-id&gt;/product-summary.xhtml&lt;/to-view-id&gt;
             &lt;redirect/&gt;
         &lt;/navigation-case&gt;
    &lt;/navigation-rule&gt;

&lt;/faces-config&gt;

</pre>
<h3>7.3) Source Files</h3>
<p align="justify">Now let us look into the Various Java Source Files (representing Session Beans, Entities, etc). Following is the code for the Product Entity.</p>
<h5>7.3.1) Product.java</h5>
<p><strong>Product.java</strong></p>
<pre class="brush: java; title: ; notranslate">package net.javabeat.articles.jboss.seam.shopping;

import org.jboss.seam.annotations.*;
import javax.persistence.*;
import java.io.Serializable;

import static org.jboss.seam.ScopeType.SESSION;

@Entity
@Name(&quot;person&quot;)
@Scope (SESSION)
public class Product {

    private String name;
    private String description;
    private double price;
    private int noOfItems;
    private double amount;
    private long id;

    public Product() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
       this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
       this.description = description;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
       this.price = price;
    }

    public int getNoOfItems() {
       return noOfItems;
    }

    public void setNoOfItems(int noOfItems) {
       this.noOfItems = noOfItems;
    }

    public double getAmount(){
       return amount;
    }

    public void setAmount(double amount){
       this.amount = amount;
    }

    @Id @GeneratedValue
    public long getId() {
       return id;
    }

    public void setId(long id) {
       this.id = id;
    }
}</pre>
<p align="justify">Note that apart from qualifying this as an Entity, it has to be qualified with Annotations like <code>@Name</code> and <code>@Scope</code> to tell to the Seam Framework that it is a <strong><em>Seam Component</em></strong>. The <strong><em>Session Bean</em></strong> will store and retrieve the values from the <code>Product</code> objects fetched from the Product Table.</p>
<h5>7.3.2) Session Bean Interface</h5>
<p align="justify">Following is the interface definition of the Session Bean. This Bean has various functionalities like <strong><em>Creating, Retrieving and Destroying products</em></strong>. Other that this it also performs the checkout operation which is nothing but the calculation of the no of items with the individual products along with the total amount calculation.</p>
<p><strong>Shopping.java</strong></p>
<pre class="brush: java; title: ; notranslate">package net.javabeat.articles.jboss.seam.shopping;

import javax.ejb.*;
import java.util.*;
import net.javabeat.articles.jboss.seam.shopping.Product;

@Local
public interface Shopping {

    public List  getProducts();
    public void createProducts();
    public void destroyProducts();
    public String checkout();
    public double getTotalPrice();

}</pre>
<h5>7.3.3) Session Bean Implementation</h5>
<p><strong>ShoppingManager.java</strong></p>
<pre class="brush: java; title: ; notranslate">package net.javabeat.articles.jboss.seam.shopping;
import java.util.List;
import javax.ejb.*;
import net.javabeat.articles.jboss.seam.shopping.Product;
import net.javabeat.articles.jboss.seam.shopping.ProductDatabase;
import org.jboss.seam.annotations.*;
import org.jboss.seam.ejb.*;

import static org.jboss.seam.ScopeType.SESSION;

import javax.persistence.*;

@Stateful
@Name(&quot;ShoppingManager&quot;)
@Scope (SESSION)
public class ShoppingManager implements Shopping {

    private List  products;
    private double totalPrice;

    @PersistenceContext(unitName = &quot;ShoppingManager&quot;)
    private EntityManager entityManager;

    @Create
    public void createProducts () {

        Product product = null;
        product = createProduct(&quot;Sony Playstation&quot;,
            &quot;Gaming Console The Sony Playstation 2&quot;, 9000d, 0);
        entityManager.persist(product);

        product = createProduct(&quot;Traffic Signal - DVD&quot;,
            &quot;SKU-CODE: Traffic Signal - Sony &amp;amp; BMG&quot;, 240d, 0);
        entityManager.persist(product);

        product = createProduct(&quot;DVD Player&quot;,
            &quot;Samsung P475 DVD Player&quot;, 3990d, 0);
        entityManager.persist(product);

        product = createProduct(&quot;COMPAQ Notebook&quot;,
            &quot;COMPAQ Notebook PC - PRESARIO V6307&quot;, 49000d, 0);
        entityManager.persist(product);

        products = entityManager.createQuery(
            &quot;select prdt from Product prdt&quot;).getResultList();
    }

    private Product createProduct(String name, String description,
        double price, int noOfItems){

        Product product = new Product();
        product.setName(name);
        product.setDescription(description);
        product.setPrice(price);
        product.setNoOfItems(noOfItems);
        return product;
    }

    public List  getProducts() {
        return products;
    }

    public String checkout(){
        for(Product aProduct : products){
            aProduct.setAmount(aProduct.getNoOfItems() * aProduct.getPrice());
            totalPrice = totalPrice + (aProduct.getNoOfItems() * aProduct.getPrice());
        }
    return &quot;checkout&quot;;
    }

    public double getTotalPrice(){
        return totalPrice;
    }

    @Destroy
    @Remove()
    public void destroyProducts(){
    }
}</pre>
<p align="justify">As already explained, the above code makes use of EntityManager to create and retrieve products from the products Table.</p>
<h2>8) Conclusion</h2>
<p align="justify">This is an Introduction article about the evolving JBoss Seam Framework. It started with the necessity of having this Framework and then continued with the discussion of having some many advantages of using this framework. The advantages like <strong><em>&#8220;EJB-JSF&#8221;</em></strong> Integration, the need for a <strong><em>Stateful Web Application</em></strong> and the exiting <strong><em>Dependency Bijection Functionality</em></strong> is covered in much detailed manner. Then the various core modules of the Seam Framework like <strong><em>Seam Components</em></strong>, the need and the usage of <strong><em>Seam Annotations</em></strong> over various modules along with the <strong><em>Various Types of Events</em></strong> emited by Seam Sources are given much consideration and discussed briefly. Then the article finally ended up with a <strong><em>Shopping Application</em></strong> thereby illustrating the various techniques and concepts available in JBoss Seam.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2007/06/introduction-to-jboss-seam/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
