<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Lux Log</title>
	<atom:link href="http://luxlog.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://luxlog.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Fri, 14 Jan 2011 07:13:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='luxlog.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Lux Log</title>
		<link>http://luxlog.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://luxlog.wordpress.com/osd.xml" title="Lux Log" />
	<atom:link rel='hub' href='http://luxlog.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Part 3 of 3 Basic web app completed with validation elements</title>
		<link>http://luxlog.wordpress.com/2007/12/22/basic-web-app-completed-with-validation-elements/</link>
		<comments>http://luxlog.wordpress.com/2007/12/22/basic-web-app-completed-with-validation-elements/#comments</comments>
		<pubDate>Sat, 22 Dec 2007 06:06:08 +0000</pubDate>
		<dc:creator>Luk</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>

		<guid isPermaLink="false">http://luxlog.wordpress.com/2007/12/22/basic-web-app-completed-with-validation-elements/</guid>
		<description><![CDATA[Up til now, in part 1 and part 2 there was hardly any validation. Everything was of type String, and the only constraint we implemented was for some fields to be required. That&#8217;s not very real world. Let&#8217;s make our application more robust by adding some extra constraints such as checks on correct date and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luxlog.wordpress.com&amp;blog=1809170&amp;post=33&amp;subd=luxlog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Up til now, in <a href="http://luxlog.wordpress.com/2007/12/15/basic-example-web-app-with-glassfish-netbeans-60-jsf-and-ejb3/">part 1</a> and <a href="http://luxlog.wordpress.com/2007/12/17/basic-web-app-extended-to-full-crud/">part 2</a> there was hardly any validation. Everything was of type String, and the only constraint we implemented was for some fields to be required. That&#8217;s not very real world. Let&#8217;s make our application more robust by adding some extra constraints such as checks on correct date and e-mail formatting, a check on whether the provided username is unique and checks on min and max length of strings. For this purpose we are using JSF validation (mainly).<span id="more-33"></span></p>
<ul>
<li>First we extend the User class with three attributes: email, active, dateCreated.</li>
<li>Then we  extend the view by adding validation capabilities and customizing the feedback JSF provides.</li>
<li>Finally we also change our entity and session beans to check the uniqueness of the username.</li>
</ul>
<p>Contrary to part 1 and part 2 in this part we are going to use software that is <i>not</i> in the Glassfish box (i.e. not part of the JavaEE spec). Instead we are going to use (a small part) of the widely used Apache Commons.</p>
<blockquote><p>To carry out the steps outlined below you first have to finish both <a href="http://luxlog.wordpress.com/2007/12/15/basic-example-web-app-with-glassfish-netbeans-60-jsf-and-ejb3/">part one</a> and <a href="http://luxlog.wordpress.com/2007/12/17/basic-web-app-extended-to-full-crud/">part two</a>. Just as in the previous parts this tutorial&#8217;s goal is too teach you web development with Java, GlassfishV2 and PostgreSQL.</p></blockquote>
<h2>Table of contents</h2>
<ul>
<li><a href="#step1">Step 1: Add new columns to underlying database</a></li>
<li><a href="#step2">Step 2: Add fields to the User Entity Bean</a></li>
<li><a href="#step3">Step 3: Add Apache Commons validation libraries</a></li>
<li><a href="#step4">Step 4: Add validator classes to web application</a></li>
<li><a href="#step5">Step 5: Modify the view (add new fields and validation elements)</a></li>
<li><a href="#step6">Step 6: Bind validator attributes to classes in faces-config.xml</a></li>
<li><a href="#step7">Step 7: Run the application</a></li>
<li><a href="#step8">Step 8: Check whether username is unique</a></li>
<li><a href="#all_together">Putting it all together</a></li>
</ul>
<h2><a title="step1" name="step1"></a>Step 1: Add new columns to underlying database</h2>
<p>Before adding three fields to the User object: email, active and date_created we need to add the counterpart columns to the database.</p>
<ol>
<li>Start psql by issuing the command <code>sudo psql -U postgres -d test_db</code> or open PgAdminIII.</li>
<li> Run the SQL statements below.</li>
<li> Quit psql by issuing the command <code>\q</code>.</li>
</ol>
<p><code><br />
ALTER TABLE users ADD COLUMN email varchar(255);<br />
ALTER TABLE users ADD COLUMN active boolean NOT NULL DEFAULT true;<br />
ALTER TABLE users ADD COLUMN date_created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP;</code></p>
<p><a href="http://luxlog.files.wordpress.com/2007/12/3100.png" title="3100.png"><img src="http://luxlog.files.wordpress.com/2007/12/3100.thumbnail.png" alt="3100.png" /></a></p>
<h2><a title="step2" name="step2"></a>Step 2: Add fields to the User Entity Bean</h2>
<ol>
<li>Start up NetBeans v6</li>
<li>Right-click the page and select <b>Refactor</b> &gt; <b>Encapsulate Fields&#8230;</b></li>
<li>Add fields to the class (see below).</li>
<li>Activate &#8220;Create Getter&#8221; and &#8220;Create Setter&#8221; for all three newly added fields.</li>
<li>Push the <b>Refactor</b> button</li>
</ol>
<pre class="brush: java;">
@Column(name = &quot;email&quot;)
private String email;
@Column(name = &quot;active&quot;, nullable= false)
private boolean active;
@Column(name = &quot;date_created&quot;, nullable = false)
@Temporal(value = TemporalType.TIMESTAMP)
private Date dateCreated;</pre>
<p><a href="http://luxlog.files.wordpress.com/2007/12/3200.png" title="3200.png"><img src="http://luxlog.files.wordpress.com/2007/12/3200.thumbnail.png" alt="3200.png" /></a><a href="http://luxlog.files.wordpress.com/2007/12/3300.png" title="3300.png"><img src="http://luxlog.files.wordpress.com/2007/12/3300.thumbnail.png" alt="3300.png" /></a>Further down this article you&#8217;ll find the complete User.java file with all modifications.<a href="http://luxlog.files.wordpress.com/2007/12/3200.png" title="3200.png"></a></p>
<h2><a title="step3" name="step3"></a>Step 3: Add Apache Commons validation libraries</h2>
<p>Although it might seam fun at first sight to develop our own validation algorithms, that would be very unwise since it&#8217;s all been thought through and through, and developed through and through before. Let&#8217;s use that wisdom to support our validation rules, in the form of the widely supported Apache Commons. Download distributions containing these jars:</p>
<ul>
<li>commons-lang-x.y.jar &#8211; <a href="http://commons.apache.org/lang/"></a>http://commons.apache.org/lang/</li>
<li>commons-validator-x.y.jar &#8211; <a href="http://commons.apache.org/validator/">http://commons.apache.org/validator/</a></li>
<li>jakarta-oro-x.y.jar &#8211; <a href="http://jakarta.apache.org/oro/">http://jakarta.apache.org/oro/</a> (one of the above, I forgot which one, depends on this archive)</li>
</ul>
<ol>
<li>Right-click <b>JumpStartEjbJsf-war</b> &gt; <b>Libraries</b> and select <b>Add JAR/folder&#8230;</b>.</li>
<li>Select the three files and click <b>Open</b>.</li>
<li>The result is that the files are added to the libary as depicted below.</li>
</ol>
<p><a href="http://luxlog.files.wordpress.com/2007/12/3400.png" title="3400.png"><img src="http://luxlog.files.wordpress.com/2007/12/3400.thumbnail.png" alt="3400.png" /></a></p>
<h2><a title="step4" name="step4"></a>Step 4: Add validator classes to web application</h2>
<p>In the next steps we create validation rules for the following constraints.</p>
<table cellpadding="5">
<tr>
<td>Field</td>
<td>Required</td>
<td>Constraints</td>
</tr>
<tr>
<td>Username</td>
<td align="center">X</td>
<td>min 2 max 30 chars, unique</td>
</tr>
<tr>
<td>First name</td>
<td align="center">&nbsp;</td>
<td>min 2 max 100 chars, no number</td>
</tr>
<tr>
<td>Last name</td>
<td align="center">&nbsp;</td>
<td>min 2 max 100 chars, no number</td>
</tr>
<tr>
<td>Password</td>
<td align="center">X</td>
<td>min 4 max 12 chars</td>
</tr>
<tr>
<td>E-mail</td>
<td align="center">&nbsp;</td>
<td>min 2 max 100 chars, valid e-mailaddress</td>
</tr>
<tr>
<td>Active</td>
<td align="center">X</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Date created</td>
<td align="center">X</td>
<td>valid date in format MM/dd/yyyy</td>
</tr>
</table>
<p>To check whether, if provided, the e-mail address is valid, and the username, first and last name only contain alphabetic characters we have to create our own Validator beans. These beans are JSF managed and implement <b>javax.faces.validator.Validator</b>. We start by creating the <b>EmailValidator</b> bean.</p>
<ol>
<li>Right-click <b>JumpStartEjbJsf-war</b> &gt; <b>Source Packages</b> &gt; <b>New&#8230;</b> and select <b>New Java Class&#8230;</b>.</li>
<li>In the <b>New Java Class</b> dialog box, fill in:</li>
</ol>
<table cellpadding="5">
<tr>
<td>ClassName</td>
<td>EmailValidator</td>
</tr>
<tr>
<td>Package</td>
<td>lux.validators</td>
</tr>
</table>
<p>Here is the source code for the validator JSF managed bean:</p>
<pre class="brush: java;">
package lux.validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class EmailValidator implements Validator
{

  public void validate(FacesContext facesContext, UIComponent uiComponent,
      Object value) throws ValidatorException
  {
    org.apache.commons.validator.EmailValidator emailValidator =
            org.apache.commons.validator.EmailValidator.getInstance();
    HtmlInputText htmlInputText = (HtmlInputText) uiComponent;

    if (!emailValidator.isValid((String) value))
    {
      FacesMessage facesMessage = new FacesMessage(htmlInputText.getLabel()
          + &quot;: E-mail format is not valid.&quot;);
      throw new ValidatorException(facesMessage);
    }
  }

}</pre>
<p>Repeat this to validate that first name and last name only contain alphabetic characters by adding the AlphaValidator class.</p>
<ol>
<li>Right-click <b>JumpStartEjbJsf-war</b> &gt; <b>Source Packages</b> &gt; <b>New&#8230;</b> and select <b>New Java Class&#8230;</b>.</li>
<li>In the <b>New Java Class</b> dialog box, fill in:</li>
</ol>
<table cellpadding="5">
<tr>
<td>ClassName</td>
<td>AlphaValidator</td>
</tr>
<tr>
<td>Package</td>
<td>lux.validators</td>
</tr>
</table>
<p>Here is the source code for the validator JSF managed bean:</p>
<pre class="brush: java;">
package lux.validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import org.apache.commons.lang.StringUtils;

public class AlphaValidator implements Validator {
    public void validate(FacesContext fc, UIComponent uiC, Object o) throws ValidatorException {
        if (!StringUtils.isAlphaSpace((String) o)) {
            HtmlInputText i = (HtmlInputText) uiC;
            FacesMessage f = new FacesMessage(i.getLabel() + &quot;: &quot; +
                    &quot;only alphabetic&quot; + &quot; characters are allowed.&quot;);
            throw new ValidatorException(f);
        }
    }
}</pre>
<p>The Source Packages folder of your web project should now be in this structure:<code><br />
Source Packages<br />
| lux.controllers<br />
| | UserController.java<br />
| lux.validators<br />
| | AlphaValidator.java<br />
| | EmailValidator.java<br />
</code></p>
<h2><a title="step5" name="step5"></a>Step 5: Modify the view (add new fields and validation elements)</h2>
<p>In this step we modify adduser.jsf. Let&#8217;s start by adding form elements for the new fields:<code><br />
&lt;h:outputText value="E-mail" /&gt;<br />
&lt;h:inputText label="E-mail" value="#{user.user.email}" required="true" /&gt;<br />
&lt;h:outputText value="Active" /&gt;<br />
&lt;h:selectBooleanCheckbox label="active" value="#{user.user.active}" /&gt;<br />
&lt;h:outputText value="Date created" /&gt;<br />
&lt;h:inputText label="Date created" value="#{user.user.dateCreated}" required="true" /&gt;<br />
</code></p>
<p>Now add the validation rules for all elements (the fact that user name has to be unique is taken care of later). You add validation rules <i>within</i> the inputText element</p>
<p>1. For username add <code>&lt;f:validateLength minimum="2" maximum="30"/&gt;</code>.<br />
2. For first name add <code>&lt;f:validateLength minimum="2" maximum="100"/&gt;</code> AND <code>&lt;f:validator validatorId="alphaValidator"/&gt;</code>.<br />
3. For last name add <code>&lt;f:validateLength minimum="2" maximum="100"/&gt;</code> AND <code>&lt;f:validator validatorId="alphaValidator"/&gt;</code>.<br />
4. For password add <code>&lt;f:validateLength minimum="4" maximum="15"/&gt;</code>.<br />
5. For e-mail add <code>&lt;f:validator validatorId="emailValidator"/&gt;</code>.<br />
6. For handling date data we use a converter element <code>&lt;f:convertDateTime pattern="dd/MM/yyyy"/&gt;</code>.</p>
<p>The complete adduser.jsp file is available at the end of this article.</p>
<h2><a title="step6" name="step6"></a>Step 6: Bind validator attributes to classes in faces-config.xml</h2>
<p>Last step is to bind the attributes in the JSF elements to the classes. You do this by editing faces-config.xml</p>
<p>1. Click <b>JumpStartEjbJsf-war</b> &lt; <b>Configuration Files</b> &lt; and open the file <b>faces-config.xml</b><br />
2. Click the <b>XML</b> button on top of the page<br />
3. Add the <code>&lt;validator&gt;</code> elements within the <code>&lt;faces-config&gt;</code> element.</p>
<pre class="brush: xml;">
    &lt;!-- #3 added validators --&gt;
    &lt;validator&gt;
        &lt;validator-id&gt;emailValidator&lt;/validator-id&gt;
        &lt;validator-class&gt;lux.validators.EmailValidator&lt;/validator-class&gt;
    &lt;/validator&gt;
    &lt;validator&gt;
        &lt;validator-id&gt;alphaValidator&lt;/validator-id&gt;
        &lt;validator-class&gt;lux.validators.AlphaValidator&lt;/validator-class&gt;
    &lt;/validator&gt;</pre>
<h2><a title="step7" name="step7"></a>Step 7: Run the application</h2>
<p>Consider what we have done up til now part 3a. You can run it (by clicking F6) and see whether it works. If it does you can move on the step 8. That is, if you want, since the next step is slightly off-topic. It is also about business rules and constraints but we will not be using JSF validation&#8230;</p>
<h2><a title="step8" name="step8"></a>Step 8: Check whether username is unique</h2>
<p>To check whether a user name is unique we need to query the database and communicate the results of this query back to the view. Out of performance and manageability concerns we will use a Named Query on our Entity Bean. The results from that query are used in a new business method <code>usernameExists(String username)</code> in our Session Bean to return a boolean. That boolean is picked up in the Controller action method <code>createUser</code> to either allow the User object to be persisted or to give feedback back to the user. This means we have to modify the backend Entity Bean, and Session Bean, and the frontend Controller. Below we first highlight the changes and then we&#8217;ll show the complete source per file again.</p>
<p>First we modify the User Entity Bean (<b>JumpStartEjbJsf-ejb</b> &gt; <b>Source Package</b> &gt; <b>lux.domain</b> &gt; <b>User.java</b>) by adding the namedQuery annotation to the class definition.</p>
<pre class="brush: java;">
// #3: cached query returning user by username
@NamedQueries
({
    @NamedQuery(
        name = &quot;User.findByUsername&quot;,
        query = &quot;SELECT u FROM User u WHERE u.username = :username&quot;)
})
public class User implements Serializable {</pre>
<p>Next we modify the UserDAO Session Bean (<b>JumpStartEjbJsf-ejb</b> &gt; <b>Source Package</b> &gt; <b>lux.facade</b> &gt; <b>UserDAOBean.java</b>) by adding a new method <code>usernameExists(String username)</code></p>
<pre class="brush: java;">
// #3: check whether username exists
public boolean usernameExists(String username) {
    List users = new LinkedList();
    Query query = em.createNamedQuery(&quot;User.findByUsername&quot;);
    query.setParameter(&quot;username&quot;, username);
    users = query.getResultList();
    if (users.isEmpty()) {
        return false;
    } else {
        return true;
    }
}</pre>
<p>Also change the interface.</p>
<pre class="brush: java;">
// #3 new method exposed
public boolean usernameExists(java.lang.String username);</pre>
<p>Finally in your web project modify the controller bean:</p>
<pre class="brush: java;">
if (user.getUserId() == null) {
    // #3: prohibit existing user names
    boolean usernameExists = userDao.usernameExists(user.getUsername());
    if (!usernameExists) {
        userDao.createUser(user);
    } else {
        addErrorMessage(&quot;Username &quot; + user.getUsername() + &quot; already exists.&quot;);
        r = &quot;try_again&quot;;
    }
} else {
    userDao.updateUser(user);
}</pre>
<p>You might have noticed a new return string &#8220;try_again&#8221; that&#8217;s not been defined in faces-config. This is a rather filthy workaround to keep the user on the current page. Whenever a return string is not recognized by the container, the user stays on the originating page.</p>
<h2><a title="step9" name="step9"></a>Step 9: Modify edituser.jsp</h2>
<p>Not much left to learn here. If you&#8217;ve come this far, congratulations. In this last step we adapt edituser.jsp to have a completed application up and running. The new edit page (<b>JumpStartEjbJsf-war</b> &gt; <b>Web Page</b> &gt; <b>edituser.jsp</b>) then looks like this:</p>
<pre class="brush: xml;">
&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;

&lt;%@taglib prefix=&quot;f&quot; uri=&quot;http://java.sun.com/jsf/core&quot;%&gt;
&lt;%@taglib prefix=&quot;h&quot; uri=&quot;http://java.sun.com/jsf/html&quot;%&gt;

&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;Edit user&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;f:view&gt;
            &lt;h1&gt;Edit user &lt;h:outputText value=&quot;#{user.user.username}&quot; /&gt;&lt;/h1&gt;
            &lt;h:form&gt;
                &lt;h:messages/&gt;
                &lt;h:panelGrid columns=&quot;2&quot;&gt;
                    &lt;h:outputText value=&quot;Username&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;Username&quot;
                        value=&quot;#{user.user.username}&quot;
                        disabled=&quot;true&quot; /&gt;
                    &lt;h:outputText value=&quot;First name&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;First name&quot;
                        value=&quot;#{user.user.firstName}&quot;&gt;
                        &lt;f:validateLength minimum=&quot;2&quot; maximum=&quot;100&quot;/&gt;
                        &lt;f:validator validatorId=&quot;alphaValidator&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:outputText value=&quot;Last name&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;Last name&quot;
                        value=&quot;#{user.user.lastName}&quot;&gt;
                        &lt;f:validateLength minimum=&quot;2&quot; maximum=&quot;100&quot;/&gt;
                        &lt;f:validator validatorId=&quot;alphaValidator&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:outputText value=&quot;E-mail&quot; /&gt;
                    &lt;h:inputText
                        label=&quot;E-mail&quot;
                        value=&quot;#{user.user.email}&quot;&gt;
                        &lt;f:validator validatorId=&quot;emailValidator&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:outputText value=&quot;Active&quot; /&gt;
                    &lt;h:selectBooleanCheckbox
                        label=&quot;active&quot;
                        value=&quot;#{user.user.active}&quot; /&gt;
                    &lt;h:outputText value=&quot;Date created&quot; /&gt;
                    &lt;h:inputText
                        label=&quot;Date created&quot;
                        value=&quot;#{user.user.dateCreated}&quot;
                        required=&quot;true&quot;&gt;
                        &lt;f:convertDateTime pattern=&quot;MM/dd/yyyy&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:panelGroup/&gt;
                    &lt;h:commandButton
                        action=&quot;#{user.saveUser}&quot;
                        value=&quot;Save&quot;/&gt;
                &lt;/h:panelGrid&gt;
            &lt;/h:form&gt;
        &lt;/f:view&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p>One last add-on we had to do to really complete the application is to have the active button selected per default when creating a new user. We do this by modifying the <code>createUser()</code> method in UserController.java:</p>
<pre class="brush: xml;">
public String createUser() {
    this.user = new User();
    // #3: active is per default true
    this.user.setActive(true);
    return &quot;create_new_user&quot;;
}</pre>
<h2><a title="all_together" name="all_together"></a>Putting it all together</h2>
<blockquote><p>That&#8217;s it. I hope you share my conviction that developing web apps with Java is a good choice, especially since Sun has released open source, free software versions of their Java products. Depending on your feedback the next arcticle might be about not only making it a good choice but also making it fun, by adding in Seam and Facelets.</p></blockquote>
<p>To wrap it up, below a complete listing of all the files we edited.</p>
<h3>The User Entity Bean</h3>
<p>JumpStartEjbJsf-ejb &gt; Source Packages &gt; lux.domain &gt; User.java</p>
<pre class="brush: java;">
package lux.domain;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = &quot;users&quot;)
// #3: cached query returning user by username
@NamedQueries
({
    @NamedQuery(
        name = &quot;User.findByUsername&quot;,
        query = &quot;SELECT u FROM User u WHERE u.username = :username&quot;)
})
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator=&quot;users_seq_generator&quot;)
    @SequenceGenerator(name=&quot;users_seq_generator&quot;, sequenceName=&quot;users_user_id_seq&quot;)
    @Column(name = &quot;user_id&quot;, nullable = false)
    private Integer userId;
    @Column(name = &quot;username&quot;, nullable = false)
    private String username;
    @Column(name = &quot;first_name&quot;)
    private String firstName;
    @Column(name = &quot;last_name&quot;)
    private String lastName;
    @Column(name = &quot;password&quot;, nullable = false)
    private String password;
    // #3: newly added fields
    @Column(name = &quot;email&quot;)
    private String email;
    @Column(name = &quot;active&quot;, nullable= false)
    private boolean active;
    @Column(name = &quot;date_created&quot;, nullable= false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateCreated;

    public User() {
    }

    public User(Integer userId) {
        this.userId = userId;
    }

    public User(Integer userId, String username, String password) {
        this.userId = userId;
        this.username = username;
        this.password = password;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (userId != null ? userId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.userId == null &amp;&amp; other.userId != null) || (this.userId != null &amp;&amp; !this.userId.equals(other.userId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return &quot;lux.domain.User[userId=&quot; + userId + &quot;]&quot;;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

}</pre>
<h3>The UserDAO Stateless Session Bean</h3>
<p>JumpStartEjbJsf-ejb &gt; Source Packages &gt; lux.facade &gt; UserDAOBean.java</p>
<pre class="brush: java;">
package lux.facade;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import lux.domain.User;

@Stateless
public class UserDAOBean implements UserDAO {

    @PersistenceContext
    private EntityManager em;

    public User getUser(int UserId) {
        User u = new User();
        u = em.find(User.class, UserId);
        return u;
    }

    public List&lt;User&gt; getAllUsers() {
        Query q = em.createQuery(&quot;SELECT u FROM User u&quot;);
        List&lt;User&gt; users = q.getResultList();
        return users;
    }

    public void createUser(User u) {
        String hashedPw = hashPassword(u.getPassword());
        u.setPassword(hashedPw);
        if (!usernameExists(u.getUsername())) {
            em.persist(u);
        }
    }

    // #2 changed: created hash on hash
    public void updateUser(User u) {
        String pw = u.getPassword();
        u.setPassword(pw);
        em.merge(u);
    }

    // #2: modified, didn't work
    public void deleteUser(User u) {
        User mgdUser = em.merge(u);
        em.remove(mgdUser);
    }

    // #3: check whether username exists
    public boolean usernameExists(String username) {
        List&lt;User&gt; users = new LinkedList&lt;User&gt;();
        Query query = em.createNamedQuery(&quot;User.findByUsername&quot;);
        query.setParameter(&quot;username&quot;, username);
        users = query.getResultList();
        if (users.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    private String hashPassword(String password) {
        StringBuilder sb = new StringBuilder();

        try {
            MessageDigest messageDigest = MessageDigest.getInstance(&quot;SHA&quot;);
            byte[] bs;
            bs = messageDigest.digest(password.getBytes());

            for (int i = 0; i &lt; bs.length; i++) {
                String hexVal = Integer.toHexString(0xFF &amp; bs[i]);
                if (hexVal.length() == 1) {
                    sb.append(&quot;0&quot;);
                }
                sb.append(hexVal);
            }

        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(UserDAOBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return sb.toString();
    }
}</pre>
<h3>The UserDAO Interface</h3>
<p>JumpStartEjbJsf-ejb &gt; Source Packages &gt; lux.facade &gt; UserDAO.java</p>
<pre class="brush: java;">
package lux.facade;

import javax.ejb.Local;

@Local
public interface UserDAO {
    public lux.domain.User getUser(int UserId);
    public void createUser(lux.domain.User u);
    public void updateUser(lux.domain.User u);
    public void deleteUser(lux.domain.User u);
    public java.util.List&lt;lux.domain.User&gt; getAllUsers();
    // #3 new method exposed
    public boolean usernameExists(java.lang.String username);
}</pre>
<h3>The Controller</h3>
<p>JumpStartEjbJsf-war &gt; Source Packages &gt; lux.controllers &gt; UserController.java</p>
<pre class="brush: java;">
package lux.controllers;

import java.util.Map;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import lux.domain.User;
import lux.facade.UserDAO;

public class UserController {

    @EJB
    UserDAO userDao;
    private User user;
    private DataModel model;

    public String createUser() {
        this.user = new User();
        // #3: active is per default true
        this.user.setActive(true);
        return &quot;create_new_user&quot;;
    }

    // #2: extended with condition to check whether it is create or update
    public String saveUser() {
        String r = &quot;success&quot;;

        try {
            if (user.getUserId() == null) {
                // #3: prohibit existing user names
                boolean usernameExists = userDao.usernameExists(user.getUsername());
                if (!usernameExists) {
                    userDao.createUser(user);
                } else {
                    addErrorMessage(&quot;Username &quot; + user.getUsername() + &quot; already exists.&quot;);
                    r = &quot;try_again&quot;;
                }
            } else {
                userDao.updateUser(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
            r = &quot;failed&quot;;
        }

        return r;
    }

    public DataModel getUsers() {
        model = new ListDataModel(userDao.getAllUsers());
        return model;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    // #2: delete action backing bean method
    public String deleteUser() {
        String r = &quot;user_deleted&quot;;
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestParameterMap = (Map) context.getExternalContext().getRequestParameterMap();

        try {
            Integer userId = Integer.parseInt(requestParameterMap.get(&quot;userId&quot;).toString());
            User u = userDao.getUser(userId);
            String userName = u.getUsername();
            addSuccessMessage(&quot;User &quot; + userName + &quot; successfully deleted.&quot;);
            userDao.deleteUser(u);
        } catch (NumberFormatException ne) {
            addErrorMessage(ne.getLocalizedMessage());
            r = &quot;failed&quot;;
        } catch (Exception e) {
            addErrorMessage(e.getLocalizedMessage());
            r = &quot;failed&quot;;
        }

        return r;
    }

    // #2: edit action backing bean method
    public String editUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestParameterMap = (Map) context.getExternalContext().getRequestParameterMap();
        Integer userId = Integer.parseInt(requestParameterMap.get(&quot;userId&quot;).toString());
        this.user = userDao.getUser(userId);
        return &quot;edit_user&quot;;
    }

    // #2: helper method for printing error message in jsf page
    private void addErrorMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage(null, facesMsg);
    }

    // #2: helper method for printing feedback in jsf page
    private void addSuccessMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage(&quot;successInfo&quot;, facesMsg);
    }
}</pre>
<h3>The AlphaValidator</h3>
<p>JumpStartEjbJsf-war &gt; Source Packages &gt; lux.validators &gt; AlphaValidator.java</p>
<pre class="brush: java;">
package lux.validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import org.apache.commons.lang.StringUtils;

public class AlphaValidator implements Validator {
    public void validate(FacesContext fc, UIComponent uiC, Object o) throws ValidatorException {
        if (!StringUtils.isAlphaSpace((String) o)) {
            HtmlInputText i = (HtmlInputText) uiC;
            FacesMessage f = new FacesMessage(i.getLabel() + &quot;: &quot; +
                    &quot;only alphabetic&quot; + &quot; characters are allowed.&quot;);
            throw new ValidatorException(f);
        }
    }
}</pre>
<h3>The EmailValidator</h3>
<p>JumpStartEjbJsf-war &gt; Source Packages &gt; lux.validators &gt; EmailValidator.java</p>
<pre class="brush: java;">
package lux.validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class EmailValidator implements Validator
{

  public void validate(FacesContext facesContext, UIComponent uiComponent,
      Object value) throws ValidatorException
  {
    org.apache.commons.validator.EmailValidator emailValidator =
            org.apache.commons.validator.EmailValidator.getInstance();
    HtmlInputText htmlInputText = (HtmlInputText) uiComponent;

    if (!emailValidator.isValid((String) value))
    {
      FacesMessage facesMessage = new FacesMessage(htmlInputText.getLabel()
          + &quot;: E-mail format is not valid.&quot;);
      throw new ValidatorException(facesMessage);
    }
  }

}</pre>
<h3>faces-config</h3>
<p>JumpStartEjbJsf-war &gt; Configuration Files &gt; faces-config.xml</p>
<pre class="brush: xml;">&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;managed-bean&gt;
        &lt;managed-bean-name&gt;user&lt;/managed-bean-name&gt;
        &lt;managed-bean-class&gt;lux.controllers.UserController&lt;/managed-bean-class&gt;
        &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;
    &lt;/managed-bean&gt;
    &lt;navigation-rule&gt;
        &lt;from-view-id&gt;/index.jsp&lt;/from-view-id&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;create_new_user&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/adduser.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;edit_user&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/edituser.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;failed&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/failed.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
    &lt;/navigation-rule&gt;
    &lt;navigation-rule&gt;
        &lt;from-view-id&gt;/adduser.jsp&lt;/from-view-id&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;failed&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/failed.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;success&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/index.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
    &lt;/navigation-rule&gt;
    &lt;navigation-rule&gt;
        &lt;from-view-id&gt;/edituser.jsp&lt;/from-view-id&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;failed&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/failed.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;success&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/index.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
    &lt;/navigation-rule&gt;
    &lt;validator&gt;
        &lt;validator-id&gt;emailValidator&lt;/validator-id&gt;
        &lt;validator-class&gt;lux.validators.EmailValidator&lt;/validator-class&gt;
    &lt;/validator&gt;
    &lt;validator&gt;
        &lt;validator-id&gt;alphaValidator&lt;/validator-id&gt;
        &lt;validator-class&gt;lux.validators.AlphaValidator&lt;/validator-class&gt;
    &lt;/validator&gt;
&lt;/faces-config&gt;</pre>
<h3>The list view</h3>
<p>JumpStartEjbJsf-war &gt; Web Pages &gt; index.jsp</p>
<pre class="brush: xml;">
&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib prefix=&quot;f&quot; uri=&quot;http://java.sun.com/jsf/core&quot;%&gt;
&lt;%@taglib prefix=&quot;h&quot; uri=&quot;http://java.sun.com/jsf/html&quot;%&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;User Listing&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;f:view&gt;
            &lt;h:form&gt;
                &lt;!-- #2 added messages element for delete feedback --&gt;
                &lt;h:messages&gt;&lt;/h:messages&gt;
                &lt;h1&gt;&lt;h:outputText value=&quot;User Listing&quot;/&gt;&lt;/h1&gt;
                &lt;p&gt;&lt;h:commandLink action=&quot;#{user.createUser}&quot; value=&quot;Create a user&quot;/&gt;&lt;/p&gt;
                &lt;h:dataTable value=&quot;#{user.users}&quot;
                             var=&quot;dataTableItem&quot; border=&quot;1&quot; cellpadding=&quot;2&quot; cellspacing=&quot;2&quot;&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;Username&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:outputText value=&quot;#{dataTableItem.username}&quot; /&gt;
                    &lt;/h:column&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;First name&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:outputText value=&quot;#{dataTableItem.firstName}&quot; /&gt;
                    &lt;/h:column&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;Last name&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:outputText value=&quot;#{dataTableItem.lastName}&quot; /&gt;
                    &lt;/h:column&gt;
                    &lt;!-- #2 added column with edit and delete commandLink
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;Edit&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:commandLink action=&quot;#{user.editUser}&quot;&gt;
                            &lt;h:outputText value=&quot;Edit&quot;/&gt;
                            &lt;f:param name=&quot;userId&quot; value=&quot;#{dataTableItem.userId}&quot; /&gt;
                        &lt;/h:commandLink&gt;
                    &lt;/h:column&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;Delete&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:commandLink action=&quot;#{user.deleteUser}&quot;&gt;
                            &lt;h:outputText value=&quot;Delete&quot;/&gt;
                            &lt;f:param name=&quot;userId&quot; value=&quot;#{dataTableItem.userId}&quot; /&gt;
                        &lt;/h:commandLink&gt;
                    &lt;/h:column&gt;
                &lt;/h:dataTable&gt;
            &lt;/h:form&gt;
        &lt;/f:view&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<h3>The create view</h3>
<p>JumpStartEjbJsf-war &gt; Web Pages &gt; adduser.jsp</p>
<pre class="brush: xml;">
&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib prefix=&quot;f&quot; uri=&quot;http://java.sun.com/jsf/core&quot;%&gt;
&lt;%@taglib prefix=&quot;h&quot; uri=&quot;http://java.sun.com/jsf/html&quot;%&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;New user&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;f:view&gt;
            &lt;h:form&gt;
                &lt;h:messages styleClass=&quot;error_message&quot; /&gt;
                &lt;h:panelGrid columns=&quot;2&quot;&gt;
                    &lt;h:outputText value=&quot;Username&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;Username&quot;
                        value=&quot;#{user.user.username}&quot;
                        required=&quot;true&quot;&gt;
                        &lt;f:validateLength minimum=&quot;2&quot; maximum=&quot;30&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:outputText value=&quot;First name&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;First name&quot;
                        value=&quot;#{user.user.firstName}&quot;&gt;
                        &lt;f:validateLength minimum=&quot;2&quot; maximum=&quot;100&quot;/&gt;
                        &lt;f:validator validatorId=&quot;alphaValidator&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:outputText value=&quot;Last name&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;Last name&quot;
                        value=&quot;#{user.user.lastName}&quot;&gt;
                        &lt;f:validateLength minimum=&quot;2&quot; maximum=&quot;100&quot;/&gt;
                        &lt;f:validator validatorId=&quot;alphaValidator&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:outputText value=&quot;Password&quot; /&gt;
                    &lt;h:inputSecret
                        label=&quot;Password&quot;
                        value=&quot;#{user.user.password}&quot;
                        required=&quot;true&quot;&gt;
                        &lt;f:validateLength minimum=&quot;4&quot; maximum=&quot;15&quot;/&gt;
                    &lt;/h:inputSecret&gt;
                    &lt;h:outputText value=&quot;E-mail&quot; /&gt;
                    &lt;h:inputText
                        label=&quot;E-mail&quot;
                        value=&quot;#{user.user.email}&quot;&gt;
                        &lt;f:validator validatorId=&quot;emailValidator&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:outputText value=&quot;Active&quot; /&gt;
                    &lt;h:selectBooleanCheckbox
                        label=&quot;active&quot;
                        value=&quot;#{user.user.active}&quot; /&gt;
                    &lt;h:outputText value=&quot;Date created&quot; /&gt;
                    &lt;h:inputText
                        label=&quot;Date created&quot;
                        value=&quot;#{user.user.dateCreated}&quot;
                        required=&quot;true&quot;&gt;
                        &lt;f:convertDateTime pattern=&quot;MM/dd/yyyy&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:panelGroup/&gt;
                    &lt;h:commandButton
                        action=&quot;#{user.saveUser}&quot;
                        value=&quot;Save&quot;/&gt;
                &lt;/h:panelGrid&gt;
            &lt;/h:form&gt;
        &lt;/f:view&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<h3>The edit view</h3>
<p>JumpStartEjbJsf-war &gt; Web Pages &gt; edituser.jsp</p>
<pre class="brush: xml;">&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib prefix=&quot;f&quot; uri=&quot;http://java.sun.com/jsf/core&quot;%&gt;
&lt;%@taglib prefix=&quot;h&quot; uri=&quot;http://java.sun.com/jsf/html&quot;%&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;Edit user&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;f:view&gt;
            &lt;h1&gt;Edit user &lt;h:outputText value=&quot;#{user.user.username}&quot; /&gt;&lt;/h1&gt;
            &lt;h:form&gt;
                &lt;h:messages/&gt;
                &lt;h:panelGrid columns=&quot;2&quot;&gt;
                    &lt;h:outputText value=&quot;Username&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;Username&quot;
                        value=&quot;#{user.user.username}&quot;
                        disabled=&quot;true&quot; /&gt;
                    &lt;h:outputText value=&quot;First name&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;First name&quot;
                        value=&quot;#{user.user.firstName}&quot;&gt;
                        &lt;f:validateLength minimum=&quot;2&quot; maximum=&quot;100&quot;/&gt;
                        &lt;f:validator validatorId=&quot;alphaValidator&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:outputText value=&quot;Last name&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;Last name&quot;
                        value=&quot;#{user.user.lastName}&quot;&gt;
                        &lt;f:validateLength minimum=&quot;2&quot; maximum=&quot;100&quot;/&gt;
                        &lt;f:validator validatorId=&quot;alphaValidator&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:outputText value=&quot;E-mail&quot; /&gt;
                    &lt;h:inputText
                        label=&quot;E-mail&quot;
                        value=&quot;#{user.user.email}&quot;&gt;
                        &lt;f:validator validatorId=&quot;emailValidator&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:outputText value=&quot;Active&quot; /&gt;
                    &lt;h:selectBooleanCheckbox
                        label=&quot;active&quot;
                        value=&quot;#{user.user.active}&quot; /&gt;
                    &lt;h:outputText value=&quot;Date created&quot; /&gt;
                    &lt;h:inputText
                        label=&quot;Date created&quot;
                        value=&quot;#{user.user.dateCreated}&quot;
                        required=&quot;true&quot;&gt;
                        &lt;f:convertDateTime pattern=&quot;MM/dd/yyyy&quot;/&gt;
                    &lt;/h:inputText&gt;
                    &lt;h:panelGroup/&gt;
                    &lt;h:commandButton
                        action=&quot;#{user.saveUser}&quot;
                        value=&quot;Save&quot;/&gt;
                &lt;/h:panelGrid&gt;
            &lt;/h:form&gt;
        &lt;/f:view&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<h3>The failed view</h3>
<p>JumpStartEjbJsf-war &gt; Web Pages &gt; failed.jsp</p>
<pre class="brush: xml;">
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
   &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;Save failed&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h2&gt;Save failed&lt;/h2&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p align="center"><img src="http://faq.files.wordpress.com/2006/12/somerights20.png" alt="cc -Some rights" /></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/luxlog.wordpress.com/33/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/luxlog.wordpress.com/33/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luxlog.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luxlog.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luxlog.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luxlog.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luxlog.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luxlog.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luxlog.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luxlog.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luxlog.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luxlog.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luxlog.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luxlog.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luxlog.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luxlog.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luxlog.wordpress.com&amp;blog=1809170&amp;post=33&amp;subd=luxlog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luxlog.wordpress.com/2007/12/22/basic-web-app-completed-with-validation-elements/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1d34babec4b54d767d4afee86976384e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Luk</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/3100.thumbnail.png" medium="image">
			<media:title type="html">3100.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/3200.thumbnail.png" medium="image">
			<media:title type="html">3200.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/3300.thumbnail.png" medium="image">
			<media:title type="html">3300.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/3400.thumbnail.png" medium="image">
			<media:title type="html">3400.png</media:title>
		</media:content>

		<media:content url="http://faq.files.wordpress.com/2006/12/somerights20.png" medium="image">
			<media:title type="html">cc -Some rights</media:title>
		</media:content>
	</item>
		<item>
		<title>Part 2 of 3 Basic web app extended to full CRUD</title>
		<link>http://luxlog.wordpress.com/2007/12/17/basic-web-app-extended-to-full-crud/</link>
		<comments>http://luxlog.wordpress.com/2007/12/17/basic-web-app-extended-to-full-crud/#comments</comments>
		<pubDate>Mon, 17 Dec 2007 20:49:32 +0000</pubDate>
		<dc:creator>Luk</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>

		<guid isPermaLink="false">http://luxlog.wordpress.com/2007/12/17/2-basic-web-app-extended-to-full-crud/</guid>
		<description><![CDATA[In this second part we&#8217;re extending the application we&#8217;ve set up in part one to support updates and delete actions for existing records. Still, the result will be far from production ready but the page views I got since yesterday encouraged me to continue writing.Before embarking upon this part it is imperative that you finished [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luxlog.wordpress.com&amp;blog=1809170&amp;post=32&amp;subd=luxlog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In this second part we&#8217;re extending the application we&#8217;ve set up in <a href="http://luxlog.wordpress.com/2007/12/15/basic-example-web-app-with-glassfish-netbeans-60-jsf-and-ejb3/">part one</a> to support updates and delete actions for existing records. Still, the result will be far from production ready but the page views I got since yesterday encouraged me to continue writing.<span id="more-32"></span>Before embarking upon this part it is imperative that you finished part one. We will be extending existing code with new parts and modifying other parts. Next to that, in order to keep my rants as compact as possible, I won&#8217;t be repeating how to open a project, how to build the application etc. If however some thing are not clear, please post me a comment. Having said that we are still using PostgreSQL, GlassfishV2 and NetBeans.</p>
<p>Upfront requirements:</p>
<ul>
<li>Have the project from <a href="http://luxlog.wordpress.com/2007/12/15/basic-example-web-app-with-glassfish-netbeans-60-jsf-and-ejb3/">part one</a> up and running</li>
</ul>
<h2>Table of contents</h2>
<ul>
<li><a href="#step1">Step 1: Extend the controller class</a></li>
<li><a href="#step2">Step 2: Extend the JSF dataTable with commandLinks for delete and edit</a></li>
<li><a href="#step3">Step 3: Add edit JSF page</a></li>
<li><a href="#step4">Step 4: Modify the page flow in faces-config.xml</a></li>
<li><a href="#step5">Step 5: Modify existing methods in UserDAOBean</a></li>
<li><a href="#step6">Step 6: Deploy and run your full CRUD web application</a></li>
</ul>
<p>The example will be extended with functionalities to update and delete existing records.</p>
<p><img src="http://luxlog.files.wordpress.com/2007/12/2010.png" alt="2010.png" /></p>
<p><img src="http://luxlog.files.wordpress.com/2007/12/2020.png" alt="2020.png" /><br />
<a title="step1" name="step1"></a></p>
<h2>Step 1: Extend the controller class</h2>
<p>Let&#8217;s start by adding two action methods to the controller class, one for updating a User object, and one for deleting a User object:</p>
<ul>
<li>deleteUser()</li>
<li>editUser()</li>
</ul>
<p>We also add two helper methods. These are called from <code>deleteUser()</code> to give feedback on whether delete succeeded or failed.</p>
<ul>
<li>addErrorMessage(String msg)</li>
<li>addSuccessMessage(String msg)</li>
</ul>
<p>Finally we have extended the saveUser() method with a condition that checks whether a userId is passed in. If so, we update an existing user. If not, a new user is added.</p>
<p>Our controller class (<strong>JumpStartEjbJsf-war</strong> &gt; <strong>Source Packages</strong> &gt; <strong>lux.controllers</strong> &gt; <strong>UserController.java</strong>) now looks like this (<strong>the added methods are indicated with a comment starting with // #2</strong>):</p>
<pre class="brush: java;">
package lux.controllers;

import java.util.Map;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import lux.domain.User;
import lux.facade.UserDAO;

public class UserController {

    @EJB
    UserDAO userDao;
    private User user;
    private DataModel model;

    public String createUser() {
        this.user = new User();
        return &quot;create_new_user&quot;;
    }

    // #2: extended with condition to check whether it is create or update
    public String saveUser() {
        String r = &quot;success&quot;;

        try {
            if (user.getUserId() == null) {
                userDao.createUser(user);
            } else {
                userDao.updateUser(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
            r = &quot;failed&quot;;
        }

        return r;
    }

    public DataModel getUsers() {
        model = new ListDataModel(userDao.getAllUsers());
        return model;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    // #2: delete action backing bean method
    public String deleteUser() {
        String r = &quot;user_deleted&quot;;
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestParameterMap = (Map) context.getExternalContext().getRequestParameterMap();

        try {
            Integer userId = Integer.parseInt(requestParameterMap.get(&quot;userId&quot;).toString());
            User u = userDao.getUser(userId);
            String userName = u.getUsername();
            userDao.deleteUser(u);
            addSuccessMessage(&quot;User &quot; + userName + &quot; successfully deleted.&quot;);
        } catch (NumberFormatException ne) {
            addErrorMessage(ne.getLocalizedMessage());
            r = &quot;failed&quot;;
        } catch (Exception e) {
            addErrorMessage(e.getLocalizedMessage());
            r = &quot;failed&quot;;
        }

        return r;
    }

    // #2: edit action backing bean method
    public String editUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestParameterMap = (Map) context.getExternalContext().getRequestParameterMap();
        Integer userId = Integer.parseInt(requestParameterMap.get(&quot;userId&quot;).toString());
        this.user = userDao.getUser(userId);
        return &quot;edit_user&quot;;
    }

    // #2: helper method for printing error message in jsf page
    private void addErrorMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage(null, facesMsg);
    }

    // #2: helper method for printing feedback in jsf page
    private void addSuccessMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage(&quot;successInfo&quot;, facesMsg);
    }
}</pre>
<p>Either replace the contents of your <strong>UserController.java</strong> file with the contents above, or modify and add the fragments prefaced with &#8220;// #2&#8243;.<a title="step2" name="step2"></a></p>
<h2>Step 2: Extend the JSF dataTable with commandLinks for delete and edit</h2>
<p>The above methods <code>editUser()</code> and <code>deleteUser()</code> are action methods. These methods can be called from a JSF form to perform an action. Let&#8217;s extend our dataTable with commandLinks to both methods. Also we are adding a <code>&lt;h:messages/&gt;</code> element to give feedback after the user requested a delete action.</p>
<pre class="brush: xml;">
&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;

&lt;%@taglib prefix=&quot;f&quot; uri=&quot;http://java.sun.com/jsf/core&quot;%&gt;
&lt;%@taglib prefix=&quot;h&quot; uri=&quot;http://java.sun.com/jsf/html&quot;%&gt;

&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;User Listing&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;f:view&gt;
            &lt;h:form&gt;
                &lt;!-- #2 added messages element for delete feedback --&gt;
                &lt;h:messages&gt;&lt;/h:messages&gt;
&lt;h1&gt;&lt;h:outputText value=&quot;User Listing&quot;/&gt;&lt;/h1&gt;

&lt;h:commandLink action=&quot;#{user.createUser}&quot; value=&quot;Create a user&quot;/&gt;
                &lt;h:dataTable value=&quot;#{user.users}&quot;
                             var=&quot;dataTableItem&quot; border=&quot;1&quot; cellpadding=&quot;2&quot; cellspacing=&quot;2&quot;&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;Username&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:outputText value=&quot;#{dataTableItem.username}&quot; /&gt;
                    &lt;/h:column&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;First name&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:outputText value=&quot;#{dataTableItem.firstName}&quot; /&gt;
                    &lt;/h:column&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;Last name&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:outputText value=&quot;#{dataTableItem.lastName}&quot; /&gt;
                    &lt;/h:column&gt;
                    &lt;!-- #2 added column with edit and delete commandLink --&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;Edit&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:commandLink action=&quot;#{user.editUser}&quot;&gt;
                            &lt;h:outputText value=&quot;Edit&quot;/&gt;
                            &lt;f:param name=&quot;userId&quot; value=&quot;#{dataTableItem.userId}&quot; /&gt;
                        &lt;/h:commandLink&gt;
                    &lt;/h:column&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;Delete&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:commandLink action=&quot;#{user.deleteUser}&quot;&gt;
                            &lt;h:outputText value=&quot;Delete&quot;/&gt;
                            &lt;f:param name=&quot;userId&quot; value=&quot;#{dataTableItem.userId}&quot; /&gt;
                        &lt;/h:commandLink&gt;
                    &lt;/h:column&gt;
                &lt;/h:dataTable&gt;
            &lt;/h:form&gt;
        &lt;/f:view&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Either replace the contents of your <strong>index.jsp</strong> file with the contents above, or modify file your file by adding/modifying the fragments above prefaced with &lt;!&#8211; #2.<a title="step3" name="step3"></a></p>
<h2>Step 3: Add edit JSF page</h2>
<p>There are better, more manageable ways to do this but for now to add an &#8220;edit&#8221; page just:</p>
<ul>
<li>make a copy of adduser.jsp,</li>
<li>remove the password fieldset from your copy,</li>
<li>add a title <code>&lt;h1&gt;Edit user &lt;h:outputText value="#{user.user.username}" /&gt;&lt;/h1&gt;</code></li>
</ul>
<p>1. Create a new JSP file <strong>edituser.jsp</strong><br />
2. Overwrite the contents of your newly created file with the code below<br />
3. Save your file</p>
<pre class="brush: xml;">
&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;

&lt;%@taglib prefix=&quot;f&quot; uri=&quot;http://java.sun.com/jsf/core&quot;%&gt;
&lt;%@taglib prefix=&quot;h&quot; uri=&quot;http://java.sun.com/jsf/html&quot;%&gt;

&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;Edit user&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;f:view&gt;
&lt;h1&gt;Edit user &lt;h:outputText value=&quot;#{user.user.username}&quot; /&gt;&lt;/h1&gt;
&lt;h:form&gt;
                &lt;h:messages/&gt;
                &lt;h:panelGrid columns=&quot;2&quot;&gt;
                    &lt;h:outputText value=&quot;Username&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;Username&quot;
                        value=&quot;#{user.user.username}&quot;
                        required=&quot;true&quot;/&gt;
                    &lt;h:outputText value=&quot;First name&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;First name&quot;
                        value=&quot;#{user.user.firstName}&quot; /&gt;
                    &lt;h:outputText value=&quot;Last name&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;Last name&quot;
                        value=&quot;#{user.user.lastName}&quot; /&gt;
                    &lt;h:panelGroup/&gt;
                    &lt;h:commandButton
                        action=&quot;#{user.saveUser}&quot;
                        value=&quot;Save&quot;/&gt;
                &lt;/h:panelGrid&gt;
            &lt;/h:form&gt;
        &lt;/f:view&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p><a title="step4" name="step4"></a></p>
<h2>Step 4: Modify the page flow in faces-config.xml</h2>
<p>For the delete action the user stays on index.jsp. For the edit action however the same page flow as for <strong>adduser.jsp</strong> is implemented. This makes a slightly more complex-looking diagram for faces-config than we had before in step 1. You can imagine that with a lot of pages and lots of navigation this can get messy.</p>
<p><a title="2100.png" href="http://luxlog.files.wordpress.com/2007/12/2100.png"><img src="http://luxlog.files.wordpress.com/2007/12/2100.thumbnail.png" alt="2100.png" /></a></p>
<p>After modifying the diagram the xml (<strong>JumpStartEjbJsf-war</strong> &gt; Configuration Files &gt; faces-config.xml) should look like this:</p>
<pre class="brush: xml;">
&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;managed-bean&gt;
        &lt;managed-bean-name&gt;user&lt;/managed-bean-name&gt;
        &lt;managed-bean-class&gt;lux.controllers.UserController&lt;/managed-bean-class&gt;
        &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;
    &lt;/managed-bean&gt;
    &lt;navigation-rule&gt;
        &lt;from-view-id&gt;/index.jsp&lt;/from-view-id&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;create_new_user&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/adduser.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
        &lt;!-- #2 added for edit action --&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;edit_user&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/edituser.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;failed&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/failed.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
    &lt;/navigation-rule&gt;
    &lt;navigation-rule&gt;
        &lt;from-view-id&gt;/adduser.jsp&lt;/from-view-id&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;failed&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/failed.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;success&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/index.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
    &lt;/navigation-rule&gt;
    &lt;!-- #2 added for edit action --&gt;
    &lt;navigation-rule&gt;
        &lt;from-view-id&gt;/edituser.jsp&lt;/from-view-id&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;failed&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/failed.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;success&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/index.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
    &lt;/navigation-rule&gt;
&lt;/faces-config&gt;</pre>
<p><a title="step5" name="step5"></a></p>
<h2>Step 5: Modify existing methods in UserDAOBean</h2>
<p>Final steps are to modify the methods <code>updateUser(User u)</code> and <code>deleteUser(User u)</code>. For updateUser we added:<br />
<code><br />
String pw = u.getPassword();<br />
u.setPassword(pw);<br />
</code><br />
We did this so that the password could be left out of the edit page. For deleteUser we had to add a line because, well er&#8230; it didn&#8217;t work.<br />
<code><br />
User mgdUser = em.merge(u);<br />
</code></p>
<p>It actually turned out to be spec defined behavior that before an entity bean can be removed it must be read in the current persistence context. More info at https://glassfish.dev.java.net/javaee5/persistence/persistence-example.html#merge_and_remove.<br />
The above changes result in following code (overwrite the existing UserDAOBean.java with it, or modify the parts prefaced with // #2)</p>
<pre class="brush: java;">
package lux.facade;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import lux.domain.User;

@Stateless
public class UserDAOBean implements UserDAO {

    @PersistenceContext
    private EntityManager em;

    public User getUser(int UserId) {
        User u = new User();
        u = em.find(User.class, UserId);
        return u;
    }

    public List getAllUsers() {
        Query q = em.createQuery(&quot;SELECT u FROM User u&quot;);
        List users = q.getResultList();
        return users;
    }

    public void createUser(User u) {
        String hashedPw = hashPassword(u.getPassword());
        u.setPassword(hashedPw);
        em.persist(u);
    }

    // #2 changed: created hash on hash
    public void updateUser(User u) {
        String pw = u.getPassword();
        u.setPassword(pw);
        em.merge(u);
    }

    // #2: modified, didn't work
    public void deleteUser(User u) {
        User mgdUser = em.merge(u);
        em.remove(mgdUser);
    }

    private String hashPassword(String password) {
        StringBuilder sb = new StringBuilder();

        try {
            MessageDigest messageDigest = MessageDigest.getInstance(&quot;SHA&quot;);
            byte[] bs;
            bs = messageDigest.digest(password.getBytes());

            for (int i = 0; i &lt; bs.length; i++) {
                String hexVal = Integer.toHexString(0xFF &amp; bs[i]);
                if (hexVal.length() == 1) {
                    sb.append(&quot;0&quot;);
                }
                sb.append(hexVal);
            }

        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(UserDAOBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return sb.toString();
    }
}</pre>
<p><a title="step6" name="step6"></a></p>
<h2>Step 6: Deploy and run your full CRUD web application</h2>
<p>Ok, that&#8217;s it. Press F6 to run your application. If you run into any problems, you can debug by pressing Shift-F5.<br />
Good luck!</p>
<p><b>Everything running fine? Then move on to the final and last <a href="http://luxlog.wordpress.com/2007/12/22/basic-web-app-completed-with-validation-elements/">part 3</a>.</b></p>
<p align="center"><img src="http://faq.files.wordpress.com/2006/12/somerights20.png" alt="cc -Some rights" /></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/luxlog.wordpress.com/32/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/luxlog.wordpress.com/32/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luxlog.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luxlog.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luxlog.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luxlog.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luxlog.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luxlog.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luxlog.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luxlog.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luxlog.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luxlog.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luxlog.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luxlog.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luxlog.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luxlog.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luxlog.wordpress.com&amp;blog=1809170&amp;post=32&amp;subd=luxlog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luxlog.wordpress.com/2007/12/17/basic-web-app-extended-to-full-crud/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1d34babec4b54d767d4afee86976384e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Luk</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/2010.png" medium="image">
			<media:title type="html">2010.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/2020.png" medium="image">
			<media:title type="html">2020.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/2100.thumbnail.png" medium="image">
			<media:title type="html">2100.png</media:title>
		</media:content>

		<media:content url="http://faq.files.wordpress.com/2006/12/somerights20.png" medium="image">
			<media:title type="html">cc -Some rights</media:title>
		</media:content>
	</item>
		<item>
		<title>Part 1 of 3 Basic example web app with Glassfish, NetBeans 6.0, JSF and EJB3</title>
		<link>http://luxlog.wordpress.com/2007/12/15/basic-example-web-app-with-glassfish-netbeans-60-jsf-and-ejb3/</link>
		<comments>http://luxlog.wordpress.com/2007/12/15/basic-example-web-app-with-glassfish-netbeans-60-jsf-and-ejb3/#comments</comments>
		<pubDate>Sat, 15 Dec 2007 15:14:58 +0000</pubDate>
		<dc:creator>Luk</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>

		<guid isPermaLink="false">http://luxlog.wordpress.com/2007/12/15/basic-example-web-app-with-glassfish-netbeans-60-jsf-and-ejb3/</guid>
		<description><![CDATA[Keywords: DAO, MVC, JSF, Session Facade, NetBeans 6.0, EJB3, GlassfishV2 If you&#8217;re looking for a walkthrough on setting up a basic web application using the DAO / MVC patterns with Glassfish V2 you’ve come to the right place. Here’s a HOWTO. Upfront software requirements: Sun’s JDK 1.6 (download: http://java.sun.com/javase/downloads/index.jsp) PostgreSQL 8.2 (download: http://postgresql.org/download/) NetBeans IDE [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luxlog.wordpress.com&amp;blog=1809170&amp;post=25&amp;subd=luxlog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Keywords: DAO, MVC, JSF, Session Facade, NetBeans 6.0, EJB3, GlassfishV2</p>
<p>If you&#8217;re looking for a walkthrough on setting up a basic web application using the DAO / MVC patterns with Glassfish V2 you’ve come to the right place. Here’s a HOWTO.<br />
<span id="more-25"></span><br />
Upfront software requirements:</p>
<ul>
<li>Sun’s JDK 1.6 (download: <a href="http://java.sun.com/javase/downloads/index.jsp">http://java.sun.com/javase/downloads/index.jsp</a>)</li>
<li>PostgreSQL 8.2 (download: <a href="http://postgresql.org/download/">http://postgresql.org/download/</a>)</li>
<li>NetBeans IDE 6.0 Web &amp; Java EE bundle (download: <a href="http://download.netbeans.org/netbeans/6.0/final/"> http://download.netbeans.org/netbeans/6.0/final/</a>)</li>
</ul>
<p>(If anyone wants this HOWTO/tutorial to be extended with instructions for MySQL post me a comment, if anyone wants a HOWTO on how to set up your environment post me a comment, if you get stuck at any certain point post me a comment…)</p>
<blockquote><p>Please note that what you’ll find here are the bare essentials to give you a jumpstart. In the real world (and in later examples if this works out fine) Java goodies such as Seam and Facelets are added to make the steps below much simpler and much more attractive. Next to that, this example uses a simple datamodel with one domain element only (User). In a next step we might add more elements.</p></blockquote>
<h2>Table of contents</h2>
<ul>
<li><a href="#step1">Step 1: Create an Enterprise Application Project with NetBeans 6<br />
</a></li>
<li><a href="#step2">Step 2: Add JSF capabilities to your project</a></li>
<li><a href="#step3">Step 3: Running the newly created project</a></li>
<li><a href="#step4">Step 4: Create the test_db database (on PostgreSQL)</a></li>
<li><a href="#step5">Step 5: Add the JDBC driver file to Glassfish</a></li>
<li><a href="#step6">Step 6: Set up a JDBC connection pool in Glassfish</a></li>
<li><a href="#step7">Step 7: Create the User Entity Bean</a></li>
<li><a href="#step8">Step 8: Add business logic</a></li>
<li><a href="#step9">Step 9: Create the web application’s Controller</a></li>
<li><a href="#step10">Step 10: Configure page flow in faces-config.xml</a></li>
<li><a href="#step11">Step 11: Create views</a></li>
</ul>
<p>The example consists of two web pages, one enlisting all the users stored in the database, the other contains a form for adding a user.</p>
<p><img src="http://luxlog.files.wordpress.com/2008/01/10.png" alt="10.png" /></p>
<p><img src="http://luxlog.files.wordpress.com/2007/12/20.png" alt="20.png" /></p>
<p><a title="step1" name="step1"></a></p>
<h2>Step 1: Create an Enterprise Application Project with NetBeans 6</h2>
<ol>
<li>Start NetBeans</li>
<li>Select Menubar item <b>File</b> &gt; <b>New Project …</b></li>
<li>Select category <b>Enterprise</b> &gt; project <b>Enterprise Application</b></li>
<li>Click <b>Next</b></li>
<li>Fill in Project Name <b>JumpStartEjbJsf</b></li>
<li>Click <b>Finish</b></li>
</ol>
<p><a href="http://luxlog.files.wordpress.com/2007/12/100.png" title="100.png"><img src="http://luxlog.files.wordpress.com/2007/12/100.thumbnail.png" alt="100.png" /></a><br />
<a title="step2" name="step2"></a></p>
<h2>Step 2: Add JSF capabilities to your project</h2>
<p>For now our web application only supports JSP. Let’s add JSF to the mix.</p>
<ol>
<li>Right-click the <b>JumpStartEjbJsf-war</b> created in Step 1 and select <b>Properties</b>.</li>
<li>In the <b>Project Properties</b> dialog box select <b>Frameworks</b>.</li>
<li>Click on <b>Add</b>.</li>
<li>Select <b>JavaServer Faces</b>.</li>
<li>Click <b>OK</b>.</li>
</ol>
<p><a href="http://luxlog.files.wordpress.com/2007/12/200.png" title="200.png"><img src="http://luxlog.files.wordpress.com/2007/12/200.thumbnail.png" alt="200.png" /></a></p>
<p>If you’ve kept the default settings, when deployed JSF pages will be preceeded by <b>/faces/</b><code></code>. Therefore we have tell the container that <b>index.jsp</b> no longer is the start page but that <b>faces/index.jsp </b><code></code>is.</p>
<ol>
<li>In the <b>Projects</b> pane traverse through the tree to <b>JumpStartEjbJsf-war</b> &gt; <b>Configuration Files</b> and open <b>web.xml</b>.</li>
<li>Click on the <b>Pages</b> button in <b>web.xml</b> and change the value for <b>Welcome Files</b> to<b> faces/index.jsp</b>.</li>
<li>Back in the Projects pane delete the file <b>index.jsp</b></li>
<li>Rename the file <b>welcomeJSF.jsp</b> to <b>index.jsp</b></li>
</ol>
<p><a href="http://luxlog.files.wordpress.com/2007/12/300.png" title="300.png"><img src="http://luxlog.files.wordpress.com/2007/12/300.thumbnail.png" alt="300.png" /></a><br />
<a title="step3" name="step3"></a></p>
<h2>Step 3: Running the newly created project</h2>
<p>Let’s check whether it is working. Press F6 to boot Glassfish, deploy the application and start up a browser window.</p>
<p><a href="http://luxlog.files.wordpress.com/2007/12/400.png" title="400.png"><img src="http://luxlog.files.wordpress.com/2007/12/400.thumbnail.png" alt="400.png" /></a></p>
<p><a href="http://luxlog.files.wordpress.com/2007/12/500.png" title="500.png"><img src="http://luxlog.files.wordpress.com/2007/12/500.thumbnail.png" alt="500.png" /></a><br />
<a title="step4" name="step4"></a></p>
<h2>Step 4: Create the test_db database (on PostgreSQL)</h2>
<p>Create a database using PgAdmin3 or by issuing the commands:<br />
<code><br />
&gt; sudo psql -U postgres<br />
# CREATE DATABASE test_db WITH ENCODING=’UTF8′;<br />
# \q<br />
</code></p>
<p>Create a table in our newly created <b>test_db</b> database by issuing the commands:<br />
<code><br />
&gt; sudo psql -U postgres -d test_db<br />
# CREATE TABLE users<br />
# (<br />
#   user_id serial,<br />
#   username varchar(255) NOT NULL,<br />
#   first_name varchar(255),<br />
#   last_name varchar(255),<br />
#   password char(64) NOT NULL,<br />
#   CONSTRAINT pk_users PRIMARY KEY (user_id)<br />
#) WITHOUT OIDS;<br />
# \q<br />
</code><br />
<a title="step5" name="step5"></a></p>
<h2>Step 5: Add the JDBC driver file to Glassfish</h2>
<p>If you have not yet setup a JDBC connection to PostgreSQL you have to add the right driver to Glassfish. Here are the steps:</p>
<ol>
<li>Download the JDBC driver for your version of PostgreSQL at <a href="http://jdbc.postgresql.org/download.html">http://jdbc.postgresql.org/download.html</a>. I am using &#8220;JDBC3 Postgresql Driver, Version 8.2-507&#8243; with filename &#8220;postgresql-8.2-507.jdbc3.jar&#8221; since JDBC4 does not support all methods yet (like giving relevant feedback to the client when throwing an exception).</li>
<li>Copy &#8220;postgresql-8.2-507.jdbc3.jar&#8221; to [GLASSFISH_DIR]/domains/domain1/lib.</li>
<li>Restart the domain using the program asadmin available in [GLASSFISH_DIR]/bin.</li>
</ol>
<p><code> &gt;./asadmin stop-domain<br />
&gt;./asadmin start-domain<br />
</code></p>
<p><a href="http://luxlog.files.wordpress.com/2007/12/600.png" title="600.png"><img src="http://luxlog.files.wordpress.com/2007/12/600.thumbnail.png" alt="600.png" /></a><br />
<a title="step6" name="step6"></a></p>
<h2>Step 6: Set up a JDBC connection pool in Glassfish</h2>
<p>The next step is to set up a connection pool which we’ll use from our application to connect to the database.</p>
<p>1.<br />
Go to the Admin Console at <a href="http://localhost:4848/" target="_blank">http://localhost:4848/</a>. If you’ve kept the default username/password combination log in as “admin” with password “adminadmin” (and change it).</p>
<p>2.<br />
Click <b>Create New JDBC Connection Pool</b> on the homepage.</p>
<p>3.<br />
Fill in for <b>New JDBC Connection Pool (Step 1 of 2)</b>:</p>
<table>
<tr>
<td>Name:</td>
<td>__PgTestDBPool</td>
</tr>
<tr>
<td>Resource type</td>
<td>javax.sql.DataSource</td>
</tr>
<tr>
<td>Database Vendor</td>
<td>PostgreSQL</td>
</tr>
</table>
<p>4.<br />
Fill in for <b>New JDBC Connection Pool (Step 2 of 2)</b> at the bottom of the page, assuming you kept default settings when installing PostgreSQL:</p>
<table>
<tr>
<td>User</td>
<td>postgres</td>
</tr>
<tr>
<td>DatabaseName</td>
<td>test_db</td>
</tr>
<tr>
<td>Password</td>
<td>xxxxxxxx</td>
</tr>
<tr>
<td>ServerName</td>
<td>localhost</td>
</tr>
<tr>
<td>PortNumber</td>
<td>5432</td>
</tr>
</table>
<p>5.<br />
You are now directed to (look at the crumbpath) <b>Resources &gt; JDBC &gt; Connection Pools</b>. Click on your newly created JDNI entry <b>__PgTestDBPool</b> and click the <b>Ping</b> button at the top. A message should pop up saying <b>Ping Succeeded</b>.</p>
<p><a href="http://luxlog.files.wordpress.com/2007/12/700.png" title="700.png"><img src="http://luxlog.files.wordpress.com/2007/12/700.thumbnail.png" alt="700.png" /></a></p>
<p>6.<br />
Now create a <b>JDCB Resource</b> by clicking <b>Resources &gt; JDBC &gt; JDBC Resources</b> in the left treeview pane</p>
<p>7.<br />
On the JDBC Resources page click the <b>New</b> button  and fill in:</p>
<table>
<tr>
<td>JDNI Name</td>
<td>jdbc/test_db</td>
</tr>
<tr>
<td>Pool Name</td>
<td>__PgTestDBPool</td>
</tr>
</table>
<p><a title="step7" name="step7"></a></p>
<h2>Step 7: Create the User Entity Bean</h2>
<p>In the next two steps we develop the backend application to define and expose our domain model. In this step we create the entity bean User which maps to the table we created in <a href="http://luxlog.wordpress.com/wp-admin/post.php#step4">Step 4</a>.</p>
<ol>
<li>In NetBeanse IDE right-click on <b>JumpStartEjbJsf-ejb</b> and select <b>New</b> &gt; <b>Entity Classes from Database&#8230;</b></li>
<li>In the <b>New Entity Classes From Database</b> dialog box select <b>Data Source</b>: <b>jdbc/test_db</b>.</li>
<li>In the <b>New Database Connection</b> that then pops up it should read the values from the table below&#8230;</li>
<li>Click <b>OK</b>.</li>
<li>On the next tab <b>Advanced</b> select Schema <b>public.</b></li>
<li>Back in the <b>New Entity Classes From Database</b> dialog box add the <b>Users</b> table to <b>Selected Tables</b>.</li>
<li>Click <b>Next</b>.</li>
<li>Change the class name from <b>Users</b> to <b>User</b></li>
<li>Fill in the package name <b>lux.domain</b>.</li>
<li>Click the <b>Create Persistence Unit</b> button and accept the defaults.</li>
<li>Uncheck <b>Generate Named Query Annotations for Persistent Fields</b>.</li>
<li>Click <b>Finish</b>.</li>
</ol>
<table>
<tr>
<td>Name</td>
<td>“PostgreSQL”</td>
</tr>
<tr>
<td>Driver</td>
<td>“org.postgresql.Driver”</td>
</tr>
<tr>
<td>Database URL</td>
<td>jdbc:postgresql://localhost:5432/test_db</td>
</tr>
<tr>
<td>Username</td>
<td>postgres</td>
</tr>
<tr>
<td>Password</td>
<td>xxxxxxxx</td>
</tr>
</table>
<p><a href="http://luxlog.files.wordpress.com/2007/12/800.png" title="800.png"><img src="http://luxlog.files.wordpress.com/2007/12/800.thumbnail.png" alt="800.png" /></a></p>
<p>The only thing we change to the Entity Bean is to tell JPA to have the DB automatically generate ID’s for us. There might be better ways (if you’re interested post me a comment…) for doing this because now we are tied to PostgreSQL but hey. Another note up ahead is that the sequenceName is <b>users_user_id_seq</b> because this is the name of the sequence PostgreSQL generated for the serial datatype we used in our table definition in Step 4.</p>
<ol>
<li>Open the Entity Bean by clicking in the <b>Projects</b> pane: <b>JumpStartEjbJsf-ejb</b> &gt; <b>Source Packages </b>&gt; <b>lux.domain</b> &gt; <b>User.java.</b></li>
<li>Change the code by adding the two lines below the comment:</li>
</ol>
<pre class="brush: java;">
@Entity
@Table(name = &quot;Users&quot;)
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
// Add these two lines to the generated code
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator=&quot;users_seq_generator&quot;)
@SequenceGenerator(name=&quot;users_seq_generator&quot;, sequenceName=&quot;users_user_id_seq&quot;)&lt;/strong&gt;
@Column(name = &quot;user_id&quot;, nullable = false)
private Integer id;</pre>
<p>Import the necessary classes (one way is too put your cursor on the erronous reference, clicking Alt-Shift-I and select the class to import). These classes are referenced:</p>
<pre class="brush: java;">
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;</pre>
<p><a title="step8" name="step8"></a></p>
<h2>Step 8: Add business logic</h2>
<p>In this step we create a Stateless Session Bean as a facade to our domain object allowing us to get, create, delete or update objects. Just as the standard DAO pattern states.</p>
<ol>
<li>Create a Session Bean by right-clicking on “JumpStartEjbJsf-ejb” and selecting “New” &gt; “Session Bean”</li>
<li>In the “New Session Bean” dialog box: fill in</li>
</ol>
<table>
<tr>
<td>EJB Name</td>
<td>UserDAO</td>
</tr>
<tr>
<td>Package</td>
<td>lux.facade</td>
</tr>
<tr>
<td>Session type</td>
<td>Stateless (default)</td>
</tr>
<tr>
<td>Create Interface</td>
<td>Local (default)</td>
</tr>
</table>
<p><a href="http://luxlog.files.wordpress.com/2007/12/900.png" title="900.png"><img src="http://luxlog.files.wordpress.com/2007/12/900.thumbnail.png" alt="900.png" /></a></p>
<p>You might consider providing both a Local (inside the JVM) and a Remote (for connecting to the component over the network) interface. But for this project it’s just Local. Therefore let’s change the name of the class from <b>UserDAOLocal.java</b> to <b>UserDAO.java</b>.</p>
<ol>
<li>In the <b>Projects</b> pane select <b>JumpStartEjbJsf-ejb</b> &gt; <b>Source Packages</b> &gt; <b>lux.facade</b> &gt; <b>UserDAOLocal.java</b> and change the file name to <b>UserDAO.java</b>.</li>
<li>Click <b>Refactor</b>.</li>
</ol>
<p>Finally. Some code. If UserDAOBean.java is not open yet then open it. Change the existing code by the code below. If you want to know more about what is going on try the very good introductory book try <a href="http://www.amazon.com/gp/product/1847192602?ie=UTF8&amp;tag=lulo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1847192602">Java EE 5 Development using GlassFish Application Server</a><img src="http://www.assoc-amazon.com/e/ir?t=lulo-20&amp;l=as2&amp;o=1&amp;a=1847192602" style="border:medium none !important;margin:0 !important;" border="0" height="1" width="1" />.</p>
<p><a href="http://www.amazon.com/gp/product/1847192602?ie=UTF8&amp;tag=lulo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1847192602"><img src="http://ecx.images-amazon.com/images/I/21RIDIFqznL._AA_SL160_.jpg" height="160" width="126" /></a></p>
<pre class="brush: java;">
package lux.facade;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import lux.domain.User;

@Stateless
public class UserDAOBean implements UserDAO {

  @PersistenceContext
  private EntityManager em;

  public User getUser(int UserId) {
    User u = new User();
    u = em.find(User.class, UserId);
    return u;
  }

  public List&lt;User&gt; getAllUsers() {
    Query q = em.createQuery(&quot;SELECT u FROM User u&quot;);
    List&lt;User&gt; users = q.getResultList();
    return users;
  }

  public void createUser(User u) {
    String hashedPw = hashPassword(u.getPassword());
    u.setPassword(hashedPw);
    em.persist(u);
  }

  public void updateUser(User u) {
    String hashedPw = hashPassword(u.getPassword());
    u.setPassword(hashedPw);
    em.merge(u);
  }

  public void deleteUser(User u) {
    em.remove(u);
  }

  private String hashPassword(String password) {
    StringBuilder sb = new StringBuilder();
    try {
      MessageDigest messageDigest = MessageDigest.getInstance(&quot;SHA&quot;);
      byte[] bs;
      bs = messageDigest.digest(password.getBytes());
      for (int i = 0; i &lt; bs.length; i++) {
        String hexVal = Integer.toHexString(0xFF &amp; bs[i]);
        if (hexVal.length() == 1) {
          sb.append(&quot;0&quot;);
        }
        sb.append(hexVal);
      }
    } catch (NoSuchAlgorithmException ex) {
      Logger.getLogger(UserDAOBean.class.getName()).log(Level.SEVERE, null, ex);
    }
    return sb.toString();
  }
}</pre>
<p>NetBeans will suggest you to update the Interface for each public method you add:<a href="http://luxlog.files.wordpress.com/2007/12/950.png" title="950.png"><img src="http://luxlog.files.wordpress.com/2007/12/950.thumbnail.png" alt="950.png" /></a>Here is the final source for the Interface.</p>
<pre class="brush: java;">
package lux.facade;
import javax.ejb.Local;
@Local
public interface UserDAO {
  public lux.domain.User getUser(int UserId);
  public void createUser(lux.domain.User u);
  public void updateUser(lux.domain.User u);
  public void deleteUser(lux.domain.User u);
  public java.util.List&lt;lux.domain.User&gt; getAllUsers();
}</pre>
<blockquote><p> Feel like you’ve been doing lots of stuff? Well you have. You’ve got your application server running, made it possible to connect through it to a database, deployed an enterprise app running JSF, created a domain object and provided an interface to get and modify it.To the frontend now where we’ll create a list of users and a form to add a user to the database.</p></blockquote>
<p><a title="step9" name="step9"></a></p>
<h2>Step 9: Create the web application&#8217;s Controller</h2>
<p>Next we create the controller class that will reference session facade created in Step 7.</p>
<p>1.<br />
Create a controller (or “backing bean”) by right clicking JumpStartEjbJsf-war, selecting <b>New</b> &gt; <b>JSF Managed Bean</b>. In the <b>New JSF Managed Bean</b> dialog box, fill in:</p>
<table>
<tr>
<td>Class name</td>
<td>UserController</td>
</tr>
<tr>
<td>Package</td>
<td>lux.controllers</td>
</tr>
<tr>
<td>Scope</td>
<td>session</td>
</tr>
<tr></tr>
</table>
<p><a href="http://luxlog.files.wordpress.com/2007/12/1000.png" title="1000.png"><img src="http://luxlog.files.wordpress.com/2007/12/1000.thumbnail.png" alt="1000.png" /></a></p>
<p>2.<br />
Change the code for <b>UserController.java</b> to:</p>
<pre class="brush: java;">
package lux.controllers;

import javax.ejb.EJB;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import lux.domain.User;
import lux.facade.UserDAO;

public class UserController {
  @EJB UserDAO userDao;
  private User user;
  private DataModel model;

  public String createUser() {
    this.user = new User();
    return “create_new_user”;
  }

  public String saveUser() {
    String r = “success”;
    try {
      userDao.createUser(user);
    } catch (Exception e) {
      e.printStackTrace();
      r = “failed”;
    }
    return r;
  }

  public DataModel getUsers() {
    model = new ListDataModel(userDao.getAllUsers());
    return model;
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }
}</pre>
<p>The <b>saveUser</b> method above is the action that will be invoked when the user submits the form. Notice that <b>saveUser</b> returns either <b>success </b>or <b>failed</b>. This will decide to what page the user is forwarded as will be defined in faces-config.xml next.<br />
<a title="step10" name="step10"></a></p>
<h2>Step 10: Configure page flow in faces-config.xml</h2>
<ol>
<li>Create the JSP file <b>adduser.jsp</b> by right-clicking <b>JumpStartEjbJsf-war</b> and selecting <b>New</b> &gt; <b>JSP</b>. Fill in <b>File Name</b> adduser.jsp, click <b>Finish</b>.</li>
<li>Repeat the previous step for another JSP file <b>failed.jsp</b>.</li>
<li>On failed.jsp change the string <b><code>&lt;h2&gt;Hello world&lt;/h2&gt;</code></b> to <b><code>&lt;h2&gt;Save failed&lt;/h2&gt;</code></b>.</li>
</ol>
<p><a href="http://luxlog.files.wordpress.com/2007/12/1100.png" title="1100.png"><img src="http://luxlog.files.wordpress.com/2007/12/1100.thumbnail.png" alt="1100.png" /></a></p>
<p>Next we configure the page flow.</p>
<ol>
<li>Open <b>JumpStartEjbJsf-war</b> &gt; <b>Configuration Files</b> &gt; <b>faces-config.xml.</b></li>
<li>Drag an arrow from <b>index.jsf</b> to <b>adduser.jsp</b> and replace the arrow’s label to <b>create_new_user</b>.</li>
<li>Repeat the previous step for <b>failed</b>, by dragging and arrow from <b>adduser.jsp</b> to <b>failed.jsp</b> renaming the label to <b>failed</b></li>
<li>Finally repeat the step for adduser.jsp, by dragging from <b>adduser.jsp</b> to <b>index.jsp</b> renaming the label to <b>success</b>. This results in the image depicted below:</li>
</ol>
<p><a href="http://luxlog.files.wordpress.com/2007/12/1200.png" title="1200.png"><img src="http://luxlog.files.wordpress.com/2007/12/1200.thumbnail.png" alt="1200.png" /></a></p>
<p><a href="http://luxlog.files.wordpress.com/2007/12/1300.png" title="1300.png"><img src="http://luxlog.files.wordpress.com/2007/12/1300.thumbnail.png" alt="1300.png" /></a></p>
<p>You’re faces-config should now look like this (click the “XML” button)</p>
<pre class="brush: xml;">
&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;managed-bean&gt;
        &lt;managed-bean-name&gt;user&lt;/managed-bean-name&gt;
        &lt;managed-bean-class&gt;lux.controllers.UserController&lt;/managed-bean-class&gt;
        &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;
    &lt;/managed-bean&gt;
    &lt;navigation-rule&gt;
        &lt;from-view-id&gt;/index.jsp&lt;/from-view-id&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;create_new_user&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/adduser.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
    &lt;/navigation-rule&gt;
    &lt;navigation-rule&gt;
        &lt;from-view-id&gt;/adduser.jsp&lt;/from-view-id&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;failed&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/failed.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
        &lt;navigation-case&gt;
            &lt;from-outcome&gt;success&lt;/from-outcome&gt;
            &lt;to-view-id&gt;/index.jsp&lt;/to-view-id&gt;
        &lt;/navigation-case&gt;
    &lt;/navigation-rule&gt;
&lt;/faces-config&gt;</pre>
<p>Last steps are writing the view elements (or “forms”).<br />
<a title="step11" name="step11"></a></p>
<h2>Step 11: Create views</h2>
<p>Open <b>JumpStartEjbJsf-war</b> &gt; <b>Web Pages</b> &gt; <b>index.jsp</b> and change the existing code by</p>
<pre class="brush: xml;">
&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;

&lt;%@taglib prefix=&quot;f&quot; uri=&quot;http://java.sun.com/jsf/core&quot;%&gt;
&lt;%@taglib prefix=&quot;h&quot; uri=&quot;http://java.sun.com/jsf/html&quot;%&gt;

&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;User Listing&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;f:view&gt;
            &lt;h:form&gt;
                &lt;h1&gt;&lt;h:outputText value=&quot;User Listing&quot;/&gt;&lt;/h1&gt;
                &lt;p&gt;&lt;h:commandLink action=&quot;#{user.createUser}&quot; value=&quot;Create a user&quot;/&gt;&lt;/p&gt;
                &lt;h:dataTable value=&quot;#{user.users}&quot;
                             var=&quot;dataTableItem&quot; border=&quot;1&quot; cellpadding=&quot;2&quot; cellspacing=&quot;2&quot;&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;Username&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:outputText value=&quot;#{dataTableItem.username}&quot; /&gt;
                    &lt;/h:column&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;First name&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:outputText value=&quot;#{dataTableItem.firstName}&quot; /&gt;
                    &lt;/h:column&gt;
                    &lt;h:column&gt;
                        &lt;f:facet name=&quot;header&quot;&gt;
                            &lt;h:outputText  value=&quot;Last name&quot;/&gt;
                        &lt;/f:facet&gt;
                        &lt;h:outputText value=&quot;#{dataTableItem.lastName}&quot; /&gt;
                    &lt;/h:column&gt;
                &lt;/h:dataTable&gt;
            &lt;/h:form&gt;
        &lt;/f:view&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Open <b>JumpStartEjbJsf-war</b> &gt; <b>Web Pages</b> &gt; <b>adduser.jsp</b> and change the existing code by</p>
<pre class="brush: xml;">
&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;

&lt;%@taglib prefix=&quot;f&quot; uri=&quot;http://java.sun.com/jsf/core&quot;%&gt;
&lt;%@taglib prefix=&quot;h&quot; uri=&quot;http://java.sun.com/jsf/html&quot;%&gt;

&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;New user&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;f:view&gt;
            &lt;h:form&gt;
                &lt;h:messages/&gt;
                &lt;h:panelGrid columns=&quot;2&quot;&gt;
                    &lt;h:outputText value=&quot;Username&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;Username&quot;
                        value=&quot;#{user.user.username}&quot;
                        required=&quot;true&quot;/&gt;
                    &lt;h:outputText value=&quot;First name&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;First name&quot;
                        value=&quot;#{user.user.firstName}&quot; /&gt;
                    &lt;h:outputText value=&quot;Last name&quot;/&gt;
                    &lt;h:inputText
                        label=&quot;Last name&quot;
                        value=&quot;#{user.user.lastName}&quot; /&gt;
                    &lt;h:outputText value=&quot;Password&quot; /&gt;
                    &lt;h:inputSecret
                        label=&quot;Password&quot;
                        value=&quot;#{user.user.password}&quot;
                        required=&quot;true&quot; /&gt;
                    &lt;h:panelGroup/&gt;
                    &lt;h:commandButton
                        action=&quot;#{user.saveUser}&quot;
                        value=&quot;Save&quot;/&gt;
                &lt;/h:panelGrid&gt;
            &lt;/h:form&gt;
        &lt;/f:view&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Let’s run our application by clicking F6 (Run). If it doesn’t work right away because try “Undeploy and deploy”-ing it. I had to each time I walked through the above code for testing. You can do this by right-clicking the project name <b>JumpStartEjbJsf</b> and selecting <b>Undeploy Deploy</b>.Haven&#8217;t had enough already? Then there is <a href="http://luxlog.wordpress.com/2007/12/17/basic-web-app-extended-to-full-crud/">Part 2: Basic web app extended to full CRUD</a></p>
<blockquote><p>Hope it worked for you. Feel free to comment. What improvements can you think of? What should be the next move (integrate with SAAJ and make a login page)? Did you run into trouble? Let us know.</p></blockquote>
<p align="center"><img src="http://faq.files.wordpress.com/2006/12/somerights20.png" alt="cc -Some rights" /></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/luxlog.wordpress.com/25/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/luxlog.wordpress.com/25/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luxlog.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luxlog.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luxlog.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luxlog.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luxlog.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luxlog.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luxlog.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luxlog.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luxlog.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luxlog.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luxlog.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luxlog.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luxlog.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luxlog.wordpress.com/25/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luxlog.wordpress.com&amp;blog=1809170&amp;post=25&amp;subd=luxlog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luxlog.wordpress.com/2007/12/15/basic-example-web-app-with-glassfish-netbeans-60-jsf-and-ejb3/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1d34babec4b54d767d4afee86976384e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Luk</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2008/01/10.png" medium="image">
			<media:title type="html">10.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/20.png" medium="image">
			<media:title type="html">20.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/100.thumbnail.png" medium="image">
			<media:title type="html">100.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/200.thumbnail.png" medium="image">
			<media:title type="html">200.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/300.thumbnail.png" medium="image">
			<media:title type="html">300.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/400.thumbnail.png" medium="image">
			<media:title type="html">400.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/500.thumbnail.png" medium="image">
			<media:title type="html">500.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/600.thumbnail.png" medium="image">
			<media:title type="html">600.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/700.thumbnail.png" medium="image">
			<media:title type="html">700.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/800.thumbnail.png" medium="image">
			<media:title type="html">800.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/900.thumbnail.png" medium="image">
			<media:title type="html">900.png</media:title>
		</media:content>

		<media:content url="http://www.assoc-amazon.com/e/ir?t=lulo-20&#038;l=as2&#038;o=1&#038;a=1847192602" medium="image" />

		<media:content url="http://ecx.images-amazon.com/images/I/21RIDIFqznL._AA_SL160_.jpg" medium="image" />

		<media:content url="http://luxlog.files.wordpress.com/2007/12/950.thumbnail.png" medium="image">
			<media:title type="html">950.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/1000.thumbnail.png" medium="image">
			<media:title type="html">1000.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/1100.thumbnail.png" medium="image">
			<media:title type="html">1100.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/1200.thumbnail.png" medium="image">
			<media:title type="html">1200.png</media:title>
		</media:content>

		<media:content url="http://luxlog.files.wordpress.com/2007/12/1300.thumbnail.png" medium="image">
			<media:title type="html">1300.png</media:title>
		</media:content>

		<media:content url="http://faq.files.wordpress.com/2006/12/somerights20.png" medium="image">
			<media:title type="html">cc -Some rights</media:title>
		</media:content>
	</item>
	</channel>
</rss>
