Spring Example


pom.xml

—————————————————-

<?xml version=”1.0″ encoding=”UTF-8″?><project>
<modelVersion>4.0.0</modelVersion>
<groupId>Spring3HibernateMaven</groupId>
<artifactId>Spring3HibernateMaven</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<description></description>
<build>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.9</version>
<configuration>
<contextPath>/Employee</contextPath>
<scanIntervalSeconds>3</scanIntervalSeconds>
<scanTargetPatterns>
<scanTargetPattern>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.jsp</exclude>
</excludes>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</scanTargetPattern>
</scanTargetPatterns>
<!– webDefaultXml>src/main/resources/webdefault.xml
</webDefaultXml–>

</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!– dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.3.2.ga</version>
</dependency–>
<!– dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.4.2</version>
</dependency –>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!– dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>20030825.184428</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>20030825.183949</version>
</dependency–>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1</artifactId>
<version>6.1.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<org.springframework.version>3.0.2.RELEASE</org.springframework.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

————————————————————–

/src/main/webapp/WEB-INF/web.xml

————————————-

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xmlns=”http://java.sun.com/xml/ns/javaee&#8221;
xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221;
id=”WebApp_ID” version=”2.5″>
<display-name>Spring3-Hibernate</display-name>
<welcome-file-list>
<welcome-file>list.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

———————————–

src/main/webapp/WEB-INF/spring-servlet.xml

—————————————————

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xmlns:aop=”http://www.springframework.org/schema/aop&#8221;
xmlns:context=”http://www.springframework.org/schema/context&#8221;
xmlns:jee=”http://www.springframework.org/schema/jee&#8221;
xmlns:lang=”http://www.springframework.org/schema/lang&#8221;
xmlns:p=”http://www.springframework.org/schema/p&#8221;
xmlns:tx=”http://www.springframework.org/schema/tx&#8221;
xmlns:util=”http://www.springframework.org/schema/util&#8221;
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd”&gt;

<context:annotation-config />
<context:component-scan base-package=”…” />

<bean id=”jspViewResolver”
class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
<property name=”viewClass”
value=”org.springframework.web.servlet.view.JstlView” />
<property name=”prefix” value=”/WEB-INF/jsp/” />
<property name=”suffix” value=”.jsp” />
</bean>

<bean id=”messageSource”
class=”org.springframework.context.support.ReloadableResourceBundleMessageSource”>
<property name=”basename” value=”classpath:messages” />
<property name=”defaultEncoding” value=”UTF-8″ />
</bean>
<bean id=”propertyConfigurer”
class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”
p:location=”/WEB-INF/jdbc.properties” />

<bean id=”contactService”>
<property name=”contactDAO” ref=”contactDAO” />

</bean>
<bean id=”contactDAO”>

</bean>
<!–bean id=”dataSource”
class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”
p:driverClassName=”${jdbc.driverClassName}”
p:url=”${jdbc.databaseurl}” p:username=”${jdbc.username}”
p:password=”${jdbc.password}” />

<bean id=”sessionFactory”
class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
<property name=”dataSource” ref=”dataSource” />
<property name=”configLocation”>
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name=”configurationClass”>
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name=”hibernateProperties”>
<props>
<prop key=”hibernate.dialect”>${jdbc.dialect}</prop>
<prop key=”hibernate.show_sql”>true</prop>
</props>
</property>
</bean>

<tx:annotation-driven />

<bean id=”transactionManager”
class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
<property name=”sessionFactory” ref=”sessionFactory” />
</bean–>

</beans>

————————————————————-

/src/main/webapp/WEB-INF/jdbc.properties

——————————————————

jdbc.driverClassName= com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://localhost:3306/ContactManager
jdbc.username=root
jdbc.password=

————————————-

src/main/webapp/WEB-INF/jsp/contact.jsp

———————————————

<%@taglib uri=”http://www.springframework.org/tags&#8221; prefix=”spring”%>
<%@taglib uri=”http://www.springframework.org/tags/form&#8221; prefix=”form”%>
<%@taglib uri=”http://java.sun.com/jsp/jstl/core&#8221; prefix=”c”%>
<html>
<head>
<title>Spring 3 MVC Series – Contact Manager</title>
<style type=”text/css”>
body {
font-family: sans-serif;
}
.data, .data td {
border-collapse: collapse;
width: 100%;
border: 1px solid #aaa;
margin: 2px;
padding: 2px;
}
.data th {
font-weight: bold;
background-color: #5C82FF;
color: white;
}
</style>
</head>
<body>

<h2>Contact Manager</h2>

<form:form method=”post” action=”add.html” commandName=”contact”>

<table>
<tr>
<td><form:label path=”firstname”><spring:message code=”label.firstname”/></form:label></td>
<td><form:input path=”firstname” /></td>
</tr>
<tr>
<td><form:label path=”lastname”><spring:message code=”label.lastname”/></form:label></td>
<td><form:input path=”lastname” /></td>
</tr>
<tr>
<td><form:label path=”email”><spring:message code=”label.email”/></form:label></td>
<td><form:input path=”email” /></td>
</tr>
<tr>
<td><form:label path=”telephone”><spring:message code=”label.telephone”/></form:label></td>
<td><form:input path=”telephone” /></td>
</tr>
<tr>
<td>Country :</td>
<td><form:select path=”country”>
<form:option value=”NONE” label=”— Select —” />
<form:options items=”${countryList}” />
</form:select>
</td>

</tr>
<tr>
<td colspan=”2″>
<input type=”submit” value=”<spring:message code=”label.addcontact”/>”/>
</td>
</tr>
</table>
</form:form>

<h3>Contacts</h3>
<c:if test=”${!empty contactList}”>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Telephone</th>
<th>&nbsp;</th>
</tr>
<c:forEach items=”${contactList}” var=”contact”>
<tr>
<td>${contact.lastname}, ${contact.firstname} </td>
<td>${contact.email}</td>
<td>${contact.telephone}</td>
<td><a href=”delete/${contact.id}”>delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>

</body>
</html>

—————————————————

src/main/resources/messages_en.properties

——————————————————

label.firstname=First Name
label.lastname=Last Name
label.email=Email
label.telephone=Telephone
label.addcontact=Add Contact

label.menu=Menu
label.title=Contact Manager
label.footer=&copy; …

——————————————-

src/main/resources/hibernate.cfg.xml

—————————————–

<?xml version=’1.0′ encoding=’utf-8′?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD//EN”
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”&gt;

<hibernate-configuration>
<session-factory>
<mapping />
</session-factory>

</hibernate-configuration>

———————————–

src/main/java/net/contact/controller/ContactController.java

—————————————————————-

import java.util.HashMap;
import java.util.Map;

import net.contact.form.Contact;
import net.contact.service.ContactService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ContactController {

@Autowired
private ContactService contactService;

@RequestMapping(“/index”)
public String listContacts(Map<String, Object> map) {

map.put(“contact”, new Contact());
map.put(“contactList”, contactService.listContact());

Map<String,String> country = new HashMap<String,String>();
country.put(“US”, “United Stated”);
country.put(“CHINA”, “China”);
country.put(“SG”, “Singapore”);
country.put(“MY”, “Malaysia”);
map.put(“countryList”, country);

return “contact”;
}

@RequestMapping(value = “/add”, method = RequestMethod.POST)
public String addContact(@ModelAttribute(“contact”)
Contact contact, BindingResult result) {

contactService.addContact(contact);

return “redirect:/index”;
}

@RequestMapping(“/delete/{contactId}”)
public String deleteContact(@PathVariable(“contactId”)
Integer contactId) {

contactService.removeContact(contactId);

return “redirect:/index”;
}
}

—————————————————————

/src/main/java/net/contact/form/Contact.java

———————————————————————-

package net.contact.form;

import java.util.Map;

public class Contact {

private Integer id;

private String firstname;

private String lastname;

private String email;

private String telephone;

private String country;

public String getEmail() {
return email;
}
public String getTelephone() {
return telephone;
}
public void setEmail(String email) {
this.email = email;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}

}

——————————————-

/src/main/java/net/contact/service/ContactService.java

———————————————————————–

package net.contact.service;

import java.util.List;

import net.contact.form.Contact;

public interface ContactService {

public void addContact(Contact contact);
public List<Contact> listContact();
public void removeContact(Integer id);
}

————————————————-

src/main/java/net/contact/service/ContactServiceImpl.java

———————————————————–

package net.contact.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.contact.dao.ContactDAO;
import net.contact.form.Contact;

//@Service
public class ContactServiceImpl implements ContactService {

// @Autowired
private ContactDAO contactDAO;

public void setContactDAO(ContactDAO contactDAO) {
this.contactDAO = contactDAO;
}

// @Transactional
public void addContact(Contact contact) {
contactDAO.addContact(contact);
}

// @Transactional
public List<Contact> listContact() {

return contactDAO.listContact();
}

// @Transactional
public void removeContact(Integer id) {

}
}

————————————————————–

src/main/java/net/contact/dao/ContactDAO.java

—————————————————–

package net.contact.dao;

import java.util.List;

import net.contact.form.Contact;

public interface ContactDAO {

public void addContact(Contact contact);
public List<Contact> listContact();
public void removeContact(Integer id);
}

—————————————————–

src/main/java/net/contact/dao/ContactDAOImpl.java

——————————————–

package net.contact.dao;

import java.util.ArrayList;
import java.util.List;

import net.contact.form.Contact;

//import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

public class ContactDAOImpl implements ContactDAO {

public void addContact(Contact contact) {

System.out.println(contact.getFirstname());
//sessionFactory.getCurrentSession().save(contact);
}

public List<Contact> listContact() {

Contact c1 = new Contact();
c1.setFirstname(“Aju”);
c1.setId(1);
c1.setEmail(“test@gmail.com”);
c1.setLastname(“B”);
List<Contact> list = new ArrayList<Contact>();
list.add(c1);
return list;
// return sessionFactory.getCurrentSession().createQuery(“from Contact”)
// .list();
}

public void removeContact(Integer id) {
// Contact contact = (Contact) sessionFactory.getCurrentSession().load(
// Contact.class, id);
// if (null != contact) {
// sessionFactory.getCurrentSession().delete(contact);
// }

}
}

———————————————————-

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s