HibernateContinentDao.java
package com.lishman.world.data.hibernate;
import java.util.Collection;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.lishman.world.data.ContinentDao;
import com.lishman.world.domain.Continent;
@Repository
@Transactional
public class HibernateContinentDao implements ContinentDao {
@Autowired
SessionFactory sessionFactory;
@Transactional(readOnly = true)
public Continent getById(int continentId) {
return (Continent) sessionFactory
.getCurrentSession()
.get(Continent.class, continentId);
}
@Transactional(readOnly = true)
public Continent getByName(String continentName) {
return (Continent) sessionFactory
.getCurrentSession()
.getNamedQuery("Continent.findByName")
.setParameter("name", continentName)
.uniqueResult();
}
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public Collection<Continent> getAll() {
return sessionFactory
.getCurrentSession()
.getNamedQuery("Continent.findAll")
.list();
}
public void save(Continent continent) {
sessionFactory.getCurrentSession().merge(continent);
}
public void delete(Continent continent) {
sessionFactory.getCurrentSession().delete(continent);
}
}
HibernateCountryDao.java
package com.lishman.world.data.hibernate;
import java.util.Collection;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.lishman.world.data.CountryDao;
import com.lishman.world.domain.Country;
@Repository
@Transactional
public class HibernateCountryDao implements CountryDao {
@Autowired
SessionFactory sessionFactory;
@Transactional(readOnly = true)
public Country getById(int countryId) {
return (Country) sessionFactory
.getCurrentSession()
.get(Country.class, countryId);
}
@Transactional(readOnly = true)
public Country getByName(String countryName) {
return (Country) sessionFactory
.getCurrentSession()
.getNamedQuery("Country.findByName")
.setParameter("name", countryName)
.uniqueResult();
}
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public Collection<Country> getAll() {
return sessionFactory
.getCurrentSession()
.getNamedQuery("Country.findAll")
.list();
}
public void save(Country country) {
sessionFactory.getCurrentSession().merge(country);
}
public void delete(Country country) {
sessionFactory.getCurrentSession().delete(country);
}
}
ContinentDao.java
package com.lishman.world.data;
import java.util.Collection;
import com.lishman.world.domain.Continent;
public interface ContinentDao {
public Collection<Continent> getAll();
public Continent getById(int continentId);
public Continent getByName(String continentName);
public void save(Continent continent);
public void delete(Continent continent);
}
CountryDao.java
package com.lishman.world.data;
import java.util.Collection;
import com.lishman.world.domain.Country;
public interface CountryDao {
public Collection<Country> getAll();
public Country getById(int countryId);
public Country getByName(String countryName);
public void save(Country country);
public void delete(Country country);
}
Continent.java
package com.lishman.world.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import org.hibernate.validator.constraints.NotEmpty;
@Entity
@Table(name = "CONT")
@NamedQueries ({
@NamedQuery(name="Continent.findAll", query="from Continent"),
@NamedQuery(name="Continent.findByName", query="from Continent cont where cont.name = :name")
})
public class Continent {
@Id
@GeneratedValue
@Column(name = "CONT_ID")
private Integer id;
@Column(name = "CONT_NAME")
@NotEmpty
private String name;
public Continent() {
}
public Continent(Integer id, String name) {
setId(id);
setName(name);
}
public boolean isNew() {
return id == null;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return getName();
}
}
Country.java
package com.lishman.world.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
@Entity
@Table(name = "CTRY")
@NamedQueries ({
@NamedQuery(name="Country.findAll", query="from Country"),
@NamedQuery(name="Country.findByName", query="from Country ctry where ctry.name = :name")
})
public class Country {
@Id
@GeneratedValue
@Column(name = "CTRY_ID")
private Integer id;
@Column(name = "CTRY_NAME")
@NotEmpty
private String name;
@Column(name = "CTRY_AREA")
@NotNull
@Min(1)
private Integer area;
@Column(name = "CTRY_POP")
@NotNull
@Min(1)
private Long population;
@ManyToOne
@JoinColumn(name = "CONT_ID")
private Continent continent;
public Country() {
}
public Country(Integer id, String name, Integer area, Long population) {
setId(id);
setName(name);
setArea(area);
setPopulation(population);
}
public boolean isNew() {
return id == null;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setArea(Integer area) {
this.area = area;
}
public Integer getArea() {
return area;
}
public void setPopulation(Long population) {
this.population = population;
}
public Long getPopulation() {
return population;
}
public void setContinent(Continent continent) {
this.continent = continent;
}
public Continent getContinent() {
return continent;
}
}
CountryValidator.java
package com.lishman.world.domain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import com.lishman.world.service.WorldService;
@Component
public class CountryValidator {
@Autowired
private WorldService worldService;
public void validate(Country country, Errors errors) {
if (!errors.hasFieldErrors("name")) {
Country existingCountry = worldService.getCountryByName(country.getName());
if (existingCountry != null
&& (country.isNew() ||
!country.getId().equals(existingCountry.getId()))) {
errors.rejectValue("name", "validation.exists", "exists");
}
}
}
}
WorldService.java
package com.lishman.world.service;
import java.util.Collection;
import com.lishman.world.domain.Continent;
import com.lishman.world.domain.Country;
public interface WorldService {
public Collection<Country> getAllCountries();
public Country getCountryById(int countryId);
public Country getCountryByName(String countryName);
public void saveCountry(Country country);
public void deleteCountry(Country country);
public Collection<Continent> getAllContinents();
public Continent getContinentById(int continentId);
public Continent getContinentByName(String continentName);
public void saveContinent(Continent continent);
public void deleteContinent(Continent continent);
}
WorldServiceImpl.java
package com.lishman.world.service;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lishman.world.data.ContinentDao;
import com.lishman.world.data.CountryDao;
import com.lishman.world.domain.Continent;
import com.lishman.world.domain.Country;
@Service
public class WorldServiceImpl implements WorldService {
@Autowired
private CountryDao countryDao;
@Autowired
private ContinentDao continentDao;
public Collection<Country> getAllCountries() {
return countryDao.getAll();
}
public Country getCountryById(int countryId) {
return countryDao.getById(countryId);
}
public Country getCountryByName(String countryName) {
return countryDao.getByName(countryName);
}
public void saveCountry(Country country) {
countryDao.save(country);
}
public void deleteCountry(Country country) {
countryDao.delete(country);
}
public Collection<Continent> getAllContinents() {
return continentDao.getAll();
}
public Continent getContinentById(int continentId) {
return continentDao.getById(continentId);
}
public Continent getContinentByName(String continentName) {
return continentDao.getByName(continentName);
}
public void saveContinent(Continent continent) {
continentDao.save(continent);
}
public void deleteContinent(Continent continent) {
continentDao.delete(continent);
}
}
ContinentPropertyEditor.java
package com.lishman.world.web;
import java.beans.PropertyEditorSupport;
import com.lishman.world.service.WorldService;
public class ContinentPropertyEditor extends PropertyEditorSupport{
private WorldService worldService;
public ContinentPropertyEditor(WorldService worldService) {
this.worldService = worldService;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(worldService.getContinentByName(text));
}
}
CountryController.java
package com.lishman.world.web;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lishman.world.domain.Country;
import com.lishman.world.service.WorldService;
@Controller
public class CountryController {
@Autowired
private WorldService worldService;
@RequestMapping("/country-list")
@ModelAttribute("countries")
public Collection<Country> getCountries() {
return worldService.getAllCountries();
}
@RequestMapping("/country-details/{id}")
public String getCountry(@PathVariable("id") int countryId,
Model model) {
Country country = worldService.getCountryById(countryId);
model.addAttribute(country);
return "country-details";
}
}
CountryForm.java
package com.lishman.world.web;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import com.lishman.world.domain.Continent;
import com.lishman.world.domain.Country;
import com.lishman.world.domain.CountryValidator;
import com.lishman.world.service.WorldService;
@Controller
@RequestMapping("/country-form")
@SessionAttributes("country")
public class CountryForm {
@Autowired
private CountryValidator countryValidator;
@Autowired
private WorldService worldService;
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
dataBinder.registerCustomEditor(Continent.class, new ContinentPropertyEditor(worldService));
}
@ModelAttribute("continentList")
public List<Continent> getTagList () {
return (List<Continent>) worldService.getAllContinents();
}
@RequestMapping(method = RequestMethod.GET)
public Country setUpForm(
@RequestParam(value = "id", required = false) Integer countryId) {
if (countryId == null) {
return new Country();
} else {
return worldService.getCountryById(countryId);
}
}
@RequestMapping(params = "save", method = RequestMethod.POST)
public String save(@Valid Country country,
BindingResult result,
SessionStatus status) {
countryValidator.validate(country, result);
if (result.hasErrors()) {
return "country-form";
} else {
worldService.saveCountry(country);
status.setComplete();
return "redirect:country-list";
}
}
@RequestMapping(params = "delete", method = RequestMethod.POST)
public String delete(Model model, SessionStatus status) {
Country country = (Country) model.asMap().get("country");
worldService.deleteCountry(country);
status.setComplete();
return "redirect:country-list";
}
}
world.css
body {
font-family: Geneva, Arial, Helvetica, sans-serif;
color: DimGray;
}
h1 {
font-size: 20px;
font-weight: normal;
color: SeaGreen;
padding: 10px;
}
img {
border-style: none;
}
a {
color: DarkSlateGray;
font-weight: normal;
text-decoration: none;
}
a:hover {
color: OrangeRed;
font-weight: normal;
text-decoration: none;
}
table {
width: 100%;
}
table.silver {
background: WhiteSmoke;
border-collapse: collapse;
margin: 2px 0 10px 0;
}
table.silver th {
color: DarkSlateGray;
background: Gainsboro;
text-align: left;
}
table.silver th, table.silver td {
border: 1px Silver solid;
padding: 5px;
}
#contents {
margin-left: 50;
text-align: right;
}
#contents.list {
width: 200px;
}
#contents.detail {
width: 300px;
}
.error {
color: Red;
}
edit.gif
country-details.jsp
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<link rel="stylesheet" href="<c:url value="/css/world.css" />" type="text/css">
<title><spring:message code="country.details"></title>
</head>
<body>
<h1><spring:message code="application.name"></h1>
<div id="contents" class="detail">
<table class="silver" width="260">
<tr>
<th colspan="2"><spring:message code="country.details"></th>
</tr>
<tr>
<td><spring:message code="country.name"></td>
<td>${country.name}</td>
</tr>
<tr>
<td><spring:message code="country.area"></td>
<td><fmt:formatNumber type="number" value="${country.area}"></td>
</tr>
<tr>
<td><spring:message code="country.population"></td>
<td><fmt:formatNumber type="number" value="${country.population}"></td>
</tr>
<tr>
<td><spring:message code="continent.label"></td>
<td>${country.continent.name}</td>
</tr>
</table>
</div>
<a href="<c:url value=" /app/country-list.html">" >
«<spring:message code="navigation.back">
</a>
</body>
</html>
country-form.jsp
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<link rel="stylesheet" href="<c:url value=" /css/world.css">" type="text/css"/>
<title><spring:message code="country.details"></title>
</head>
<body>
<h1><spring:message code="application.name"></h1>
<div id="contents" class="detail">
<form:form modelAttribute="country" action="country-form" method="post">
<c:choose>
<c:when test="${country.new}">
<button type="submit" name="save">
<spring:message code="button.create">
</button>
</c:when>
<c:otherwise>
<button type="submit" name="save">
<spring:message code="button.update">
</button>
<button type="submit" name="delete" onclick="return confirm('<spring:message code='button.delete'/> ${country.name}?')">
<spring:message code="button.delete">
</button>
</c:otherwise>
</c:choose>
<table class="silver">
<tr>
<th colspan="2"><spring:message code="country.details"></th>
</tr>
<tr>
<td><spring:message code="country.name"></td>
<td>
<form:input path="name" size="20" maxlength="50"><br/>
<form:errors path="name" cssClass="error">
</td>
</tr>
<tr>
<td><spring:message code="country.area"></td>
<td>
<form:input path="area" size="8" maxlength="8"><br/>
<form:errors path="area" cssClass="error">
</td>
</tr>
<tr>
<td><spring:message code="country.population"></td>
<td>
<form:input path="population" size="10" maxlength="10"><br/>
<form:errors path="population" cssClass="error">
</td>
</tr>
<tr>
<td><spring:message code="continent.label"></td>
<td>
<form:select path="continent" items="${continentList}">
</td>
</tr>
</table>
</form:form>
</div>
<a href="<c:url value=" /app/country-list.html">" >
«<spring:message code="navigation.back">
</a>
</body>
</html>
country-list.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<link rel="stylesheet" href="<c:url value=" /css/world.css">" type="text/css"/>
<title><spring:message code="country.plural"></title>
</head>
<body>
<h1><spring:message code="application.name"></h1>
<div id="contents" class="list">
<a href="<c:url value=" /app/country-form.html">">
<button><spring:message code="button.create"></button>
</a>
<table class="silver">
<tr>
<th style="width: 20px"> </th>
<th><spring:message code="country.plural"></th>
</tr>
<c:forEach items="${countries}" var="country">
<tr>
<td>
<a href="<c:url value='/app/country-form.html?id=${country.id}'/>">
<img src="<c:url value=" /images/edit.gif">"/>
</a>
</td>
<td>
<a href="<c:url value='/app/country-details/${country.id}'/>">
${country.name}
</a>
</td>
</tr>
</c:forEach>
</table>
<a href="?locale=en_us">us</a> |
<a href="?locale=en_gb">gb</a> |
<a href="?locale=fr_fr">fr</a> |
<a href="?locale=de_de">de</a>
</div>
</body>
</html>
messages.properties
application.name=Countries of the World
navigation.back=back
button.create=Create
button.update=Update
button.delete=Delete
country.label=Country
country.plural=Countries
country.details=Country Details
country.name=Name
country.area=Area
country.population=Population
country.estimatedOn=Date of Estimate
country.currency=Currency
continent.label=Continent
validation.exists=already exists
typeMismatch.java.util.Date=must be a valid date
typeMismatch.java.lang.Integer=must be a valid number
typeMismatch.java.lang.Long=must be a valid number
messages_de.properties
application.name=Länder der Welt
navigation.back=Startseite
button.create=Schaffen
button.update=Unter
button.delete=Löschen
country.label=Land
country.plural=Länder
country.details=Land Details
country.name=Name
country.area=Die Umgebung
country.population=Bevölkerung
country.estimatedOn=Datum der Schätzung
country.currency=Währung
continent.label=Kontinent
validation.exists=vorhanden
typeMismatch.java.util.Date=ungültiges Datum
typeMismatch.java.lang.Integer=ungültige Zahl
typeMismatch.java.lang.Long=ungültige Zahl
messages_fr.properties
application.name=Pays du Monde
navigation.back=retournez
button.create=Créer
button.update=Changement
button.delete=Supprimer
country.label=pays
country.plural=Pays
country.details=Détails de pays
country.name=Nom
country.area=Taille
country.population=La Population
country.estimatedOn=Date de l'estimation
country.currency=Devise
continent.label=Continent
validation.exists=existe
typeMismatch.java.util.Date=date non valide
typeMismatch.java.lang.Integer=nombre invalide
typeMismatch.java.lang.Long=nombre invalide
hibernate-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="com.lishman.world.data.hibernate"/>
<context:property-placeholder location="WEB-INF/spring/data/jdbc.properties"/>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:maxActive="${dbcp.maxActive}"
p:maxIdle="${dbcp.maxIdle}"
p:maxWait="${dbcp.maxWait}"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configurationClass="org.hibernate.cfg.AnnotationConfiguration"
p:packagesToScan="com.lishman.world.domain">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>
<tx:annotation-driven transaction-manager="txnManager"/>
<bean id="txnManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>
jdbc.properties
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@my-server:1521:my-db
jdbc.username=my-user
jdbc.password=my-password
dbcp.maxActive=100
dbcp.maxIdle=30
dbcp.maxWait=20000
hibernate.generate_statistics=false
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
app-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.lishman.world.service"/>
<context:component-scan base-package="com.lishman.world.domain"/>
<import resource="mvc-config.xml"/>
<import resource="data/hibernate-config.xml"/>
</beans>
mvc-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.lishman.world.web"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"/>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="/WEB-INF/messages/messages"/>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
</mvc:interceptors>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
p:cookieName="WorldLocale"/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>world</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/app-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>world</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>app/country-list.html</welcome-file>
</welcome-file-list>
</web-app>
world.sql
DROP TABLE ctry;
DROP TABLE cont;
CREATE TABLE cont (
cont_id NUMBER,
cont_name VARCHAR2(50) NOT NULL,
CONSTRAINT cont_pk PRIMARY KEY (cont_id),
CONSTRAINT cont_uk1 UNIQUE (cont_name)
);
DROP SEQUENCE cont_seq1;
CREATE SEQUENCE cont_seq1;
CREATE OR REPLACE TRIGGER cont_trg1
BEFORE INSERT ON cont FOR EACH ROW
BEGIN
IF :NEW.cont_id IS NULL THEN
SELECT cont_seq1.NEXTVAL INTO :NEW.cont_id FROM DUAL;
END IF;
END;
/
INSERT INTO cont (cont_name) VALUES('Africa');
INSERT INTO cont (cont_name) VALUES('Asia');
INSERT INTO cont (cont_name) VALUES('Europe');
INSERT INTO cont (cont_name) VALUES('North America');
INSERT INTO cont (cont_name) VALUES('South America');
INSERT INTO cont (cont_name) VALUES('Oceania');
INSERT INTO cont (cont_name) VALUES('Antarctica');
COMMIT;
CREATE TABLE ctry (
ctry_id NUMBER,
ctry_name VARCHAR2(50) NOT NULL,
ctry_area NUMBER NOT NULL,
ctry_pop NUMBER NOT NULL,
cont_id NUMBER NOT NULL,
CONSTRAINT ctry_pk PRIMARY KEY (ctry_id),
CONSTRAINT ctry_uk1 UNIQUE (ctry_name),
CONSTRAINT ctry_fk1 FOREIGN KEY (cont_id) REFERENCES cont
);
DROP SEQUENCE ctry_seq1;
CREATE SEQUENCE ctry_seq1;
CREATE OR REPLACE TRIGGER ctry_trg1
BEFORE INSERT ON ctry FOR EACH ROW
BEGIN
IF :NEW.ctry_id IS NULL THEN
SELECT ctry_seq1.NEXTVAL INTO :NEW.ctry_id FROM DUAL;
END IF;
END;
/
INSERT INTO ctry (ctry_name, ctry_area, ctry_pop, cont_id)
VALUES('Germany', 137847, 82046000, 3);
INSERT INTO ctry (ctry_name, ctry_area, ctry_pop, cont_id)
VALUES('Ghana', 92098, 23837000, 1);
INSERT INTO ctry (ctry_name, ctry_area, ctry_pop, cont_id)
VALUES('Australia', 2966200, 21884000, 6);
INSERT INTO ctry (ctry_name, ctry_area, ctry_pop, cont_id)
VALUES('Greece', 50949, 11257285, 3);
INSERT INTO ctry (ctry_name, ctry_area, ctry_pop, cont_id)
VALUES('Georgia', 26900, 4382100, 3);
INSERT INTO ctry (ctry_name, ctry_area, ctry_pop, cont_id)
VALUES('New Zealand', 104454, 4320300, 6);
INSERT INTO ctry (ctry_name, ctry_area, ctry_pop, cont_id)
VALUES('Gambia', 4361, 1705000, 1);
INSERT INTO ctry (ctry_name, ctry_area, ctry_pop, cont_id)
VALUES('Gabon', 103347, 1475000, 1);
COMMIT;
%%_UserGuide_%%