The Ultimate Hello World with: Maven + Spring (Spring MVC + ORM) + JPA + Tomcat

This is the Ultimate Hello World!

This is my Directory Layout

Lets see the files one by one:
pom.xml
<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/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
    <groupId>biz.tugay.au</groupId>
    <artifactId>au</artifactId>
    <packaging>war</packaging>
 
    <version>1.0-SNAPSHOT</version>
    <name>au Maven Webapp</name>
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
 
        <!-- Persistency Dependencies -->
 
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
            <scope>compile</scope>
        </dependency>
 
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>
 
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.10.Final</version>
            <scope>runtime</scope>
        </dependency>
 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
 
        <!-- Spring Dependencies -->
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.0.5.RELEASE</version>
            <scope>compile</scope>
        </dependency>
 
    </dependencies>
 
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
 
    <build>
        <finalName>augg</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

and web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="3.0">
 
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
 
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <!-- jsp configurations -->
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <!--
            <include-prelude>/WEB-INF/views/headerAndFooter/header.jsp</include-prelude>
            <include-coda>/WEB-INF/views/headerAndFooter/footer.jsp</include-coda>
            -->
            <scripting-invalid>true</scripting-invalid>
            <trim-directive-whitespaces>true</trim-directive-whitespaces>
            <default-content-type>text/html</default-content-type>
        </jsp-property-group>
    </jsp-config>
 
    <session-config>
        <session-timeout>30</session-timeout>
        <cookie-config>
            <http-only>true</http-only>
        </cookie-config>
        <tracking-mode>COOKIE</tracking-mode>
    </session-config>
 
</web-app>

context.xml
<?xml version='1.0' encoding='utf-8'?>
<Context>
    <Resource name="jdbc/dboard"
              type="javax.sql.DataSource"
              maxActive="20" maxIdle="5" maxWait="10000"
              username="root" password=""
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/dboard?useUnicode=yes&amp;characterEncoding=UTF-8"/>
</Context>

persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
                                 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
 
    <persistence-unit name="dboard" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <non-jta-data-source>java:comp/env/jdbc/dboard</non-jta-data-source>
        <properties>
            <property name="connection.useUnicode" value="true"/>
            <property name="connection.characterEncoding" value="utf-8"/>
        </properties>
    </persistence-unit>
 
</persistence>

Discussion.java
package biz.tugay.au.model;
/* User: koray@tugay.biz Date: 12/08/15 Time: 09:45 */
 
import javax.persistence.*;
 
@Entity
@Table(name = "discussion")
public class Discussion {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
 
    @Basic
    private String user;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getUser() {
        return user;
    }
 
    public void setUser(String user) {
        this.user = user;
    }
 
}

DiscussionDao.java
package biz.tugay.au.model;
/* User: koray@tugay.biz Date: 12/08/15 Time: 14:43 */
 
public interface DiscussionDao {
 
    public Discussion getDiscussion();
    public void save(Discussion discussion);
 
}

DiscussionDaoImpl.java
package biz.tugay.au.model;
/* User: koray@tugay.biz Date: 12/08/15 Time: 10:05 */
 
import org.springframework.stereotype.Repository;
 
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
 
@Repository
public class DiscussionDaoImpl implements DiscussionDao {
 
    @PersistenceContext
    EntityManager entityManager;
 
    @Override
    @Transactional
    public Discussion getDiscussion() {
        return entityManager.find(Discussion.class, 1);
    }
 
    @Override
    @Transactional
    public void save(Discussion discussion) {
        entityManager.persist(discussion);
    }
 
}

HelloController.java
package biz.tugay.au.web;
/* User: koray@tugay.biz Date: 12/08/15 Time: 09:36 */
 
import biz.tugay.au.model.Discussion;
import biz.tugay.au.model.DiscussionDao;
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.RequestMethod;
 
@Controller
@RequestMapping(value = "/")
public class HelloController {
 
    @Autowired
    DiscussionDao discussionDao;
 
    @RequestMapping(method = RequestMethod.GET, value = "/")
    protected String doGet(Model model) {
        model.addAttribute("helloMessage", discussionDao.getDiscussion().getUser());
        return "main.jsp";
    }
 
    @RequestMapping(method = RequestMethod.POST, value = "/")
    protected String doPost(Model model, Discussion discussion) {
        discussionDao.save(discussion);
        model.addAttribute("helloMessage", discussionDao.getDiscussion().getUser());
        return "main.jsp";
    }
 
}

main.jsp
<%@ page pageEncoding="utf-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
</head>
<body>
 
<div>
    ${requestScope.helloMessage}
</div>
 
<br/>
<br/>
 
<div>
    <form method="post">
        <label>User:
            <input type="text" name="user"/>
        </label>
        <input type="submit"/>
    </form>
</div>
 
</body>
</html>

This is the relavent table:
mysql> DESCRIBE discussion;
+------+-----------------+-----+----+--------+---------------+
| Field| Type            | Null| Key| Default| Extra         |
+------+-----------------+-----+----+--------+---------------+
| id   | int(11) unsigned| NO  | PRI| NULL   | auto_increment|
| user | varchar(40)     | YES |    | NULL   |               |
+------+-----------------+-----+----+--------+---------------+

Currently the state of the table is as follows:
mysql> SELECT * from discussion;
+----+-------+
| id | user  |
+----+-------+
|  1 | koray |
+----+-------+
1 row in set (0.00 sec)

and when the application is deployed to Tomcat, main page will be as follows:
and when I type something in User field and click Submit:
+----+--------+
| id | user   |
+----+--------+
|  1 | koray  |
|  2 | frfrfr |
+----+--------+
1 row in set (0.00 sec)
Dude! Everything works!