Eclipse Object Db Spring Mvc

28
Eclipse/JPA Spring MVC Web Tutorial This is the Eclipse version of the Spring MVC and JPA tutorial. http://www.objectdb.com/tutorial/jpa/eclipse/spring It demonstrates how to create and run a full Java Spring MVC (Model View Controller) web application in Eclipse - using ObjectDB, JPA, Tomcat (or GlassFish) and Maven. The demo web application manages a basic guestbook page. Every visitor can sign the guestbook by filling a simple form. The visitors that have already signed are listed below the form: Persiste nce is managed by JPA. Every visitor that has signed the guestbook is represented by a Guest JPA entity object and all the Guest entity objects are stored in an ObjectDB database. Required Software For this tutorial you will need the following software:

description

MVC and database connection

Transcript of Eclipse Object Db Spring Mvc

Page 1: Eclipse Object Db Spring Mvc

Eclipse/JPA Spring MVC Web TutorialThis is the Eclipse version of the Spring MVC and JPA tutorial.

http://www.objectdb.com/tutorial/jpa/eclipse/spring

It demonstrates how to create and run a full Java Spring MVC (Model View Controller)

web application in Eclipse - using ObjectDB, JPA, Tomcat (or GlassFish) and Maven.

The demo web application manages a basic guestbook page. Every visitor can sign the

guestbook by filling a simple form. The visitors that have already signed are listed below

the form:

Persistence is managed by JPA. Every visitor that has signed the guestbook is

represented by a Guest JPA entity object and all the Guest entity objects are stored in

an ObjectDB database.

Required Software

For this tutorial you will need the following software:

Java JDK 6.0  (or above).

Eclipse IDE for Java EE Developers .

Page 2: Eclipse Object Db Spring Mvc

m2eclipse (Maven plugin)  including Maven Integration for WTP (in m2eclipse

extras).

The tutorial was written for Eclipse 3.6.1 but it should work with other Eclipse versions

as well (dialog boxes and menus might look slightly different but the code would be the

same).

The tutorial is based on using a Maven project. Therefore, required JAR files (ObjectDB,

Spring Framework) will be downloaded automatically during build from Maven

repositories.

The Tutorial Steps

This tutorial consists of the following steps:

Step 1: Create a Maven Web Project

Step 2: Entity Class and Persistence Unit

Step 3: Define a Spring DAO Component

Step 4: Add a Controller Class

Step 5: Add a JSP Page

Step 6: Set the Spring XML

Step 7: Run the Spring Web App

Step 1: Create a Maven Web ProjectWe start by creating a new Maven-WTP dynamic web project in Eclipse:

Open the [New Project] dialog box, e.g. by using File > New > Project...

Select Maven > Maven Project and click Next.

Verify that the Create a simple project checkbox is disabled and click Next.

Enter maven-archetype-webapp as a filter, select maven-archetype-

webapp in the artifact list and click Next.

Page 3: Eclipse Object Db Spring Mvc

Enter Group Id (e.g. Guestbook), Artifact Id (e.g. Guestbook), Version (e.g. 1.0)

and Package (guest), and click Finish to create the project.

Page 4: Eclipse Object Db Spring Mvc

The configuration of the new created project is contained in a pom.xml file that was

created in the project main folder:

Note: You should verify now that Maven Integration for WTP is installed, by right

clicking the project node and checking that the Run As > Run on Server command

is available. If it is not available - you have to install Maven Integration for WTP and

create a new project.

To add dependency on the Spring MVC Framework and ObjectDB and to automatically

download all the required JAR files from Maven repositories - open the pom.xml file and

Page 5: Eclipse Object Db Spring Mvc

in the pom.xmltab use copy and paste to replace its content with the following

settings:

<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>com.objectdb.tutorial.spring</groupId>

<artifactId>Guestbook</artifactId>

<packaging>war</packaging>

<version>1.0</version>

<name>Guestbook</name>

<url>http://maven.apache.org</url>

<properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

 </properties>

 

<repositories>

<repository>

<id>objectdb</id>

<name>ObjectDB Repository</name>

<url>http://m2.objectdb.com</url>

</repository>

</repositories>

 

<dependencies>

<dependency>

<groupId>com.objectdb</groupId>

<artifactId>objectdb</artifactId>

<version>2.0.4</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

<scope>provided</scope>

</dependency>

Page 6: Eclipse Object Db Spring Mvc

<dependency>

<groupId>javax.servlet.jsp</groupId>

<artifactId>jsp-api</artifactId>

<version>2.1</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>3.0.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>3.0.5.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-orm</artifactId>

<version>3.0.5.RELEASE</version>

</dependency>

<dependency>

<groupId>aopalliance</groupId>

<artifactId>aopalliance</artifactId>

<version>1.0</version>

</dependency>

<dependency>

<groupId>cglib</groupId>

<artifactId>cglib</artifactId>

<version>2.2</version>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.6.10</version>

</dependency>

</dependencies>

 

<build>

Page 7: Eclipse Object Db Spring Mvc

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>2.3.2</version>

<configuration>

<source>1.5</source>

<target>1.5</target>

</configuration>

</plugin>

<plugin>

<groupId>org.mortbay.jetty</groupId>

<artifactId>maven-jetty-plugin</artifactId>

<version>6.1.10</version>

<configuration>

<scanIntervalSeconds>10</scanIntervalSeconds>

<stopKey>foo</stopKey>

<stopPort>9999</stopPort>

</configuration>

<executions>

<execution>

<id>start-jetty</id>

<phase>pre-integration-test</phase>

<goals>

<goal>run</goal>

</goals>

<configuration>

<scanIntervalSeconds>0</scanIntervalSeconds>

<daemon>true</daemon>

</configuration>

</execution>

<execution>

<id>stop-jetty</id>

<phase>post-integration-test</phase>

<goals>

<goal>stop</goal>

</goals>

</execution>

</executions>

Page 8: Eclipse Object Db Spring Mvc

</plugin>

</plugins>

<finalName>Guestbook</finalName>

</build>

 

</project>

Now you should have a Maven based Eclipse dynamic web project (WTP) with Spring

MVC Framework and ObjectDB/JPA support.

The next step is creating a JPA Entity class.

Step 2: Entity Class and Persistence UnitTo store objects in an ObjectDB database using JPA we need to define an entity class:

Right click the project node (in the [Package Explorer] window), select New >

Source Folder, enter src/main/java as Folder Name and click Finish.

Open the [New Java Class] dialog box, e.g. by right clicking the new source

directory node (in the [Package Explorer] window) and selecting New > Class.

Enter guest as the package name - use exactly that case sensitive package name.

Enter Guest as the class name - use exactly that case sensitive class name.

Page 9: Eclipse Object Db Spring Mvc

Click Finish to create the new entity class.

A new class that should represent Guest objects in the database was created in the

project (under Java Resources: src/main/java > guest).

Use copy and paste to replace the new source file content with the following code:

package guest;

 

import java.io.Serializable;

Page 10: Eclipse Object Db Spring Mvc

import java.sql.Date;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

 

@Entity

public class Guest implements Serializable {

private static final long serialVersionUID = 1L;

 

// Persistent Fields:

@Id @GeneratedValue

Long id;

private String name;

private Date signingDate;

 

// Constructors:

public Guest() {

}

 

public Guest(String name) {

this.name = name;

this.signingDate = new Date(System.currentTimeMillis());

}

 

// String Representation:

@Override

public String toString() {

return name + " (signed on " + signingDate + ")";

}

}

The Guest entity class will represents guests in the database.

We also need to configure JPA by setting a META-INF/persistence.xml file:

Right click the src/main/resources node (in the [Project Explorer]) and select New

> Folder.

The parent folder should be resources.

Enter META-INF as a new folder name and click Finish.

Page 11: Eclipse Object Db Spring Mvc

Right click the new META-INF folder in the [Project Explorer] window, select New >

File, enter persistence.xml as file name and click Finish.

Verify that a new persistence.xml file was created as shown below:

Page 12: Eclipse Object Db Spring Mvc

Finally, use copy and paste to copy the following content to the persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/persistence

http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

 

<persistence-unit name="GuestbookPU" transaction-type="RESOURCE_LOCAL">

<provider>com.objectdb.jpa.Provider</provider>

<properties>

<property name="javax.persistence.jdbc.url" value="$objectdb/db/guests.odb"/>

<property name="javax.persistence.jdbc.user" value="admin"/>

<property name="javax.persistence.jdbc.password" value="admin"/>

</properties>

</persistence-unit>

 

</persistence>

Now ObjectDB should be used as a JPA provider with the specified database url.

The next step is adding a Spring DAO Component class that will manage Guest entity

objects.

Step 3: Define a Spring DAO ComponentOperations on the database will be performed by an instance of a Data Access Object

(DAO) that we will define in this step as a Spring MVC component:

Page 13: Eclipse Object Db Spring Mvc

Open the [New Java Class] dialog box by right clicking the guest package node (in

the [Package Explorer] window) and selecting New > Class.

The package name should be guest.

Enter GuestDao as the class name - use exactly that case sensitive class name.

Click Finish to create the new DAO Spring component class.

Now replace the content of the new source file with the following code:

package guest;

Page 14: Eclipse Object Db Spring Mvc

 

import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;

import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;

import org.springframework.transaction.annotation.Transactional;

 

@Component

public class GuestDao {

// Injected database connection:

@PersistenceContext private EntityManager em;

 

// Stores a new guest:

@Transactional

public void persist(Guest guest) {

em.persist(guest);

}

 

// Retrieves all the guests:

public List<Guest> getAllGuests() {

TypedQuery<Guest> query = em.createQuery(

"SELECT g FROM Guest g ORDER BY g.id", Guest.class);

return query.getResultList();

}

}

The GuestDao Spring component class defines two methods:

persist - for storing a new Guest entity object in the database.

getAllGuests - for retrieving all the existing Guest objects from the database.

By using Spring components we can move some work from the application to the Spring

Framework. For instance, in this example the Spring container:

Manages the instantiation of the DAO component class and injects an instance of

the DAO component class into the controller, as shown in the next step.

Prepares an EntityManager automatically and injects it into the em field (because it

is annotated with the @PersistenceContext annotation).

Page 15: Eclipse Object Db Spring Mvc

Handles transactions automatically for methods that are annotated with

the @Transactionalannotation, saving the need to wrap every operation that

modifies the database (such as persist) by transaction begin and commit.

The next step is adding a Controller Class.

Step 4: Add a Controller ClassIn this step we will add a Spring Controller to manage guestbook web requests:

Open the [New Java Class] dialog box by right clicking the guest package node (in

the [Package Explorer] window) and selecting New > Class.

The package name should be guest.

Enter GuestController as the class name - use exactly that case sensitive class

name.

Click Finish to create the new Spring Controller class.

Page 16: Eclipse Object Db Spring Mvc

Now

replace the content of the new source file with the following code:

package guest;

 

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

 

Page 17: Eclipse Object Db Spring Mvc

@Controller

public class GuestController {

 

@Autowired

private GuestDao guestDao;

 

@RequestMapping(value="/guest")

public ModelAndView guestbook(HttpServletRequest request) {

// Handle a new guest (if any):

String name = request.getParameter("name");

if (name != null)

guestDao.persist(new Guest(name));

 

// Prepare the result view (guest.jsp):

return new ModelAndView("guest.jsp", "guestDao", guestDao);

}

}

The GuestController class is defined as a Spring managed web controller using

the @Controllerannotation. A GuestDao component is automatically constructed and

injected by Spring into the guestDao field (because it is annotated with

the @Autowired annotation). The guestbook method, which is attached to

the "/guest" web request uri (using the @RequestMapping annotation) uses

the GuestDao component to process the web request:

If a new guest has registered (using a JSP form that will be added in the next

tutorial step) - a new Guest entity object is constructed and stored in the database.

Processing is forwarded to a JSP page (which is presented in the next tutorial step)

that generates the HTML output. The JSP uses the GuestDao component to display

the existing Guest objects. The returned ModelAndView object defines a target JSP

("guest.jsp") and passes the GuestDao component to the JSP as a request attribute

(whose name is "guestDao" and its value is guestDao).

The next step is adding a JSP page that will serve as the application view and will

produce the guestbook page output.

Step 5: Add a JSP Page

Page 18: Eclipse Object Db Spring Mvc

In this step we will add the JSP that generates the guestbook HTML output:

Open the [New JSP File] dialog box by right clicking WEB-INF under

src/main/webapp (in the [Package Explorer] window), selecting New > Other... >

Web > JSP File and clicking Next.

The parent folder should be WEB-INF (to prevent direct access to the JSP bypassing

Spring).

Enter guest as the jsp file name - use exactly that case sensitive class name.

Click Finish to create the new JSP file.

Now replace the

content of the new jsp file with the following content:

Page 19: Eclipse Object Db Spring Mvc

<%@page contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-

1"%>

<%@page import="guest.*"%>

 

<jsp:useBean id="guestDao" type="guest.GuestDao" scope="request" />

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

 

<html>

<head>

<title>JPA Guestbook Web Application Tutorial</title>

</head>

 

<body>

<form method="POST" action="guest.html">

Name: <input type="text" name="name" />

<input type="submit" value="Add" />

</form>

 

<hr><ol>

<% for (Guest guest : guestDao.getAllGuests()) { %>

<li> <%= guest %> </li>

<% } %>

</ol><hr>

 

<iframe src="http://www.objectdb.com/pw.html?spring-eclipse"

frameborder="0" scrolling="no" width="100%" height="30"> </iframe>

</body>

</html>

The JSP generates the guestbook page output, which contains a simple form for signing

the guestbook, followed by a list of all the guests that have already signed (which are

retrieved from the request's guestDao attribute that is set by the controller in the

previous step).

The next step (and the last in this tutorial) is setting the Spring XML configuration.

Page 20: Eclipse Object Db Spring Mvc

Step 6: Set the Spring XMLTo integrate the Spring Framework into a web application we have to add the definition

of the Spring dispatcher servlet to the web.xml configuration file and to configure that

servlet using another xml configuration file.

Open the web.xml file (under src/main/webapp/WEB-INF) in a text editor (by right

clicking and selecting Open With > Text Editor or by double click and then moving to

the Source tab in the editor window). Use copy and paste to replace the default

content of the web.xml file with the following new content:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 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_2_5.xsd">

<servlet>

<servlet-name>spring</servlet-name>

<servlet-class>

org.springframework.web.servlet.DispatcherServlet

</servlet-class>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring</servlet-name>

<url-pattern>*.html</url-pattern>

</servlet-mapping>

<session-config>

<session-timeout>

30

</session-timeout>

</session-config>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

The settings above routes web requests with .html suffix to the Spring dispatcher

servlet.

Page 21: Eclipse Object Db Spring Mvc

Additional spring configuration is set in another XML file (whose name is derived from

the name of the Spring dispatcher servlet in web.xml). To generate that file:

Right click the WEB-INF node in the [Package Explorer] window, select New >

Other... > XML > XML File and click Next.

The parent folder should be Guestbook/src/main/webapp/WEB-INF.

Enter spring-servlet as the File Name.

Click Finish to create the XML file.

Now use copy

and paste to replace the content of the spring-servlet.xml file with the following new

content:

Page 22: Eclipse Object Db Spring Mvc

<?xml version="1.0" encoding="windows-1252"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-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/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 

<!-- Use @Component annotations for bean definitions -->

<context:component-scan base-package="guest"/>

 

<!-- Use @Controller annotations for MVC controller definitions -->

<mvc:annotation-driven />

 

<!-- Add JPA support -->

<bean id="emf" class=

"org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

<property name="loadTimeWeaver">

<bean class=

"org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>

</property>

</bean>

 

<!-- Add Transaction support -->

<bean id="myTxManager"

class="org.springframework.orm.jpa.JpaTransactionManager">

<property name="entityManagerFactory" ref="emf"/>

Page 23: Eclipse Object Db Spring Mvc

</bean>

 

<!-- Use @Transaction annotations for managing transactions -->

<tx:annotation-driven transaction-manager="myTxManager" />

 

<!-- View resolver -->

<bean class=

"org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/"/>

</bean>

 

</beans>

The settings above guides Spring to support annotations (for components, controllers

and transactions), to inject a JPA EntityManager, to manage transactions and to look for

JSP pages in the WEB-INF directory.

The next step (and the last in this tutorial) is running the Spring web application.

Step 7: Run the Spring Web AppAt this stage your project should have the following structure:

Page 24: Eclipse Object Db Spring Mvc

Use copy and paste to replace the content of the index.jsp file (which serves as the

default front page) with the following redirection to the controller path:

<jsp:forward page="guest.html" />

If Tomcat 6.0 is installed as a server in your Eclipse - you can run the application now by

right clicking the project node (in the [Project Explorer] window), selecting Run As >

Run on Server, selecting the Tomcat 6.0 server and clicking Finish.

Page 25: Eclipse Object Db Spring Mvc

To install a new server check the Manually define a new server radio button

(after Run As > Run on Server) and follow the instructions.

Alternatively you can run the web application using the embedded Maven Jetty plugin:

Right click the project node and select Run As > Maven Build...

Enter jetty:run in the Goals field and click Run.

Open your browser at http://localhost:8080/Guestbook/.

This Spring web application can also run with GlassFish 3.0.1, with a small change in

the persistence.xml file - instead of RESOURCE_LOCAL you will have to specify JTA.

To enable on the fly automatic enhancement (weaving) in Tomcat it has to be run with

ObjectDB as a Java agent (javaagent:objectdb.jar). When using GlassFish - on the fly

enhancement (weaving) is automatically enabled without specifying a Java agent.

Since we are using ObjectDB in embedded mode - the database file is created under the

Tomcat directory. You can stop the Tomcat server and open the database file

in ObjectDB Explorer:

Page 26: Eclipse Object Db Spring Mvc

This is the end of the tutorial. Learn more about ObjectDB and JPA by reading

the Manual.