From c6ce8216d8128777575b264851b6e5a2a91b569d Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sun, 28 Jul 2013 00:59:57 -0700 Subject: [PATCH] Adding the original version from the blog. --- pom.xml | 73 +++++++++++++++++++ .../controller/MainController.java | 32 ++++++++ .../lusiardi/testprojekt/dao/CounterDao.java | 40 ++++++++++ .../lusiardi/testprojekt/entity/Counter.java | 35 +++++++++ src/main/webapp/META-INF/context.xml | 2 + .../WEB-INF/config/jdbc/jdbc.properties | 7 ++ src/main/webapp/WEB-INF/spring-servlet.xml | 57 +++++++++++++++ src/main/webapp/WEB-INF/web.xml | 25 +++++++ src/main/webapp/index.jsp | 12 +++ src/main/webapp/views/view.jsp | 12 +++ 10 files changed, 295 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/de/lusiardi/testprojekt/controller/MainController.java create mode 100644 src/main/java/de/lusiardi/testprojekt/dao/CounterDao.java create mode 100644 src/main/java/de/lusiardi/testprojekt/entity/Counter.java create mode 100644 src/main/webapp/META-INF/context.xml create mode 100644 src/main/webapp/WEB-INF/config/jdbc/jdbc.properties create mode 100644 src/main/webapp/WEB-INF/spring-servlet.xml create mode 100644 src/main/webapp/WEB-INF/web.xml create mode 100644 src/main/webapp/index.jsp create mode 100644 src/main/webapp/views/view.jsp diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5ce15bf --- /dev/null +++ b/pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + + de.lusiardi + webbasics2 + 1.0-SNAPSHOT + war + + Java Webprogrammierung mit Spring und Maven (Teil 2) + + 3.0 + + + + UTF-8 + + + + + + org.springframework + spring-webmvc + 3.2.3.RELEASE + jar + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + org.springframework + spring-orm + 3.1.2.RELEASE + + + org.hibernate + hibernate-entitymanager + 4.1.6.Final + + + cglib + cglib + 2.2.2 + + + mysql + mysql-connector-java + 5.1.21 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + + + \ No newline at end of file diff --git a/src/main/java/de/lusiardi/testprojekt/controller/MainController.java b/src/main/java/de/lusiardi/testprojekt/controller/MainController.java new file mode 100644 index 0000000..34009db --- /dev/null +++ b/src/main/java/de/lusiardi/testprojekt/controller/MainController.java @@ -0,0 +1,32 @@ +package de.lusiardi.testprojekt.controller; + +import de.lusiardi.testprojekt.dao.CounterDao; +import de.lusiardi.testprojekt.entity.Counter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +@Controller +public class MainController { + + @Autowired + CounterDao counterDao; + + @RequestMapping("view") + public ModelAndView getTest() { + Counter c = counterDao.findByPage("test"); + if (c == null) { + c = new Counter(); + c.setPage("test"); + c.setCount(0); + counterDao.save(c); + } + int count = c.getCount() + 1; + c.setCount(count); + counterDao.update(c); + + return new ModelAndView("view", "key", "Page view number: " + + count); + } +} \ No newline at end of file diff --git a/src/main/java/de/lusiardi/testprojekt/dao/CounterDao.java b/src/main/java/de/lusiardi/testprojekt/dao/CounterDao.java new file mode 100644 index 0000000..9642b6c --- /dev/null +++ b/src/main/java/de/lusiardi/testprojekt/dao/CounterDao.java @@ -0,0 +1,40 @@ +package de.lusiardi.testprojekt.dao; + +import de.lusiardi.testprojekt.entity.Counter; +import org.hibernate.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +@Repository +public class CounterDao { + + @Autowired + SessionFactory sessionFactory; + + @Transactional(readOnly = true) + public Counter findByPage(String page) { + Query q = getCurrentSession().getNamedQuery("counter.findByPage"); + q.setString("page", page); + return (Counter) q.uniqueResult(); + } + + @Transactional + public void save(final Counter entity) { + getCurrentSession().persist(entity); + } + + @Transactional + public void update(final Counter entity) { + getCurrentSession().merge(entity); + } + + @Transactional + public void delete(final Counter entity) { + getCurrentSession().delete(entity); + } + + private Session getCurrentSession() throws HibernateException { + return sessionFactory.getCurrentSession(); + } +} \ No newline at end of file diff --git a/src/main/java/de/lusiardi/testprojekt/entity/Counter.java b/src/main/java/de/lusiardi/testprojekt/entity/Counter.java new file mode 100644 index 0000000..0aa5c23 --- /dev/null +++ b/src/main/java/de/lusiardi/testprojekt/entity/Counter.java @@ -0,0 +1,35 @@ +package de.lusiardi.testprojekt.entity; + +import java.io.Serializable; +import javax.persistence.*; + +@Entity +@NamedQueries({ + @NamedQuery(name = "counter.findByPage", query = "from Counter c where c.page=:page") +}) +public class Counter implements Serializable { + + @Id + @GeneratedValue + private Long id; + @Column(nullable = false, unique = true) + private String page; + @Column(nullable = false, name = "counter") + private int count; + + public String getPage() { + return page; + } + + public void setPage(String page) { + this.page = page; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } +} \ No newline at end of file diff --git a/src/main/webapp/META-INF/context.xml b/src/main/webapp/META-INF/context.xml new file mode 100644 index 0000000..ade03dd --- /dev/null +++ b/src/main/webapp/META-INF/context.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/config/jdbc/jdbc.properties b/src/main/webapp/WEB-INF/config/jdbc/jdbc.properties new file mode 100644 index 0000000..d7e25d0 --- /dev/null +++ b/src/main/webapp/WEB-INF/config/jdbc/jdbc.properties @@ -0,0 +1,7 @@ +# Diese Werte entsprechend anpassen +jdbc.url=jdbc:mysql://localhost:3306/webbasics2 +jdbc.username=webbasics +jdbc.password=Patobi34 + +jdbc.driverClassName=com.mysql.jdbc.Driver +jdbc.dialect=org.hibernate.dialect.MySQL5Dialect \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/spring-servlet.xml b/src/main/webapp/WEB-INF/spring-servlet.xml new file mode 100644 index 0000000..6270478 --- /dev/null +++ b/src/main/webapp/WEB-INF/spring-servlet.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ${jdbc.dialect} + true + create-drop + + + + + + + + + + + \ 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 100644 index 0000000..318e0c7 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,25 @@ + + + + Spring3MVC + + + index.jsp + + + + spring + org.springframework.web.servlet.DispatcherServlet + 1 + + + + spring + *.html + + + \ No newline at end of file diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp new file mode 100644 index 0000000..2489f31 --- /dev/null +++ b/src/main/webapp/index.jsp @@ -0,0 +1,12 @@ +<%@page contentType="text/html" pageEncoding="UTF-8"%> + + + + + Lusiardi.de - Tutorial + + + Controller
+ + \ No newline at end of file diff --git a/src/main/webapp/views/view.jsp b/src/main/webapp/views/view.jsp new file mode 100644 index 0000000..ab1d4c8 --- /dev/null +++ b/src/main/webapp/views/view.jsp @@ -0,0 +1,12 @@ +<%@page contentType="text/html" pageEncoding="UTF-8"%> + + + + + Lusiardi.de - Tutorial + + + ${key}
+ + \ No newline at end of file