Adding the original version from the blog.
This commit is contained in:
parent
8912c139cf
commit
c6ce8216d8
|
@ -0,0 +1,73 @@
|
|||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
||||
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>de.lusiardi</groupId>
|
||||
<artifactId>webbasics2</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<name>Java Webprogrammierung mit Spring und Maven (Teil 2)</name>
|
||||
<prerequisites>
|
||||
<maven>3.0</maven>
|
||||
</prerequisites>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>
|
||||
UTF-8
|
||||
</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>3.2.3.RELEASE</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
<version>3.1.2.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<version>4.1.6.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cglib</groupId>
|
||||
<artifactId>cglib</artifactId>
|
||||
<version>2.2.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.21</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Context path="/WebBasics2"/>
|
|
@ -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
|
|
@ -0,0 +1,57 @@
|
|||
<?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:mvc="http://www.springframework.org/schema/mvc"
|
||||
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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
|
||||
|
||||
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
|
||||
|
||||
<context:component-scan base-package="de.lusiardi.testprojekt" />
|
||||
|
||||
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
|
||||
|
||||
<mvc:annotation-driven />
|
||||
|
||||
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/views/" />
|
||||
<property name="suffix" value=".jsp" />
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="location" value="/WEB-INF/config/jdbc/jdbc.properties">
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
<property name="driverClassName" value="${jdbc.driverClassName}">
|
||||
</property>
|
||||
<property name="url" value="${jdbc.url}"></property>
|
||||
<property name="username" value="${jdbc.username}"></property>
|
||||
<property name="password" value="${jdbc.password}"></property>
|
||||
</bean>
|
||||
|
||||
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
|
||||
<property name="dataSource" ref="myDataSource"></property>
|
||||
<property name="packagesToScan" value="de.lusiardi.testprojekt" >
|
||||
</property>
|
||||
<property name="hibernateProperties">
|
||||
<props>
|
||||
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
|
||||
<prop key="hibernate.show_sql">true</prop>
|
||||
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
|
||||
</props>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
|
||||
<property name="dataSource" ref="myDataSource" />
|
||||
<property name="sessionFactory" ref="sessionFactory" />
|
||||
</bean>
|
||||
|
||||
<tx:annotation-driven transaction-manager="txManager" />
|
||||
</beans>
|
|
@ -0,0 +1,25 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app version="3.0"
|
||||
xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
|
||||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
|
||||
|
||||
<display-name>Spring3MVC</display-name>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.jsp</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>*.html</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
</web-app>
|
|
@ -0,0 +1,12 @@
|
|||
<%@page contentType="text/html" pageEncoding="UTF-8"%>
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
||||
"http://www.w3.org/TR/html4/loose.dtd">
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Lusiardi.de - Tutorial</title>
|
||||
</head>
|
||||
<body>
|
||||
<a href="main/view.html">Controller</a><br />
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,12 @@
|
|||
<%@page contentType="text/html" pageEncoding="UTF-8"%>
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
||||
"http://www.w3.org/TR/html4/loose.dtd">
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Lusiardi.de - Tutorial</title>
|
||||
</head>
|
||||
<body>
|
||||
<a href="../">${key}</a><br />
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue