The application is really simple - it allows you to create a database of cities/country that you like. You enter the city & country name on a page and click on Submit. This stores the data entered in the backend database and displays all the stored values in a new page. This application demonstrates simple JSF concepts:
- How to create a JSF application using NetBeans IDE ?
- How to populate a JSF widget with a Managed Bean ?
- How to use a Persistence Unit with JSF widgets ?
- How to setup navigation rules between multiple pages ?
- How to print simple error validation messages ?
- How to inject a bean into another class ?

and click on "Next".

take defaults and click on "Finish".
create table cities(id integer AUTO_INCREMENT,
city_name varchar(20),
country_name varchar(20),
PRIMARY KEY(id));
@NamedQuery(name = "Cities.findAll", query = "SELECT c FROM Cities c"),
right after the highlighted parentheses shown below:


and click on "Finish".
private Cities cities;
public void setCities(Cities cities) {
this.cities = cities;
}
and then injecting in "faces-config.xml" as shown by the fragment below:
<managed-bean>
<managed-bean-name>cities</managed-bean-name>
<managed-bean-class>server.Cities</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>dbUtil</managed-bean-name>
<managed-bean-class>server.DatabaseUtil</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>cities</property-name>
<value>#{cities}</value>
</managed-property>
</managed-bean>
@PersistenceContext(unitName="CitiesPU")
private EntityManager entityManager;
@Resource
UserTransaction utx;
public Collection<Cities> getAllCities() {
Collection<Cities> allCities = new ArrayList<Cities>();
List list = entityManager.createNamedQuery("Cities.findAll").getResultList();
for (int i = 0; i < list.size(); i++) {
allCities.add((Cities)list.get(i));
}
return allCities;
}
public String saveCity() throws NotSupportedException, SystemException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
utx.begin();
entityManager.persist(cities);
utx.commit();
return "submit";
}

and click on "OK". Make sure to pick the right package name for "NotSupportedException" and "RollbackException".


<h2>Detail</h2>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Id:"/>
<h:outputText value="#{anInstanceOfserver.Cities.id}" title="Id" />
<h:outputText value="CityName:"/>
<h:outputText value="#{anInstanceOfserver.Cities.cityName}" title="CityName" />
<h:outputText value="CountryName:"/>
<h:outputText value="#{anInstanceOfserver.Cities.countryName}" title="CountryName" />
</h:panelGrid>
</h:form>
It generates a 2-column table based upon fields from the entity class. We will use this form for accepting inputs by making the following changes:
The updated code fragment (with changes highlighted in bold) looks like:
<h2>Detail</h2>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="CityName:"/>
<h:inputText value="#{cities.cityName}" title="CityName" id="cityName" required="true"/>
<h:outputText value="CountryName:"/>
<h:inputText value="#{cities.countryName}" title="CountryName" id="countryName" required="true"/>
</h:panelGrid>
</h:form>
Issue# 144217 will ensure to pick a pre-declared managed-bean or declare a new one if it does not exist already. After issue# 144499 is fixed then "id" attributes will be generated by default.
<h:commandButton action="#{dbUtil.saveCity}" value="submit"/>
This must be added between </h:panelGrid> and </h:form> tags.
<br><br>
<h:message for="cityName" showSummary="true" showDetail="false" style="color: red"/><br>
<h:message for="countryName" showSummary="true" showDetail="false" style="color: red"/>
right after <h:commandButton> tag. The official docs specify the default value of "false" for both "showSummary" and "showDetail" attribute. But TLD says "false" for "showSummary" and "true" for "showDetail". Issue# 773 will fix that.
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
Issue #144218 will ensure these namespaces are declared by the IDE.

The generated code fragment looks like:
<f:view>
<h:form>
<h1><h:outputText value="List"/></h1>
<h:dataTable value="#{arrayOrCollectionOfserver.Cities}" var="item">
<h:column>
<f:facet name="header">
<h:outputText value="Id"/>
</f:facet>
<h:outputText value=" #{item.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="CityName"/>
</f:facet>
<h:outputText value=" #{item.cityName}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="CountryName"/>
</f:facet>
<h:outputText value=" #{item.countryName}"/>
</h:column>
</h:dataTable>
</h:form>
</f:view>
Change the <h:dataTable> tag as shown below (changes highlighted in bold):
<h:dataTable value="#{dbUtil.allCities}" var="item">
<h:form>
<h:commandButton action="back" value="back"/>
</h:form>
between </h:form> and </f:view> tags.

The corresponding XML fragment is:
<navigation-rule>
<from-view-id>/welcomeJSF.jsp</from-view-id>
<navigation-case>
<from-outcome>submit</from-outcome>
<to-view-id>/result.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/result.jsp</from-view-id>
<navigation-case>
<from-outcome>back</from-outcome>
<to-view-id>/welcomeJSF.jsp</to-view-id>
</navigation-case>
</navigation-rule>
Let's run the application by right-clicking on the project and selecting "Deploy and Undeploy". The welcome page shows up and looks like as shown below:

Clicking on "Submit" without entering any values shows the default error messages as shown below:

Enter your favorite city/country and click on "Submit" to see the result page as:

Click on "Back" and enter few more cities. The updated result page looks like:

Here are some useful pointers for you:
- JSF Tag Library & API docs
- javaserverfaces.dev.java.net - the community website
- Java EE 5 JSF Tutorial and many more on the community website right navbar.
- Java Server Faces on SDN
- GlassFish Webtier Aggregated Feed
- Feedback
Please leave suggestions on other TOTD (Tip Of The Day) that you'd like to see. A complete archive of all tips is available here.
Technorati: totd mysql javaserverfaces netbeans glassfish