diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..beef00d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.classpath +.project +.settings +target diff --git a/context.xml b/context.xml new file mode 100755 index 0000000..baa80fe --- /dev/null +++ b/context.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..740a190 --- /dev/null +++ b/pom.xml @@ -0,0 +1,257 @@ + + + 4.0.0 + info.ahlberg + spring-mvc-template + war + 1.0-SNAPSHOT + spring-mvc-template + http://ahlberg.info + + 3.0.6.RELEASE + + + + + central + http://repo1.maven.org/maven2 + + + maven2.java.net + http://download.java.net/maven/2 + + + java.net2 + http://download.java.net/maven/2 + + + maven2.jboss.org + http://repository.jboss.org/maven2 + + + + + + + org.codehaus.mojo + tomcat-maven-plugin + + + target/spring-mvc-template.war + context.xml + + + + org.apache.maven.plugins + + maven-eclipse-plugin + + 2.8 + + [artifactId]-[version] + true + true + 2.0 + ${basedir}/src/main/resources/META-INF/MANIFEST.MF + true + true + + + + maven-compiler-plugin + + 1.6 + 1.6 + UTF-8 + + + + maven-resources-plugin + + UTF-8 + + + + gallery + + + + + org.springframework + spring-core + ${spring.version} + + + org.springframework + spring-web + ${spring.version} + + + org.springframework + spring-webmvc + ${spring.version} + + + org.springframework + spring-orm + ${spring.version} + + + org.springframework + spring-jdbc + ${spring.version} + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-aop + ${spring.version} + + + + + org.codehaus.jackson + jackson-mapper-lgpl + 1.8.1 + + + + + jstl + jstl + 1.2 + + + javax.persistence + persistence-api + 1.0 + + + javax.servlet + servlet-api + 2.5 + provided + + + org.hibernate + hibernate + 3.2.6.ga + + + org.hibernate + hibernate-annotations + + 3.3.1.GA + + + org.hibernate + hibernate-entitymanager + + 3.3.2.GA + + + + log4j + log4j + 1.2.15 + + + com.sun.jmx + jmxri + + + com.sun.jdmk + jmxtools + + + + + org.slf4j + slf4j-api + 1.5.8 + + + org.slf4j + slf4j-log4j12 + 1.5.8 + + + + hsqldb + hsqldb + 1.8.0.7 + + + javax.el + el-api + 1.0 + provided + + + javax.servlet.jsp + jsp-api + 2.1 + provided + + + + javax.annotation + jsr250-api + 1.0 + + + + javax.persistence + persistence-api + 1.0 + + + + junit + junit + 4.7 + test + + + org.easymock + easymock + 2.5.2 + test + + + org.springframework + spring-test + ${spring.version} + test + + + org.unitils + unitils-easymock + 3.1 + test + + + org.unitils + unitils-inject + 3.1 + test + + + + + + + + + javax.mail + mail + 1.4.2 + + + + diff --git a/src/main/java/info/ahlberg/spring/Contact.java b/src/main/java/info/ahlberg/spring/Contact.java new file mode 100755 index 0000000..13190a1 --- /dev/null +++ b/src/main/java/info/ahlberg/spring/Contact.java @@ -0,0 +1,50 @@ +package info.ahlberg.spring; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Version; + +@Entity +public class Contact implements Serializable { + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Version + private Long version; + + @Column(nullable = false) + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getVersion() { + return version; + } + + public void setVersion(Long version) { + this.version = version; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/src/main/java/info/ahlberg/spring/ContactController.java b/src/main/java/info/ahlberg/spring/ContactController.java new file mode 100755 index 0000000..97ce1d7 --- /dev/null +++ b/src/main/java/info/ahlberg/spring/ContactController.java @@ -0,0 +1,37 @@ +package info.ahlberg.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +@RequestMapping("/") +public class ContactController { + @Autowired + private ContactService contactService; + + @RequestMapping("/") + String index(Model model) { + model.addAttribute("msg", "A nice message"); + + model.addAttribute("contacts", contactService.findAll()); + + return "index"; + } + + + @RequestMapping("/add") + String add(@RequestParam String name, Model model) { + model.addAttribute("msg", "A nice message"); + + Contact contact = new Contact(); + contact.setName(name); + contactService.save(contact); + + model.addAttribute("contacts", contactService.findAll()); + + return "index"; + } +} diff --git a/src/main/java/info/ahlberg/spring/ContactDAO.java b/src/main/java/info/ahlberg/spring/ContactDAO.java new file mode 100755 index 0000000..c66753f --- /dev/null +++ b/src/main/java/info/ahlberg/spring/ContactDAO.java @@ -0,0 +1,5 @@ +package info.ahlberg.spring; + +public interface ContactDAO extends GenericDAO { + +} diff --git a/src/main/java/info/ahlberg/spring/ContactDAOWithJPA.java b/src/main/java/info/ahlberg/spring/ContactDAOWithJPA.java new file mode 100755 index 0000000..f138b07 --- /dev/null +++ b/src/main/java/info/ahlberg/spring/ContactDAOWithJPA.java @@ -0,0 +1,9 @@ +package info.ahlberg.spring; + +import org.springframework.stereotype.Repository; + + +@Repository +public class ContactDAOWithJPA extends GenericDAOWithJPA implements ContactDAO { + +} diff --git a/src/main/java/info/ahlberg/spring/ContactService.java b/src/main/java/info/ahlberg/spring/ContactService.java new file mode 100755 index 0000000..afa56fa --- /dev/null +++ b/src/main/java/info/ahlberg/spring/ContactService.java @@ -0,0 +1,9 @@ +package info.ahlberg.spring; + +public interface ContactService { + + Object findAll(); + + void save(Contact contact); + +} diff --git a/src/main/java/info/ahlberg/spring/ContactServiceImpl.java b/src/main/java/info/ahlberg/spring/ContactServiceImpl.java new file mode 100755 index 0000000..e0325df --- /dev/null +++ b/src/main/java/info/ahlberg/spring/ContactServiceImpl.java @@ -0,0 +1,23 @@ +package info.ahlberg.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class ContactServiceImpl implements ContactService { + @Autowired + private ContactDAO contactDAO; + + @Override + public Object findAll() { + return contactDAO.loadAll(); + } + + @Override + @Transactional + public void save(Contact contact) { + contactDAO.persist(contact); + } + +} diff --git a/src/main/java/info/ahlberg/spring/GenericDAO.java b/src/main/java/info/ahlberg/spring/GenericDAO.java new file mode 100755 index 0000000..539127d --- /dev/null +++ b/src/main/java/info/ahlberg/spring/GenericDAO.java @@ -0,0 +1,19 @@ +package info.ahlberg.spring; + +import java.io.Serializable; +import java.util.List; + +public interface GenericDAO { + + T loadById(ID id); + + void persist(T entity); + + void update(T entity); + + void delete(T entity); + + List loadAll(); + + void flush(); +} diff --git a/src/main/java/info/ahlberg/spring/GenericDAOWithJPA.java b/src/main/java/info/ahlberg/spring/GenericDAOWithJPA.java new file mode 100755 index 0000000..4fbb5b5 --- /dev/null +++ b/src/main/java/info/ahlberg/spring/GenericDAOWithJPA.java @@ -0,0 +1,61 @@ +package info.ahlberg.spring; + +import java.io.Serializable; +import java.lang.reflect.ParameterizedType; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +public abstract class GenericDAOWithJPA implements GenericDAO { + + private Class persistentClass; + + protected EntityManager entityManager; + + @SuppressWarnings("unchecked") + public GenericDAOWithJPA() { + this.persistentClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()) + .getActualTypeArguments()[0]; + } + + @PersistenceContext + public void setEntityManager(EntityManager entityManager) { + this.entityManager = entityManager; + } + + public void flush() { + entityManager.flush(); + } + + public Class getPersistentClass() { + return persistentClass; + } + + public T loadById(ID id) { + if(id == null) { + return null; + } else { + return entityManager.find(persistentClass, id); + } + } + + public void persist(T entity) { + entityManager.persist(entity); + } + + public void update(T entity) { + entityManager.merge(entity); + entityManager.flush(); + } + + public void delete(T entity) { + entityManager.remove(entity); + } + + @SuppressWarnings("unchecked") + public List loadAll() { + return entityManager.createQuery("Select t from " + persistentClass.getSimpleName() + " t") + .setHint("org.hibernate.cacheable", true).getResultList(); + } +} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml new file mode 100755 index 0000000..ffa8e7e --- /dev/null +++ b/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,13 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/hibernate.properties b/src/main/resources/hibernate.properties new file mode 100755 index 0000000..e69de29 diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml new file mode 100755 index 0000000..609a21a --- /dev/null +++ b/src/main/resources/log4j.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/META-INF/MANIFEST.MF b/src/main/webapp/META-INF/MANIFEST.MF new file mode 100755 index 0000000..2a52efb --- /dev/null +++ b/src/main/webapp/META-INF/MANIFEST.MF @@ -0,0 +1,17 @@ +Manifest-Version: 1.0 +Class-Path: activation-1.1.jar antlr-2.7.6.jar aopalliance-1.0.jar asm + -1.5.3.jar asm-attrs-1.5.3.jar cglib-2.1_3.jar commons-collections-2. + 1.1.jar commons-logging-1.0.4.jar dom4j-1.6.1.jar ehcache-1.2.3.jar e + jb3-persistence-1.0.1.GA.jar hibernate-3.2.6.ga.jar hibernate-annotat + ions-3.3.1.GA.jar hibernate-commons-annotations-3.0.0.ga.jar hibernat + e-entitymanager-3.3.2.GA.jar hsqldb-1.8.0.7.jar jackson-core-lgpl-1.8 + .1.jar jackson-mapper-lgpl-1.8.1.jar javassist-3.4.GA.jar jms-1.1.jar + jsr250-api-1.0.jar jstl-1.2.jar jta-1.0.1B.jar log4j-1.2.15.jar mail + -1.4.2.jar persistence-api-1.0.jar slf4j-api-1.5.8.jar slf4j-log4j12- + 1.5.8.jar spring-aop-3.0.6.RELEASE.jar spring-asm-3.0.6.RELEASE.jar s + pring-beans-3.0.6.RELEASE.jar spring-context-3.0.6.RELEASE.jar spring + -context-support-3.0.6.RELEASE.jar spring-core-3.0.6.RELEASE.jar spri + ng-expression-3.0.6.RELEASE.jar spring-jdbc-3.0.6.RELEASE.jar spring- + orm-3.0.6.RELEASE.jar spring-tx-3.0.6.RELEASE.jar spring-web-3.0.6.RE + LEASE.jar spring-webmvc-3.0.6.RELEASE.jar + diff --git a/src/main/webapp/WEB-INF/config/data-access-config.xml b/src/main/webapp/WEB-INF/config/data-access-config.xml new file mode 100755 index 0000000..749f494 --- /dev/null +++ b/src/main/webapp/WEB-INF/config/data-access-config.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/config/web-application-config.xml b/src/main/webapp/WEB-INF/config/web-application-config.xml new file mode 100755 index 0000000..07e0641 --- /dev/null +++ b/src/main/webapp/WEB-INF/config/web-application-config.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + diff --git a/src/main/webapp/WEB-INF/config/webmvc-config.xml b/src/main/webapp/WEB-INF/config/webmvc-config.xml new file mode 100755 index 0000000..3b8b477 --- /dev/null +++ b/src/main/webapp/WEB-INF/config/webmvc-config.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + diff --git a/src/main/webapp/WEB-INF/view/index.jsp b/src/main/webapp/WEB-INF/view/index.jsp new file mode 100755 index 0000000..dd9c741 --- /dev/null +++ b/src/main/webapp/WEB-INF/view/index.jsp @@ -0,0 +1,43 @@ + + + + + + + + + + + + Spring MVC example + + + + +
+ + + +
+ +
+ +
    + + + +
  • + +
    + +
+ +
+ + + +
\ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100755 index 0000000..aef57b0 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,83 @@ + + + + + contextConfigLocation + + /WEB-INF/config/web-application-config.xml + + + + + characterEncodingFilter + + org.springframework.web.filter.CharacterEncodingFilter + + encoding + UTF-8 + + + forceEncoding + true + + + + characterEncodingFilter + /* + + + + + JPA Filter + + org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter + + + JPA Filter + /* + + + + + + org.springframework.web.context.ContextLoaderListener + + + + + default + *.css + + + default + *.gif + + + default + *.js + + + + + Spring MVC Dispatcher Servlet + + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + + + 1 + + + Spring MVC Dispatcher Servlet + / + + + + + index.html + + + \ No newline at end of file