Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2dfcfaf
Change the group and artifact id in pom
kdabir Oct 23, 2012
e6d96e6
Update the heading of the app in README
kdabir Oct 23, 2012
93d0cf4
Add a plain vanila jdbc impl for person service jut to remind oneself…
kdabir Oct 24, 2012
580ff0a
Add a spring jdbc based person service impl
kdabir Oct 24, 2012
c1b57b7
Uppercase the query
kdabir Oct 25, 2012
13a7836
change DispatcherServlet's path mapping to web app root /
kdabir Oct 25, 2012
4e2791a
add jetty plugin so that webapp can be run as mvn jetty:run
kdabir Oct 25, 2012
3d95288
add plugin version for jetty plugin because thats the right thing to do
kdabir Oct 25, 2012
2858f46
add groovy as dependency, just like any other library
kdabir Oct 25, 2012
d58f6cf
Add groovy-eclipse-compiler as dependency of maven compiler plugin an…
kdabir Oct 25, 2012
1b5a81f
Just for fun, rename the Person.java to Person.groovy
kdabir Oct 26, 2012
2fe643b
Remove noise from the entity class
kdabir Oct 26, 2012
06eb712
Add the groovy version of PersonService that uses groovy.sql.Sql to p…
kdabir Oct 26, 2012
602742b
Rename the controller to end with .groovy extension
kdabir Oct 26, 2012
7b15642
Controller feels little lighter once some of java baggage is dropped
kdabir Oct 26, 2012
4de92d2
Remove Schema Name from SQL queries. Schema names were written assumi…
kdabir Oct 26, 2012
c3044dd
Add stop port and key so that jetty can be stopped from command line.…
kdabir Oct 27, 2012
a690d3a
Add groovy favicon
kdabir Oct 27, 2012
6710ba5
Add Sitemesh for DRY layout management. remove Heroku specific Text a…
kdabir Oct 27, 2012
7208507
Update instruction of how to run the application
kdabir Oct 27, 2012
2d4f972
Example of producing xml and json from controller.
kdabir Oct 27, 2012
9abbae6
Produce nice html snippets with groovy MarkupBuilder
kdabir Oct 27, 2012
c8fa866
Use the JPA impl of service
kdabir Oct 27, 2012
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
# Spring MVC and Hibernate template application
# Groovy, Spring MVC and Hibernate sample application

This is a template for a web application that uses Spring MVC and Hibernate. The sample code is a simple CRUD page that manipulates records for a single model object.
This is a sample web application that uses Groovy, Spring MVC, Sitemesh and Hibernate.
The sample code is a simple CRUD page that manipulates records for a single model object.

## Running the application locally

First build with:

$mvn clean install

Then run it with:

$java -jar target/dependency/webapp-runner.jar target/*.war

$mvn jetty:run
41 changes: 39 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-hibernate-template</artifactId>
<groupId>com.github.kdabir</groupId>
<artifactId>groovy-springmvc-sample</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>

Expand All @@ -18,6 +18,12 @@
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- groovy -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
Expand All @@ -28,6 +34,11 @@
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
Expand Down Expand Up @@ -64,6 +75,12 @@
<version>7.0.27.1</version>
<scope>provided</scope>
</dependency>
<!-- sitemesh3 alpha-->
<dependency>
<groupId>org.sitemesh</groupId>
<artifactId>sitemesh</artifactId>
<version>3.0-alpha-1</version>
</dependency>
</dependencies>

<build>
Expand All @@ -73,12 +90,32 @@
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.7.0-01</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
</plugin>

<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.7.v20120910</version>
<configuration>
<scanIntervalSeconds>3</scanIntervalSeconds>
<stopKey>groovy-rocks</stopKey>
<stopPort>8123</stopPort>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/com/example/controller/PersonController.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.model.Person;
import com.example.service.PersonService
import org.springframework.web.bind.annotation.ResponseBody
import groovy.json.JsonBuilder
import groovy.xml.MarkupBuilder;

@Controller
@RequestMapping("/people")
class PersonController {

@Qualifier("personServiceImpl")
@Autowired
PersonService personService;

@RequestMapping("/")
String listPeople(Map<String, Object> map) {

map.person = new Person()
map.peopleList = personService.listPeople()

"people"
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
String addPerson(@ModelAttribute("person") Person person, BindingResult result) {

personService.addPerson(person)

"redirect:/people/"
}

@RequestMapping("/delete/{personId}")
String deletePerson(@PathVariable("personId") Integer personId) {

personService.removePerson(personId)

"redirect:/people/"
}

@RequestMapping(value = "/status/json", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public String getJson() {
def json = new JsonBuilder()
json.status {
date new Date()
peopleCount personService.listPeople().size()
}
json.toPrettyString()
}

@RequestMapping(value = "/xml", method = RequestMethod.GET, produces="application/xml")
@ResponseBody
public String getXml() {
StringWriter writer = new StringWriter()
def xml = new MarkupBuilder(writer)

xml.people {
personService.listPeople().each { Person p ->
person {
id p.id
firstName p.firstName
lastName p.lastName
}
}
}
writer.toString()
}

@RequestMapping(value = "/firstNames", method = RequestMethod.GET)
@ResponseBody
public String getFirstNamesHtml() {
StringWriter writer = new StringWriter()
def out = new MarkupBuilder(writer)

out.html {
head {
title "List of First Names"
}
body {
h1 "List of First Names"

ul {
personService.listPeople().each { Person p ->
li {
h3 p.firstName
}
}

}
}
}
writer.toString()
}

}
46 changes: 0 additions & 46 deletions src/main/java/com/example/controller/PersonController.java

This file was deleted.

18 changes: 18 additions & 0 deletions src/main/java/com/example/model/Person.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Person {

@Id
@GeneratedValue
Integer id

String firstName

String lastName

}
43 changes: 0 additions & 43 deletions src/main/java/com/example/model/Person.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.service;


import com.example.model.Person
import groovy.sql.Sql
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

import javax.sql.DataSource

@Service
public class PersonServiceGroovyJdbcImpl implements PersonService {

@Autowired
DataSource dataSource;

@Override
public void addPerson(Person person) {
new Sql(dataSource).execute('INSERT INTO PERSON(FIRSTNAME,LASTNAME) VALUES (?,?)',
[person.firstName, person.lastName])
}

@Override
public List<Person> listPeople() {
List<Person> people = []

new Sql(dataSource).eachRow("SELECT * FROM PERSON") {
people << new Person(id: it.id, firstName: it.firstName, lastName: it.lastName)
}

return people;
}

@Override
public void removePerson(Integer id) {
new Sql(dataSource).execute("DELETE FROM PERSON WHERE PERSON.ID=$id");
}
}
Loading