├── .gitignore ├── README.md ├── ember-example ├── books │ └── index.html ├── css │ ├── styles.css │ └── ui-lightness │ │ ├── images │ │ ├── ui-bg_diagonals-thick_18_b81900_40x40.png │ │ ├── ui-bg_diagonals-thick_20_666666_40x40.png │ │ ├── ui-bg_flat_10_000000_40x100.png │ │ ├── ui-bg_glass_100_f6f6f6_1x400.png │ │ ├── ui-bg_glass_100_fdf5ce_1x400.png │ │ ├── ui-bg_glass_65_ffffff_1x400.png │ │ ├── ui-bg_gloss-wave_35_f6a828_500x100.png │ │ ├── ui-bg_highlight-soft_100_eeeeee_1x100.png │ │ ├── ui-bg_highlight-soft_75_ffe45c_1x100.png │ │ ├── ui-icons_222222_256x240.png │ │ ├── ui-icons_228ef1_256x240.png │ │ ├── ui-icons_ef8c08_256x240.png │ │ ├── ui-icons_ffd27a_256x240.png │ │ └── ui-icons_ffffff_256x240.png │ │ └── jquery-ui-1.8.22.custom.css ├── images │ └── spinner.gif ├── index.html ├── js │ ├── app.js │ ├── ember-jquery-ui-mixin.js │ └── libs │ │ ├── ember-1.0.pre.min.js │ │ ├── handlebars-1.0.0.beta.6.js │ │ ├── jquery-1.8.0.min.js │ │ └── jquery-ui-1.9pre.js └── pom.xml ├── hello-spring-mvc ├── pom.xml └── src │ └── main │ ├── java │ └── examples │ │ └── controller │ │ └── HelloController.java │ └── webapp │ ├── WEB-INF │ ├── example-servlet.xml │ ├── jsp │ │ └── hello.jsp │ └── web.xml │ ├── css │ └── styles.css │ └── index.html ├── hibernate-example ├── README.md ├── create-db.sh ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── examples │ │ │ │ ├── controller │ │ │ │ ├── AbstractBookController.java │ │ │ │ ├── HtmlBookController.java │ │ │ │ ├── JsonBookController.java │ │ │ │ ├── Page.java │ │ │ │ ├── Paginator.java │ │ │ │ └── UploadFile.java │ │ │ │ ├── data │ │ │ │ ├── Book.java │ │ │ │ ├── Library.java │ │ │ │ └── MissingBookException.java │ │ │ │ └── filter │ │ │ │ └── AccessControlFilter.java │ │ ├── resources │ │ │ ├── application.properties │ │ │ ├── database.properties │ │ │ ├── hibernate.cfg.xml │ │ │ └── log4j.xml │ │ └── webapp │ │ │ ├── WEB-INF │ │ │ ├── applicationContext.xml │ │ │ ├── example-servlet.xml │ │ │ ├── jsp │ │ │ │ └── book │ │ │ │ │ ├── delete.jsp │ │ │ │ │ ├── edit.jsp │ │ │ │ │ ├── list.jsp │ │ │ │ │ ├── search.jsp │ │ │ │ │ ├── upload.jsp │ │ │ │ │ └── view.jsp │ │ │ └── web.xml │ │ │ └── index.html │ └── test │ │ └── resources │ │ ├── add-json.rcq │ │ ├── list-json.rcq │ │ ├── search-contains-json.rcq │ │ ├── search-prefix-json.rcq │ │ ├── update-json.rcq │ │ └── view-json.rcq ├── start-db.sh └── stop-db.sh ├── hibernate-search-example ├── README.md ├── create-db.sh ├── create-index.sh ├── load-db.sh ├── pom.xml ├── rebuild.sh ├── src │ ├── main │ │ ├── java │ │ │ └── examples │ │ │ │ ├── controller │ │ │ │ ├── AbstractBookController.java │ │ │ │ ├── HtmlBookController.java │ │ │ │ ├── JsonBookController.java │ │ │ │ ├── Page.java │ │ │ │ └── Paginator.java │ │ │ │ ├── data │ │ │ │ ├── Book.java │ │ │ │ ├── Indexer.java │ │ │ │ └── Library.java │ │ │ │ └── filter │ │ │ │ └── AccessControlFilter.java │ │ ├── resources │ │ │ ├── application.properties │ │ │ ├── applicationContext.xml │ │ │ ├── database.properties │ │ │ ├── hibernate.cfg.xml │ │ │ └── log4j.xml │ │ └── webapp │ │ │ ├── WEB-INF │ │ │ ├── example-servlet.xml │ │ │ ├── jsp │ │ │ │ └── book │ │ │ │ │ ├── delete.jsp │ │ │ │ │ ├── edit.jsp │ │ │ │ │ ├── list.jsp │ │ │ │ │ ├── search.jsp │ │ │ │ │ └── view.jsp │ │ │ └── web.xml │ │ │ └── index.html │ └── test │ │ └── scripts │ │ ├── book_crawler.py │ │ ├── books.sql │ │ ├── books.txt │ │ └── sqlgen.py ├── start-db.sh └── stop-db.sh ├── jquery-autocomplete-example ├── books │ └── index.html ├── css │ ├── styles.css │ └── ui-lightness │ │ ├── images │ │ ├── ui-bg_diagonals-thick_18_b81900_40x40.png │ │ ├── ui-bg_diagonals-thick_20_666666_40x40.png │ │ ├── ui-bg_flat_10_000000_40x100.png │ │ ├── ui-bg_glass_100_f6f6f6_1x400.png │ │ ├── ui-bg_glass_100_fdf5ce_1x400.png │ │ ├── ui-bg_glass_65_ffffff_1x400.png │ │ ├── ui-bg_gloss-wave_35_f6a828_500x100.png │ │ ├── ui-bg_highlight-soft_100_eeeeee_1x100.png │ │ ├── ui-bg_highlight-soft_75_ffe45c_1x100.png │ │ ├── ui-icons_222222_256x240.png │ │ ├── ui-icons_228ef1_256x240.png │ │ ├── ui-icons_ef8c08_256x240.png │ │ ├── ui-icons_ffd27a_256x240.png │ │ └── ui-icons_ffffff_256x240.png │ │ └── jquery-ui-1.8.22.custom.css ├── index.html ├── js │ ├── books.js │ └── lib │ │ ├── jquery-ui.js │ │ ├── jquery.js │ │ ├── jquery.serialize-json.js │ │ └── jquery.url.js └── pom.xml ├── jquery-json-example ├── pom.xml └── src │ └── main │ └── webapp │ ├── WEB-INF │ └── web.xml │ ├── books │ ├── addForm.html │ ├── editForm.html │ ├── index.html │ └── view.html │ ├── index.html │ └── js │ ├── books.js │ └── lib │ ├── jquery.js │ ├── jquery.serialize-json.js │ └── jquery.url.js ├── json-ajax-example ├── pom.xml └── src │ └── main │ └── webapp │ ├── WEB-INF │ └── web.xml │ ├── addForm.html │ ├── books.js │ ├── editForm.html │ ├── index.html │ ├── json.js │ └── view.html ├── json-binding-example ├── pom.xml └── src │ ├── main │ ├── java │ │ └── examples │ │ │ ├── controller │ │ │ └── BookController.java │ │ │ ├── data │ │ │ ├── Book.java │ │ │ └── Library.java │ │ │ ├── reader │ │ │ ├── HttpRequestReader.java │ │ │ └── json │ │ │ │ └── JsonHttpRequestReader.java │ │ │ └── view │ │ │ └── JsonMarshallingView.java │ └── webapp │ │ ├── WEB-INF │ │ ├── example-servlet.xml │ │ ├── jsp │ │ │ └── book │ │ │ │ ├── edit.jsp │ │ │ │ └── view.jsp │ │ └── web.xml │ │ └── index.html │ └── test │ └── resources │ ├── save-json.rcq │ └── view-json.rcq ├── json-view-example ├── pom.xml └── src │ ├── main │ ├── java │ │ └── examples │ │ │ ├── controller │ │ │ └── BookController.java │ │ │ ├── data │ │ │ ├── Book.java │ │ │ └── Library.java │ │ │ └── view │ │ │ └── JsonMarshallingView.java │ └── webapp │ │ ├── WEB-INF │ │ ├── example-servlet.xml │ │ ├── jsp │ │ │ └── book │ │ │ │ ├── edit.jsp │ │ │ │ └── view.jsp │ │ └── web.xml │ │ └── index.html │ └── test │ └── resources │ └── view-json.rcq ├── multi-action-controller-example ├── pom.xml └── src │ └── main │ ├── java │ └── examples │ │ ├── controller │ │ └── BookController.java │ │ └── data │ │ ├── Book.java │ │ └── Library.java │ └── webapp │ ├── WEB-INF │ ├── example-servlet.xml │ ├── jsp │ │ └── book │ │ │ ├── edit.jsp │ │ │ └── view.jsp │ └── web.xml │ ├── css │ └── styles.css │ └── index.html ├── pom.xml ├── rest-app-example ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── examples │ │ │ ├── controller │ │ │ ├── AbstractBookController.java │ │ │ ├── HtmlBookController.java │ │ │ └── JsonBookController.java │ │ │ ├── data │ │ │ ├── Book.java │ │ │ └── Library.java │ │ │ └── filter │ │ │ └── AccessControlFilter.java │ └── webapp │ │ ├── WEB-INF │ │ ├── example-servlet.xml │ │ ├── jsp │ │ │ └── book │ │ │ │ ├── edit.jsp │ │ │ │ ├── list.jsp │ │ │ │ └── view.jsp │ │ └── web.xml │ │ ├── css │ │ └── styles.css │ │ └── index.html │ ├── site │ ├── library_state_diagram.png │ └── library_state_diagram.zargo │ └── test │ └── resources │ ├── add-json.rcq │ ├── list-json.rcq │ ├── update-json.rcq │ └── view-json.rcq ├── spring3-example ├── pom.xml └── src │ └── main │ ├── java │ └── examples │ │ ├── controller │ │ └── BookController.java │ │ └── data │ │ ├── Book.java │ │ └── Library.java │ └── webapp │ ├── WEB-INF │ ├── example-servlet.xml │ ├── jsp │ │ └── book │ │ │ ├── edit.jsp │ │ │ └── view.jsp │ └── web.xml │ ├── css │ └── styles.css │ └── index.html ├── spring3-json-example ├── pom.xml └── src │ ├── main │ ├── java │ │ └── examples │ │ │ ├── controller │ │ │ ├── AbstractBookController.java │ │ │ ├── HtmlBookController.java │ │ │ └── JsonBookController.java │ │ │ └── data │ │ │ ├── Book.java │ │ │ └── Library.java │ └── webapp │ │ ├── WEB-INF │ │ ├── example-servlet.xml │ │ ├── jsp │ │ │ └── book │ │ │ │ ├── edit.jsp │ │ │ │ └── view.jsp │ │ └── web.xml │ │ └── index.html │ └── test │ └── resources │ ├── save-json.rcq │ └── view-json.rcq └── src └── site ├── library_http_state_diagram.zargo ├── library_state_diagram.png └── library_state_diagram.zargo /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | .settings 3 | .classpath 4 | .springBeans 5 | target 6 | .DS_Store 7 | MANIFEST.MF 8 | -------------------------------------------------------------------------------- /ember-example/books/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Book List 7 | 8 | 9 | 10 | 11 | 83 | 84 | 98 | 99 | 109 | 110 | 124 | 125 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | -------------------------------------------------------------------------------- /ember-example/css/styles.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | font-family: Verdana, Arial, sans-serif; 4 | } 5 | 6 | body, body .ui-widget, body .ui-widget .ui-widget { 7 | font-size: 90%; 8 | } 9 | 10 | #list-view table { 11 | margin: 1em 0; 12 | border-collapse: collapse; 13 | width: 100%; 14 | } 15 | 16 | #list-view .ui-widget-header th { 17 | padding: 2px 0px 2px 10px; 18 | } 19 | 20 | #list-view table td { 21 | border: 1px solid #EEE; 22 | padding: .6em 10px; 23 | text-align: left; 24 | } 25 | 26 | #list-view table.nav-links td { 27 | border: 0; 28 | } 29 | 30 | #list-view table.nav-links td.prev-links { 31 | text-align: left; 32 | width: 50%; 33 | } 34 | 35 | #list-view table.nav-links td.next-links { 36 | text-align: right; 37 | width: 50%; 38 | } 39 | 40 | #list-view .commands { 41 | width: 100px; 42 | } 43 | 44 | fieldset { 45 | padding: 0; 46 | border: 0; 47 | margin-top: 25px; 48 | } 49 | 50 | .field + .field { 51 | padding-top: 20px; 52 | } 53 | 54 | label { 55 | font-weight:bold; 56 | } 57 | 58 | input.text { 59 | width: 95%; 60 | } 61 | 62 | #clear-button .ui-button-text { 63 | font-size: .9em; 64 | padding: .1em 1em; 65 | } -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-bg_diagonals-thick_20_666666_40x40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-bg_diagonals-thick_20_666666_40x40.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-bg_flat_10_000000_40x100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-bg_flat_10_000000_40x100.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-bg_glass_100_f6f6f6_1x400.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-bg_glass_100_f6f6f6_1x400.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-bg_glass_100_fdf5ce_1x400.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-bg_glass_100_fdf5ce_1x400.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-bg_glass_65_ffffff_1x400.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-bg_glass_65_ffffff_1x400.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-bg_gloss-wave_35_f6a828_500x100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-bg_gloss-wave_35_f6a828_500x100.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-bg_highlight-soft_75_ffe45c_1x100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-bg_highlight-soft_75_ffe45c_1x100.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-icons_222222_256x240.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-icons_222222_256x240.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-icons_228ef1_256x240.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-icons_228ef1_256x240.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-icons_ef8c08_256x240.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-icons_ef8c08_256x240.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-icons_ffd27a_256x240.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-icons_ffd27a_256x240.png -------------------------------------------------------------------------------- /ember-example/css/ui-lightness/images/ui-icons_ffffff_256x240.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/css/ui-lightness/images/ui-icons_ffffff_256x240.png -------------------------------------------------------------------------------- /ember-example/images/spinner.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/images/spinner.gif -------------------------------------------------------------------------------- /ember-example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /ember-example/js/ember-jquery-ui-mixin.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/js/ember-jquery-ui-mixin.js -------------------------------------------------------------------------------- /ember-example/js/libs/jquery-1.8.0.min.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/js/libs/jquery-1.8.0.min.js -------------------------------------------------------------------------------- /ember-example/js/libs/jquery-ui-1.9pre.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/ember-example/js/libs/jquery-ui-1.9pre.js -------------------------------------------------------------------------------- /ember-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | examples.spring-mvc-examples 4 | ember-example 5 | 1.0.0-SNAPSHOT 6 | pom 7 | 8 | 9 | 10 | maven-assembly-plugin 11 | 2.3 12 | 13 | 14 | package 15 | 16 | single 17 | 18 | 19 | 20 | project 21 | 22 | 23 | 24 | 25 | 26 | 27 | maven-antrun-plugin 28 | 1.7 29 | 30 | 31 | package 32 | 33 | 34 | 37 | 38 | 39 | 40 | run 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /hello-spring-mvc/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | examples.spring-mvc-examples 5 | hello-spring-mvc 6 | 1.0.0-SNAPSHOT 7 | war 8 | 9 | 10 | 11 | maven-compiler-plugin 12 | 13 | 1.6 14 | 1.6 15 | 16 | 17 | 18 | 19 | 20 | 21 | javax.servlet 22 | servlet-api 23 | 2.5 24 | 25 | 26 | javax.servlet 27 | jstl 28 | 1.2 29 | 30 | 31 | org.springframework 32 | spring 33 | 1.2.7 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /hello-spring-mvc/src/main/java/examples/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | import javax.servlet.http.HttpServletResponse; 5 | 6 | import org.springframework.web.servlet.ModelAndView; 7 | import org.springframework.web.servlet.mvc.AbstractController; 8 | 9 | public class HelloController extends AbstractController { 10 | 11 | public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) 12 | throws Exception { 13 | 14 | ModelAndView mav = new ModelAndView("hello"); 15 | mav.addObject("message", "Hello World!"); 16 | return mav; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /hello-spring-mvc/src/main/webapp/WEB-INF/example-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | helloController 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /hello-spring-mvc/src/main/webapp/WEB-INF/jsp/hello.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | 9 | Hello 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /hello-spring-mvc/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | hello-spring-mvc 7 | 8 | 9 | example 10 | org.springframework.web.servlet.DispatcherServlet 11 | 1 12 | 13 | 14 | 15 | example 16 | *.form 17 | 18 | 19 | 20 | index.html 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /hello-spring-mvc/src/main/webapp/css/styles.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | font-family: Verdana, Arial, sans-serif; 4 | font-size: 1em; 5 | } 6 | -------------------------------------------------------------------------------- /hello-spring-mvc/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /hibernate-example/README.md: -------------------------------------------------------------------------------- 1 | Hibernate Example 2 | =================== 3 | 4 | Example HTML / JSON application with an HSQL database back-end using Hibernate and JPA annotations. 5 | 6 | * Run the start-db.sh script in the hibernate-example directory to start the HSQL database server in target/data/examples. 7 | * Run the create-db.sh script to create an empty database or drop and recreate the database. 8 | * Run the stop-db.sh script to stop the database server. 9 | -------------------------------------------------------------------------------- /hibernate-example/create-db.sh: -------------------------------------------------------------------------------- 1 | mvn -Pcreate-db hibernate3:hbm2ddl -------------------------------------------------------------------------------- /hibernate-example/src/main/java/examples/controller/AbstractBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.http.HttpStatus; 11 | import org.springframework.web.bind.annotation.ResponseStatus; 12 | 13 | import examples.data.Book; 14 | import examples.data.Library; 15 | import examples.data.MissingBookException; 16 | 17 | public abstract class AbstractBookController { 18 | 19 | private Library library; 20 | private Paginator paginator; 21 | 22 | @Autowired 23 | public void setLibrary(Library library) { 24 | this.library = library; 25 | } 26 | 27 | @Autowired 28 | public void setPaginator(Paginator paginator) { 29 | this.paginator = paginator; 30 | } 31 | 32 | @ResponseStatus(HttpStatus.NOT_FOUND) 33 | @SuppressWarnings("serial") 34 | public class NotFoundException extends Exception { 35 | } 36 | 37 | protected Map getBooks(Integer firstResult, Integer maxResults) { 38 | long total = library.countBooks(); 39 | Page page = paginator.getPage(firstResult, maxResults, total); 40 | return buildListModel( 41 | library.getBooks(page.getFirstResult(), page.getMaxResults()), 42 | page); 43 | } 44 | 45 | private Map buildListModel(List books, Page page) { 46 | Map model = new HashMap(); 47 | model.put("books", books); 48 | model.put("count", books.size()); 49 | model.put("total", page.getTotal()); 50 | model.put("firstResult", page.getFirstResult()); 51 | model.put("maxResults", page.getMaxResults()); 52 | model.put("nextResult", page.getNextResult()); 53 | model.put("prevResult", page.getPrevResult()); 54 | model.put("startResult", page.getStartResult()); 55 | model.put("lastResult", page.getLastResult()); 56 | return model; 57 | } 58 | 59 | protected Book getBook(int bookId) throws NotFoundException { 60 | Book book = library.getBook(bookId); 61 | if (book == null) { 62 | throw new NotFoundException(); 63 | } 64 | return book; 65 | } 66 | 67 | protected void saveBook(Book book) throws NotFoundException { 68 | try { 69 | library.saveBook(book); 70 | } catch (MissingBookException e) { 71 | throw new NotFoundException(); 72 | } 73 | } 74 | 75 | protected void deleteBook(int bookId) { 76 | library.deleteBook(bookId); 77 | } 78 | 79 | protected Map searchBooksByTitle(String title, Integer firstResult, Integer maxResults) { 80 | long total = library.countBooksByTitle(title); 81 | Page page = paginator.getPage(firstResult, maxResults, total); 82 | return buildListModel( 83 | library.searchBooksByTitle(title, page.getFirstResult(), page.getMaxResults()), 84 | page); 85 | } 86 | 87 | protected Map searchBooksByTitlePrefix(String title, Integer firstResult, Integer maxResults) { 88 | long total = library.countBooksByTitlePrefix(title); 89 | Page page = paginator.getPage(firstResult, maxResults, total); 90 | return buildListModel( 91 | library.searchBooksByTitlePrefix(title, page.getFirstResult(), page.getMaxResults()), 92 | page); 93 | } 94 | 95 | protected void importBooksAsCsv(InputStream inputStream) throws IOException { 96 | library.importBooksAsCsv(inputStream); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /hibernate-example/src/main/java/examples/controller/HtmlBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import java.io.IOException; 4 | 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.ModelAttribute; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestMethod; 11 | import org.springframework.web.bind.annotation.RequestParam; 12 | import org.springframework.web.bind.annotation.ResponseStatus; 13 | import org.springframework.web.servlet.ModelAndView; 14 | 15 | import examples.data.Book; 16 | 17 | @Controller 18 | @RequestMapping("/books") 19 | public class HtmlBookController extends AbstractBookController { 20 | 21 | @RequestMapping(value = "/", method = RequestMethod.GET) 22 | public ModelAndView list( 23 | @RequestParam(required = false) Integer firstResult, 24 | @RequestParam(required = false) Integer maxResults) { 25 | return new ModelAndView("book/list", getBooks(firstResult, maxResults)); 26 | } 27 | 28 | @RequestMapping(value = "/", method = RequestMethod.POST) 29 | public String save(@ModelAttribute("book") Book book) throws NotFoundException { 30 | saveBook(book); 31 | return "redirect:/library/books/"; 32 | } 33 | 34 | @RequestMapping("/addForm") 35 | public ModelAndView addForm() { 36 | return new ModelAndView("book/edit", "book", new Book()); 37 | } 38 | 39 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.GET) 40 | public ModelAndView view(@PathVariable int bookId) throws NotFoundException { 41 | return new ModelAndView("book/view", "book", getBook(bookId)); 42 | } 43 | 44 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.POST) 45 | public ModelAndView delete(@PathVariable int bookId, @RequestParam String submit, @RequestParam String action) 46 | throws NotFoundException, InvalidActionException { 47 | 48 | if (action.equals("confirmDelete")) { 49 | return new ModelAndView("book/delete", "book", getBook(bookId)); 50 | 51 | } else if (action.equals("delete")) { 52 | if (submit.equals("Cancel")) { 53 | return new ModelAndView("redirect:/library/books/book/" + bookId + "/editForm"); 54 | } else { 55 | deleteBook(bookId); 56 | return new ModelAndView("redirect:/library/books/"); 57 | } 58 | } 59 | throw new InvalidActionException(); 60 | } 61 | 62 | @ResponseStatus(HttpStatus.BAD_REQUEST) 63 | @SuppressWarnings("serial") 64 | public class InvalidActionException extends Exception { 65 | } 66 | 67 | @RequestMapping("/book/{bookId}/editForm") 68 | public ModelAndView editForm(@PathVariable int bookId) throws NotFoundException { 69 | return new ModelAndView("book/edit", "book", getBook(bookId)); 70 | } 71 | 72 | @RequestMapping("/searchForm") 73 | public ModelAndView searchForm() { 74 | return new ModelAndView("book/search"); 75 | } 76 | 77 | @RequestMapping("/search") 78 | public ModelAndView search( 79 | @RequestParam String title, 80 | @RequestParam(required = false) Integer firstResult, 81 | @RequestParam(required = false) Integer maxResults) { 82 | return new ModelAndView("book/list", searchBooksByTitle(title, firstResult, maxResults)); 83 | } 84 | 85 | @RequestMapping("/uploadForm") 86 | public ModelAndView uploadForm() { 87 | return new ModelAndView("book/upload", "uploadFile", new UploadFile()); 88 | } 89 | 90 | @RequestMapping(value = "upload", method = RequestMethod.POST) 91 | public String upload(UploadFile uploadFile) throws IOException { 92 | importBooksAsCsv(uploadFile.getInputStream()); 93 | return "redirect:/library/books/"; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /hibernate-example/src/main/java/examples/controller/JsonBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import java.util.Map; 4 | 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestBody; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestMethod; 11 | import org.springframework.web.bind.annotation.RequestParam; 12 | import org.springframework.web.bind.annotation.ResponseBody; 13 | import org.springframework.web.bind.annotation.ResponseStatus; 14 | 15 | import examples.data.Book; 16 | 17 | @Controller 18 | @RequestMapping("/books") 19 | public class JsonBookController extends AbstractBookController { 20 | 21 | @RequestMapping(value = "/", method = RequestMethod.GET, headers = "Accept=application/json") 22 | public @ResponseBody Map list( 23 | @RequestParam(required = false) Integer firstResult, 24 | @RequestParam(required = false) Integer maxResults) { 25 | return getBooks(firstResult, maxResults); 26 | } 27 | 28 | @RequestMapping(value = "/", method = RequestMethod.POST, headers = "Content-type=application/json") 29 | public @ResponseBody Book add(@RequestBody Book book) { 30 | book.setBookId(null); 31 | try { 32 | saveBook(book); 33 | } catch (NotFoundException e) { 34 | } 35 | return book; 36 | } 37 | 38 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.PUT, headers = "Content-type=application/json") 39 | public @ResponseBody Book update(@PathVariable int bookId, @RequestBody Book book) throws NotFoundException { 40 | book.setBookId(bookId); 41 | saveBook(book); 42 | return book; 43 | } 44 | 45 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.DELETE) 46 | @ResponseStatus(HttpStatus.NO_CONTENT) 47 | public void delete(@PathVariable int bookId) { 48 | deleteBook(bookId); 49 | } 50 | 51 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.GET, headers = "Accept=application/json") 52 | public @ResponseBody Book view(@PathVariable int bookId) throws NotFoundException { 53 | return getBook(bookId); 54 | } 55 | 56 | @RequestMapping(value = "/search", headers = "Accept=application/json") 57 | public @ResponseBody Map search( 58 | @RequestParam(required = false) String contains, 59 | @RequestParam(required = false) String prefix, 60 | @RequestParam(required = false) Integer firstResult, 61 | @RequestParam(required = false) Integer maxResults) { 62 | if (contains != null) { 63 | return searchBooksByTitle(contains, firstResult, maxResults); 64 | } else if (prefix != null) { 65 | return searchBooksByTitlePrefix(prefix, firstResult, maxResults); 66 | } 67 | return getBooks(firstResult, maxResults); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /hibernate-example/src/main/java/examples/controller/Page.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | public class Page { 4 | 5 | private final int firstResult; 6 | private final int maxResults; 7 | private final long total; 8 | private final Integer nextResult; 9 | private final Integer prevResult; 10 | private final Integer startResult; 11 | private final Integer lastResult; 12 | 13 | public Page(int firstResult, int maxResults, long total, Integer nextResult, Integer prevResult, 14 | Integer startResult, Integer lastResult) { 15 | 16 | this.firstResult = firstResult; 17 | this.maxResults = maxResults; 18 | this.total = total; 19 | this.nextResult = nextResult; 20 | this.prevResult = prevResult; 21 | this.startResult = startResult; 22 | this.lastResult = lastResult; 23 | } 24 | 25 | public int getFirstResult() { 26 | return firstResult; 27 | } 28 | 29 | public int getMaxResults() { 30 | return maxResults; 31 | } 32 | 33 | public long getTotal() { 34 | return total; 35 | } 36 | 37 | public Integer getNextResult() { 38 | return nextResult; 39 | } 40 | 41 | public Integer getPrevResult() { 42 | return prevResult; 43 | } 44 | 45 | public Integer getStartResult() { 46 | return startResult; 47 | } 48 | 49 | public Integer getLastResult() { 50 | return lastResult; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /hibernate-example/src/main/java/examples/controller/Paginator.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component 7 | public class Paginator { 8 | 9 | private int maxResults; 10 | 11 | @Value("${defaultMaxResults}") 12 | public void setMaxResults(int maxResults) { 13 | this.maxResults = maxResults; 14 | } 15 | 16 | public Page getPage(Integer firstResult, Integer maxResults, long total) { 17 | maxResults = getMaxResults(maxResults); 18 | firstResult = getFirstResult(firstResult, maxResults, total); 19 | return new Page( 20 | firstResult, 21 | maxResults, 22 | total, 23 | getNextResult(firstResult, maxResults, total), 24 | getPreviousResult(firstResult, maxResults), 25 | getStartResult(firstResult), 26 | getLastResult(firstResult, maxResults, total) 27 | ); 28 | } 29 | 30 | private int getFirstResult(Integer firstResult, int maxResults, long total) { 31 | return firstResult == null ? 0 : (firstResult < total ? firstResult : firstResult - maxResults); 32 | } 33 | 34 | private int getMaxResults(Integer maxResults) { 35 | return maxResults == null ? this.maxResults : Math.min(maxResults, this.maxResults); 36 | } 37 | 38 | private Integer getNextResult(int firstResult, int maxResults, long total) { 39 | int nextResult = firstResult + maxResults; 40 | return nextResult < total ? nextResult : null; 41 | } 42 | 43 | private Integer getPreviousResult(int firstResult, int maxResults) { 44 | int previousResult = firstResult - maxResults; 45 | return previousResult >= 0 ? previousResult : null; 46 | } 47 | 48 | private Integer getStartResult(long firstResult) { 49 | return firstResult == 0 ? null : 0; 50 | } 51 | 52 | private Integer getLastResult(int firstResult, int maxResults, long total) { 53 | if (total > maxResults) { 54 | int lastResult = (int) (total / maxResults) * maxResults; 55 | return (lastResult > firstResult) ? lastResult : null; 56 | } else { 57 | return null; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /hibernate-example/src/main/java/examples/controller/UploadFile.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | import org.springframework.web.multipart.commons.CommonsMultipartFile; 7 | 8 | public class UploadFile { 9 | 10 | private CommonsMultipartFile fileData; 11 | 12 | public CommonsMultipartFile getFileData() { 13 | return fileData; 14 | } 15 | 16 | public void setFileData(CommonsMultipartFile fileData) { 17 | this.fileData = fileData; 18 | } 19 | 20 | public InputStream getInputStream() throws IOException { 21 | return fileData.getInputStream(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /hibernate-example/src/main/java/examples/data/Book.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.Id; 6 | 7 | import org.apache.commons.lang.builder.EqualsBuilder; 8 | import org.apache.commons.lang.builder.HashCodeBuilder; 9 | import org.apache.commons.lang.builder.ToStringBuilder; 10 | 11 | @Entity 12 | public class Book { 13 | 14 | private String title; 15 | private String author; 16 | 17 | public Book() { 18 | } 19 | 20 | public Book(String title, String author) { 21 | this.title = title; 22 | this.author = author; 23 | } 24 | 25 | @Id 26 | @GeneratedValue 27 | private Integer bookId; 28 | 29 | public String getTitle() { 30 | return title; 31 | } 32 | 33 | public void setTitle(String title) { 34 | this.title = title; 35 | } 36 | 37 | public String getAuthor() { 38 | return author; 39 | } 40 | 41 | public void setAuthor(String author) { 42 | this.author = author; 43 | } 44 | 45 | public Integer getBookId() { 46 | return bookId; 47 | } 48 | 49 | public void setBookId(Integer bookId) { 50 | this.bookId = bookId; 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | return HashCodeBuilder.reflectionHashCode(this); 56 | } 57 | 58 | @Override 59 | public boolean equals(Object obj) { 60 | return EqualsBuilder.reflectionEquals(this, obj); 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | return ToStringBuilder.reflectionToString(this); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /hibernate-example/src/main/java/examples/data/MissingBookException.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | @SuppressWarnings("serial") 4 | public class MissingBookException extends Exception { 5 | 6 | public MissingBookException() { 7 | } 8 | 9 | public MissingBookException(String message, Throwable cause) { 10 | super(message, cause); 11 | } 12 | 13 | public MissingBookException(String message) { 14 | super(message); 15 | } 16 | 17 | public MissingBookException(Throwable cause) { 18 | super(cause); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /hibernate-example/src/main/java/examples/filter/AccessControlFilter.java: -------------------------------------------------------------------------------- 1 | package examples.filter; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.Filter; 6 | import javax.servlet.FilterChain; 7 | import javax.servlet.FilterConfig; 8 | import javax.servlet.ServletException; 9 | import javax.servlet.ServletRequest; 10 | import javax.servlet.ServletResponse; 11 | import javax.servlet.http.HttpServletResponse; 12 | 13 | /** 14 | * Implements Server-Side Access Control for Cross-Origin Resource Sharing. 15 | * 16 | * @see https://developer.mozilla.org/en-US/docs/Server-Side_Access_Control 17 | * @see http://dvcs.w3.org/hg/cors/raw-file/tip/Overview.html 18 | * 19 | * @author jfarr 20 | */ 21 | public class AccessControlFilter implements Filter { 22 | 23 | @Override 24 | public void init(FilterConfig filterConfig) throws ServletException { 25 | } 26 | 27 | @Override 28 | public void destroy() { 29 | } 30 | 31 | @Override 32 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 33 | throws IOException, ServletException { 34 | HttpServletResponse httpResponse = (HttpServletResponse) response; 35 | httpResponse.setHeader("Access-Control-Allow-Origin", "*"); 36 | httpResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); 37 | httpResponse.setHeader("Access-Control-Allow-Headers", "Content-Type"); 38 | chain.doFilter(request, response); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /hibernate-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | defaultMaxResults=20 2 | -------------------------------------------------------------------------------- /hibernate-example/src/main/resources/database.properties: -------------------------------------------------------------------------------- 1 | hibernate.dialect=org.hibernate.dialect.HSQLDialect 2 | hibernate.connection.driver_class=org.hsqldb.jdbcDriver 3 | hibernate.connection.url=jdbc:hsqldb:hsql://localhost 4 | hibernate.connection.username=sa 5 | hibernate.connection.password= 6 | -------------------------------------------------------------------------------- /hibernate-example/src/main/resources/hibernate.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | org.hibernate.dialect.HSQLDialect 10 | org.hibernate.cache.NoCacheProvider 11 | true 12 | update 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /hibernate-example/src/main/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /hibernate-example/src/main/webapp/WEB-INF/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | classpath:hibernate.cfg.xml 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /hibernate-example/src/main/webapp/WEB-INF/example-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /hibernate-example/src/main/webapp/WEB-INF/jsp/book/delete.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=utf-8" 2 | pageEncoding="utf-8"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | Confirm Delete Book 9 | 10 | 11 |

Confirm Delete Book

12 | 13 |

Are you sure you want to delete the book ''?

14 |
15 | 16 | 17 | 18 |
19 |
20 | request: 21 | 22 |
23 |
24 | model: 25 |
26 | book: 27 | 28 |
29 | 30 | 31 | -------------------------------------------------------------------------------- /hibernate-example/src/main/webapp/WEB-INF/jsp/book/edit.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=utf-8" 2 | pageEncoding="utf-8"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | <c:out value="${title}" /> 17 | 18 | 19 |

20 | book list search books add bookadd book upload books 21 |

22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 |
Title
Author
 
37 | 38 | " /> 39 | 40 |
41 | 42 |
43 | 44 | 45 | " /> 46 |
47 |
48 |
49 | request: 50 | 51 |
52 |
53 | model: 54 |
55 | book: 56 | 57 |
58 | 59 | 60 | -------------------------------------------------------------------------------- /hibernate-example/src/main/webapp/WEB-INF/jsp/book/search.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=utf-8" 2 | pageEncoding="utf-8"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Search Books 12 | 13 | 14 |

Search Books

15 | book list search books add book upload books 16 |
17 |
18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
Title
27 |
28 | 29 | 30 | -------------------------------------------------------------------------------- /hibernate-example/src/main/webapp/WEB-INF/jsp/book/upload.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=utf-8" 2 | pageEncoding="utf-8"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Upload Books 14 | 15 | 16 |

Upload Books

17 | book list search books ">add book upload books 18 |
19 |
20 | 21 | 22 |

23 | Input File 24 | 25 |

26 |

27 | 28 |

29 |
30 | 31 | 32 | -------------------------------------------------------------------------------- /hibernate-example/src/main/webapp/WEB-INF/jsp/book/view.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=utf-8" 2 | pageEncoding="utf-8"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | View Book 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |

View Book

17 |

book list search books add book

18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
Title
Author
28 |
29 | 30 |
31 |
32 | request: 33 | 34 |
35 |
36 | model: 37 |
38 | book: 39 | 40 |
41 | 42 | 43 | -------------------------------------------------------------------------------- /hibernate-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | hibernate-example 6 | 7 | AccessControlFilter 8 | examples.filter.AccessControlFilter 9 | 10 | 11 | AccessControlFilter 12 | /* 13 | 14 | 15 | org.springframework.web.context.ContextLoaderListener 16 | 17 | 18 | example 19 | org.springframework.web.servlet.DispatcherServlet 20 | 1 21 | 22 | 23 | example 24 | /library/* 25 | 26 | 27 | index.html 28 | 29 | 30 | -------------------------------------------------------------------------------- /hibernate-example/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /hibernate-example/src/test/resources/add-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/hibernate-example/library/books/POST{"title":"test","author":"added!"} 3 | -------------------------------------------------------------------------------- /hibernate-example/src/test/resources/list-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/hibernate-example/library/books/GET
3 | -------------------------------------------------------------------------------- /hibernate-example/src/test/resources/search-contains-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/hibernate-example/library/books/search?contains=testGET
3 | -------------------------------------------------------------------------------- /hibernate-example/src/test/resources/search-prefix-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/hibernate-example/library/books/search?prefix=testGET
3 | -------------------------------------------------------------------------------- /hibernate-example/src/test/resources/update-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/hibernate-example/library/books/book/1/PUT{"title":"test","author":"updated!","bookId":1} 3 | -------------------------------------------------------------------------------- /hibernate-example/src/test/resources/view-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/hibernate-example/library/books/book/1/GET
3 | -------------------------------------------------------------------------------- /hibernate-example/start-db.sh: -------------------------------------------------------------------------------- 1 | mvn exec:java -Dexec.mainClass="org.hsqldb.Server" -Dexec.args="-database.0 file:target/data/examples" 2 | -------------------------------------------------------------------------------- /hibernate-example/stop-db.sh: -------------------------------------------------------------------------------- 1 | mvn exec:java -Dexec.mainClass="org.hsqldb.cmdline.SqlTool" -Dexec.args='--sql "shutdown;" --inlineRc url=jdbc:hsqldb:hsql://localhost,user=sa,password=' 2 | -------------------------------------------------------------------------------- /hibernate-search-example/README.md: -------------------------------------------------------------------------------- 1 | Hibernate Example 2 | =================== 3 | 4 | Example HTML / JSON application using [Hibernate Search](http://www.hibernate.org/subprojects/search.html) and 5 | [Lucene](http://lucene.apache.org/) for automatic fast full-text search and indexing. 6 | 7 | * Run the start-db.sh script in the hibernate-search-example directory to start the HSQL database server in target/data/examples. 8 | * Run the create-db.sh script to create an empty database or drop and recreate the database. 9 | * Run the load-db.sh script to populate the database with test data. 10 | * Run the create-index.sh script to build a Lucene index from an existing database. 11 | * Run the stop-db.sh script to stop the database server. 12 | -------------------------------------------------------------------------------- /hibernate-search-example/create-db.sh: -------------------------------------------------------------------------------- 1 | mvn -Pcreate-db hibernate3:hbm2ddl -------------------------------------------------------------------------------- /hibernate-search-example/create-index.sh: -------------------------------------------------------------------------------- 1 | mvn exec:java -Dexec.mainClass="examples.data.Indexer" 2 | -------------------------------------------------------------------------------- /hibernate-search-example/load-db.sh: -------------------------------------------------------------------------------- 1 | mvn exec:java -Dexec.mainClass="org.hsqldb.cmdline.SqlTool" -Dexec.args='--inlineRc url=jdbc:hsqldb:hsql://localhost,user=sa,password=,charset=UTF-8 src/test/scripts/books.sql' 2 | -------------------------------------------------------------------------------- /hibernate-search-example/rebuild.sh: -------------------------------------------------------------------------------- 1 | ./stop-db.sh 2 | mvn clean install 3 | ./start-db.sh & 4 | ./create-db.sh 5 | ./load-db.sh 6 | ./create-index.sh 7 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/java/examples/controller/AbstractBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.http.HttpStatus; 9 | import org.springframework.web.bind.annotation.ResponseStatus; 10 | 11 | import examples.data.Book; 12 | import examples.data.Library; 13 | 14 | public abstract class AbstractBookController { 15 | 16 | private Library library; 17 | private Paginator paginator; 18 | 19 | @Autowired 20 | public void setLibrary(Library library) { 21 | this.library = library; 22 | } 23 | 24 | @Autowired 25 | public void setPaginator(Paginator paginator) { 26 | this.paginator = paginator; 27 | } 28 | 29 | @ResponseStatus(HttpStatus.NOT_FOUND) 30 | @SuppressWarnings("serial") 31 | public class NotFoundException extends Exception { 32 | } 33 | 34 | protected Map getBooks(Integer firstResult, Integer maxResults) { 35 | long total = library.countBooks(); 36 | Page page = paginator.getPage(firstResult, maxResults, total); 37 | return buildListModel( 38 | library.getBooks(page.getFirstResult(), page.getMaxResults()), 39 | page); 40 | } 41 | 42 | private Map buildListModel(List books, Page page) { 43 | Map model = new HashMap(); 44 | model.put("books", books); 45 | model.put("count", books.size()); 46 | model.put("total", page.getTotal()); 47 | model.put("firstResult", page.getFirstResult()); 48 | model.put("maxResults", page.getMaxResults()); 49 | model.put("nextResult", page.getNextResult()); 50 | model.put("prevResult", page.getPrevResult()); 51 | model.put("startResult", page.getStartResult()); 52 | model.put("lastResult", page.getLastResult()); 53 | return model; 54 | } 55 | 56 | protected Book getBook(int bookId) throws NotFoundException { 57 | Book book = library.getBook(bookId); 58 | if (book == null) { 59 | throw new NotFoundException(); 60 | } 61 | return book; 62 | } 63 | 64 | protected void saveBook(Book book) { 65 | library.saveBook(book); 66 | } 67 | 68 | protected void deleteBook(int bookId) { 69 | library.deleteBook(bookId); 70 | } 71 | 72 | protected Map searchBooksByTitle(String title, Integer firstResult, Integer maxResults) { 73 | long total = library.countBooksByTitle(title); 74 | Page page = paginator.getPage(firstResult, maxResults, total); 75 | return buildListModel( 76 | library.searchBooksByTitle(title, page.getFirstResult(), page.getMaxResults()), 77 | page); 78 | } 79 | 80 | protected Map searchBooksByTitlePrefix(String title, Integer firstResult, Integer maxResults) { 81 | long total = library.countBooksByTitlePrefix(title); 82 | Page page = paginator.getPage(firstResult, maxResults, total); 83 | return buildListModel( 84 | library.searchBooksByTitlePrefix(title, page.getFirstResult(), page.getMaxResults()), 85 | page); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/java/examples/controller/HtmlBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.web.bind.annotation.ModelAttribute; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | import org.springframework.web.bind.annotation.RequestParam; 10 | import org.springframework.web.bind.annotation.ResponseStatus; 11 | import org.springframework.web.servlet.ModelAndView; 12 | 13 | import examples.data.Book; 14 | 15 | @Controller 16 | @RequestMapping("/books") 17 | public class HtmlBookController extends AbstractBookController { 18 | 19 | @RequestMapping(value = "/", method = RequestMethod.GET) 20 | public ModelAndView list( 21 | @RequestParam(required = false) Integer firstResult, 22 | @RequestParam(required = false) Integer maxResults) { 23 | return new ModelAndView("book/list", getBooks(firstResult, maxResults)); 24 | } 25 | 26 | @RequestMapping(value = "/", method = RequestMethod.POST) 27 | public ModelAndView save(@ModelAttribute("book") Book book) { 28 | saveBook(book); 29 | return new ModelAndView("redirect:/library/books/"); 30 | } 31 | 32 | @RequestMapping("/addForm") 33 | public ModelAndView addForm() { 34 | return new ModelAndView("book/edit", "book", new Book()); 35 | } 36 | 37 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.GET) 38 | public ModelAndView view(@PathVariable int bookId) throws NotFoundException { 39 | return new ModelAndView("book/view", "book", getBook(bookId)); 40 | } 41 | 42 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.POST) 43 | public ModelAndView delete(@PathVariable int bookId, @RequestParam String submit, @RequestParam String action) 44 | throws NotFoundException, InvalidActionException { 45 | 46 | if (action.equals("confirmDelete")) { 47 | return new ModelAndView("book/delete", "book", getBook(bookId)); 48 | 49 | } else if (action.equals("delete")) { 50 | if (submit.equals("Cancel")) { 51 | return new ModelAndView("redirect:/library/books/book/" + bookId + "/editForm"); 52 | } else { 53 | deleteBook(bookId); 54 | return new ModelAndView("redirect:/library/books/"); 55 | } 56 | } 57 | throw new InvalidActionException(); 58 | } 59 | 60 | @ResponseStatus(HttpStatus.BAD_REQUEST) 61 | @SuppressWarnings("serial") 62 | public class InvalidActionException extends Exception { 63 | } 64 | 65 | @RequestMapping("/book/{bookId}/editForm") 66 | public ModelAndView editForm(@PathVariable int bookId) throws NotFoundException { 67 | return new ModelAndView("book/edit", "book", getBook(bookId)); 68 | } 69 | 70 | @RequestMapping("/searchForm") 71 | public ModelAndView searchForm() { 72 | return new ModelAndView("book/search"); 73 | } 74 | 75 | @RequestMapping("/search") 76 | public ModelAndView search( 77 | @RequestParam String title, 78 | @RequestParam(required = false) Integer firstResult, 79 | @RequestParam(required = false) Integer maxResults) { 80 | return new ModelAndView("book/list", searchBooksByTitle(title, firstResult, maxResults)); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/java/examples/controller/JsonBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import java.util.ArrayList; 4 | 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestBody; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestMethod; 11 | import org.springframework.web.bind.annotation.RequestParam; 12 | import org.springframework.web.bind.annotation.ResponseStatus; 13 | import org.springframework.web.servlet.ModelAndView; 14 | 15 | import examples.data.Book; 16 | 17 | @Controller 18 | @RequestMapping("/books") 19 | public class JsonBookController extends AbstractBookController { 20 | 21 | @RequestMapping(value = "/", method = RequestMethod.GET, headers = "Accept=application/json") 22 | public ModelAndView list( 23 | @RequestParam(required = false) Integer firstResult, 24 | @RequestParam(required = false) Integer maxResults) { 25 | return new ModelAndView("book/list-json", getBooks(firstResult, maxResults)); 26 | } 27 | 28 | @RequestMapping(value = "/", method = RequestMethod.POST, headers = "Content-type=application/json") 29 | public ModelAndView add(@RequestBody Book book) { 30 | saveBook(book); 31 | return new ModelAndView("book/view-json", "book", book); 32 | } 33 | 34 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.PUT, headers = "Content-type=application/json") 35 | public ModelAndView update(@PathVariable int bookId, @RequestBody Book book) { 36 | book.setBookId(bookId); 37 | saveBook(book); 38 | return new ModelAndView("book/view-json", "book", book); 39 | } 40 | 41 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.DELETE) 42 | @ResponseStatus(HttpStatus.NO_CONTENT) 43 | public void delete(@PathVariable int bookId) { 44 | deleteBook(bookId); 45 | } 46 | 47 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.GET, headers = "Accept=application/json") 48 | public ModelAndView view(@PathVariable int bookId) throws NotFoundException { 49 | return new ModelAndView("book/view-json", "book", getBook(bookId)); 50 | } 51 | 52 | @RequestMapping(value = "/search", headers = "Accept=application/json") 53 | public ModelAndView search( 54 | @RequestParam(required = false) String contains, 55 | @RequestParam(required = false) String prefix, 56 | @RequestParam(required = false) Integer firstResult, 57 | @RequestParam(required = false) Integer maxResults) { 58 | if (contains != null) { 59 | return new ModelAndView("book/list-json", searchBooksByTitle(contains, firstResult, maxResults)); 60 | } else if (prefix != null) { 61 | return new ModelAndView("book/list-json", searchBooksByTitlePrefix(prefix, firstResult, maxResults)); 62 | } 63 | return new ModelAndView("book/list-json", "books", new ArrayList()); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/java/examples/controller/Page.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | public class Page { 4 | 5 | private final int firstResult; 6 | private final int maxResults; 7 | private final long total; 8 | private final Integer nextResult; 9 | private final Integer prevResult; 10 | private final Integer startResult; 11 | private final Integer lastResult; 12 | 13 | public Page(int firstResult, int maxResults, long total, Integer nextResult, Integer prevResult, 14 | Integer startResult, Integer lastResult) { 15 | 16 | this.firstResult = firstResult; 17 | this.maxResults = maxResults; 18 | this.total = total; 19 | this.nextResult = nextResult; 20 | this.prevResult = prevResult; 21 | this.startResult = startResult; 22 | this.lastResult = lastResult; 23 | } 24 | 25 | public int getFirstResult() { 26 | return firstResult; 27 | } 28 | 29 | public int getMaxResults() { 30 | return maxResults; 31 | } 32 | 33 | public long getTotal() { 34 | return total; 35 | } 36 | 37 | public Integer getNextResult() { 38 | return nextResult; 39 | } 40 | 41 | public Integer getPrevResult() { 42 | return prevResult; 43 | } 44 | 45 | public Integer getStartResult() { 46 | return startResult; 47 | } 48 | 49 | public Integer getLastResult() { 50 | return lastResult; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/java/examples/controller/Paginator.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component 7 | public class Paginator { 8 | 9 | private int maxResults; 10 | 11 | @Value("${defaultMaxResults}") 12 | public void setMaxResults(int maxResults) { 13 | this.maxResults = maxResults; 14 | } 15 | 16 | public Page getPage(Integer firstResult, Integer maxResults, long total) { 17 | maxResults = getMaxResults(maxResults); 18 | firstResult = getFirstResult(firstResult, maxResults, total); 19 | return new Page( 20 | firstResult, 21 | maxResults, 22 | total, 23 | getNextResult(firstResult, maxResults, total), 24 | getPreviousResult(firstResult, maxResults), 25 | getStartResult(firstResult), 26 | getLastResult(firstResult, maxResults, total) 27 | ); 28 | } 29 | 30 | private int getFirstResult(Integer firstResult, int maxResults, long total) { 31 | return firstResult == null ? 0 : (firstResult < total ? firstResult : firstResult - maxResults); 32 | } 33 | 34 | private int getMaxResults(Integer maxResults) { 35 | return maxResults == null ? this.maxResults : Math.min(maxResults, this.maxResults); 36 | } 37 | 38 | private Integer getNextResult(int firstResult, int maxResults, long total) { 39 | int nextResult = firstResult + maxResults; 40 | return nextResult < total ? nextResult : null; 41 | } 42 | 43 | private Integer getPreviousResult(int firstResult, int maxResults) { 44 | int previousResult = firstResult - maxResults; 45 | return previousResult >= 0 ? previousResult : null; 46 | } 47 | 48 | private Integer getStartResult(long firstResult) { 49 | return firstResult == 0 ? null : 0; 50 | } 51 | 52 | private Integer getLastResult(int firstResult, int maxResults, long total) { 53 | long remaining = total % maxResults; 54 | int lastResult = (int) (remaining == 0 ? (total - maxResults) : (total - remaining)); 55 | return firstResult == lastResult ? null : lastResult; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/java/examples/data/Book.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.Id; 6 | 7 | import org.apache.commons.lang.builder.EqualsBuilder; 8 | import org.apache.commons.lang.builder.HashCodeBuilder; 9 | import org.apache.commons.lang.builder.ToStringBuilder; 10 | import org.hibernate.search.annotations.Field; 11 | import org.hibernate.search.annotations.Index; 12 | import org.hibernate.search.annotations.Indexed; 13 | import org.hibernate.search.annotations.Store; 14 | 15 | @Entity 16 | @Indexed 17 | public class Book { 18 | 19 | @Field(index=Index.TOKENIZED, store=Store.YES) 20 | private String title; 21 | @Field(index=Index.TOKENIZED, store=Store.YES) 22 | private String author; 23 | 24 | @Id 25 | @GeneratedValue 26 | private Integer bookId; 27 | 28 | public String getTitle() { 29 | return title; 30 | } 31 | 32 | @Field(name="title_fulltext", index=Index.UN_TOKENIZED, store=Store.YES) 33 | public String getLowerCaseTitle() { 34 | return title.toLowerCase(); 35 | } 36 | 37 | public void setTitle(String title) { 38 | this.title = title; 39 | } 40 | 41 | public String getAuthor() { 42 | return author; 43 | } 44 | 45 | public void setAuthor(String author) { 46 | this.author = author; 47 | } 48 | 49 | public Integer getBookId() { 50 | return bookId; 51 | } 52 | 53 | public void setBookId(Integer bookId) { 54 | this.bookId = bookId; 55 | } 56 | 57 | @Override 58 | public int hashCode() { 59 | return HashCodeBuilder.reflectionHashCode(this); 60 | } 61 | 62 | @Override 63 | public boolean equals(Object obj) { 64 | return EqualsBuilder.reflectionEquals(this, obj); 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return ToStringBuilder.reflectionToString(this); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/java/examples/data/Indexer.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | public class Indexer { 7 | 8 | public static void main(String[] args) throws InterruptedException { 9 | ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 10 | Library library = (Library) context.getBean("library"); 11 | library.index(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/java/examples/filter/AccessControlFilter.java: -------------------------------------------------------------------------------- 1 | package examples.filter; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.Filter; 6 | import javax.servlet.FilterChain; 7 | import javax.servlet.FilterConfig; 8 | import javax.servlet.ServletException; 9 | import javax.servlet.ServletRequest; 10 | import javax.servlet.ServletResponse; 11 | import javax.servlet.http.HttpServletResponse; 12 | 13 | /** 14 | * Implements Server-Side Access Control for Cross-Origin Resource Sharing. 15 | * 16 | * @see https://developer.mozilla.org/en-US/docs/Server-Side_Access_Control 17 | * @see http://dvcs.w3.org/hg/cors/raw-file/tip/Overview.html 18 | * 19 | * @author jfarr 20 | */ 21 | public class AccessControlFilter implements Filter { 22 | 23 | @Override 24 | public void init(FilterConfig filterConfig) throws ServletException { 25 | } 26 | 27 | @Override 28 | public void destroy() { 29 | } 30 | 31 | @Override 32 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 33 | throws IOException, ServletException { 34 | HttpServletResponse httpResponse = (HttpServletResponse) response; 35 | httpResponse.setHeader("Access-Control-Allow-Origin", "*"); 36 | httpResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); 37 | httpResponse.setHeader("Access-Control-Allow-Headers", "Content-Type"); 38 | chain.doFilter(request, response); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | defaultMaxResults=20 2 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | classpath:hibernate.cfg.xml 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/resources/database.properties: -------------------------------------------------------------------------------- 1 | hibernate.dialect=org.hibernate.dialect.HSQLDialect 2 | hibernate.connection.driver_class=org.hsqldb.jdbcDriver 3 | hibernate.connection.url=jdbc:hsqldb:hsql://localhost 4 | hibernate.connection.username=sa 5 | hibernate.connection.password= 6 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/resources/hibernate.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | org.hibernate.dialect.HSQLDialect 10 | org.hibernate.cache.NoCacheProvider 11 | true 12 | org.hibernate.search.store.FSDirectoryProvider 13 | ${project.build.directory}/lucene/indexes 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/webapp/WEB-INF/example-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/webapp/WEB-INF/jsp/book/delete.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=utf-8" 2 | pageEncoding="utf-8"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | Confirm Delete Book 9 | 10 | 11 |

Confirm Delete Book

12 | 13 |

Are you sure you want to delete the book ''?

14 |
15 | 16 | 17 | 18 |
19 |
20 | request: 21 | 22 |
23 |
24 | model: 25 |
26 | book: 27 | 28 |
29 | 30 | 31 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/webapp/WEB-INF/jsp/book/edit.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=utf-8" 2 | pageEncoding="utf-8"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | <c:out value="${title}" /> 16 | 17 | 18 |

19 | book list 20 |   21 | search books 22 |   23 | add book 24 |

25 |
26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
Title
Author
 
40 | 41 | " /> 42 | 43 |
44 | 45 |
46 | 47 | 48 | " /> 49 |
50 |
51 |
52 | request: 53 | 54 |
55 |
56 | model: 57 |
58 | book: 59 | 60 |
61 | 62 | 63 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/webapp/WEB-INF/jsp/book/search.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=utf-8" 2 | pageEncoding="utf-8"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | Search Books 9 | 10 | 11 |

Search Books

12 | ">book list 13 |   14 | ">search books 15 |   16 | ">add book 17 |
18 |
19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
Title
30 |
31 | 32 | 33 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/webapp/WEB-INF/jsp/book/view.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=utf-8" 2 | pageEncoding="utf-8"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | View Book 9 | 10 | 11 | 12 | 13 |

View Book

14 |

list

15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
Title
Author
25 |
26 | 27 |
28 |
29 | request: 30 | 31 |
32 |
33 | model: 34 |
35 | book: 36 | 37 |
38 | 39 | 40 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | hibernate-search-example 6 | 7 | contextConfigLocation 8 | classpath:applicationContext.xml 9 | 10 | 11 | AccessControlFilter 12 | examples.filter.AccessControlFilter 13 | 14 | 15 | AccessControlFilter 16 | /* 17 | 18 | 19 | org.springframework.web.context.ContextLoaderListener 20 | 21 | 22 | example 23 | org.springframework.web.servlet.DispatcherServlet 24 | 1 25 | 26 | 27 | example 28 | /library/* 29 | 30 | 31 | index.html 32 | 33 | -------------------------------------------------------------------------------- /hibernate-search-example/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /hibernate-search-example/src/test/scripts/book_crawler.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | import time 5 | import re 6 | import urllib2 7 | import csv 8 | import HTMLParser 9 | 10 | list_pattern = re.compile(r"
  • ") 11 | book_pattern = re.compile(r".*?(.*?).*?,\s+by\s+(.*?)[(,<]") 12 | next_link_pattern = re.compile(r'-- next>') 13 | 14 | start_url = 'http://onlinebooks.library.upenn.edu/webbin/book/browse?type=title' 15 | output_filename = 'books.txt' 16 | book_count = 0 17 | 18 | def main() : 19 | global book_count 20 | verbose = len(sys.argv) > 1 and sys.argv[1] == '-v' 21 | output_file = file(output_filename,'wb') 22 | writer = csv.writer(output_file) 23 | parser = HTMLParser.HTMLParser() 24 | next_link = start_url 25 | 26 | while next_link: 27 | print 'parsing', next_link 28 | print 29 | page = urllib2.urlopen(next_link).read() 30 | for match in list_pattern.finditer(page): 31 | listitem = match.group(0) 32 | book_match = book_pattern.match(listitem) 33 | if book_match: 34 | try: 35 | title = parser.unescape(book_match.group(1)).encode('utf8').strip() 36 | author = parser.unescape(book_match.group(2)).encode('utf8').strip() 37 | writer.writerow([title, author]) 38 | if verbose: 39 | print 'title:', title 40 | print 'author:', author 41 | print 42 | book_count += 1 43 | except UnicodeDecodeError: 44 | pass 45 | link_match = next_link_pattern.search(page) 46 | if link_match: 47 | next_link = link_match.group(1) 48 | else: 49 | next_link = None 50 | output_file.flush() 51 | time.sleep(2) 52 | 53 | print 'wrote', book_count, 'books' 54 | print 'done' 55 | 56 | if __name__ == "__main__": 57 | try: 58 | main() 59 | except KeyboardInterrupt: 60 | print 'wrote', book_count, 'books' 61 | -------------------------------------------------------------------------------- /hibernate-search-example/src/test/scripts/sqlgen.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import csv 4 | 5 | input_filename = 'books.txt' 6 | output_filename = 'books.sql' 7 | batch_size = 1000 8 | 9 | def main(): 10 | input_file = file(input_filename,'rb') 11 | reader = csv.reader(input_file) 12 | output_file = file(output_filename,'wb') 13 | rownum = 1 14 | for row in reader: 15 | output_file.write("insert into book (title, author) values ('%s', '%s');\n" % (row[0].replace("'","''"), row[1].replace("'","''"))) 16 | if rownum % batch_size == 0: 17 | output_file.write('commit;\n\n') 18 | rownum += 1 19 | output_file.write('commit;\n') 20 | 21 | if __name__ == "__main__": 22 | main() 23 | -------------------------------------------------------------------------------- /hibernate-search-example/start-db.sh: -------------------------------------------------------------------------------- 1 | mvn exec:java -Dexec.mainClass="org.hsqldb.Server" -Dexec.args="-database.0 file:target/data/examples" 2 | -------------------------------------------------------------------------------- /hibernate-search-example/stop-db.sh: -------------------------------------------------------------------------------- 1 | mvn exec:java -Dexec.mainClass="org.hsqldb.cmdline.SqlTool" -Dexec.args='--sql "shutdown;" --inlineRc url=jdbc:hsqldb:hsql://localhost,user=sa,password=' 2 | -------------------------------------------------------------------------------- /jquery-autocomplete-example/books/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 14 | Book List 15 | 16 | 17 |
    18 |

    Book List

    19 |
    20 | 21 | 22 |
    23 |

    add book

    24 |
    25 |
    26 | 27 | 28 | 29 |
    30 |

    31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 |
    TitleAuthor 
    edit delete
    47 | 48 | 49 | 50 | 54 | 58 | 59 | 60 | 61 | 62 |
    63 | book data: 64 | 65 |
    66 | 67 |
    68 |
    69 |
    70 |
    71 | 72 | 73 |
    74 |
    75 | 76 | 77 |
    78 |
    79 |
    80 |
    81 | 82 |
    83 |

    84 |
    85 | 86 |

    87 |

    88 |
    89 | 90 |

    91 |
    92 | 93 |
    94 |
    95 |
    96 |
    97 | 98 | 99 |
    100 |
    101 | 102 | 103 |
    104 |
    105 | 106 |
    107 |
    108 | 109 |
    Are you sure you want to delete the book ''?
    110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/styles.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | font-family: Verdana, Arial, sans-serif; 4 | } 5 | 6 | body, body .ui-widget, body .ui-widget .ui-widget { 7 | font-size: 90%; 8 | } 9 | 10 | #list-view table { 11 | margin: 1em 0; 12 | border-collapse: collapse; 13 | width: 100%; 14 | } 15 | 16 | #list-view .ui-widget-header { 17 | padding: 0px; 18 | } 19 | 20 | #list-view table td { 21 | border: 1px solid #EEE; 22 | padding: .6em 10px; 23 | text-align: left; 24 | } 25 | 26 | #list-view table.nav-links td { 27 | border: 0; 28 | } 29 | 30 | #list-view table.nav-links td.prev-links { 31 | text-align: left; 32 | width: 50%; 33 | } 34 | 35 | #list-view table.nav-links td.next-links { 36 | text-align: right; 37 | width: 50%; 38 | } 39 | 40 | #list-view .commands { 41 | width: 100px; 42 | } 43 | 44 | fieldset { 45 | padding: 0; 46 | border: 0; 47 | margin-top: 25px; 48 | } 49 | 50 | .field + .field { 51 | padding-top: 20px; 52 | } 53 | 54 | label { 55 | font-weight:bold; 56 | } 57 | 58 | input.text { 59 | width: 95%; 60 | } 61 | 62 | #clear .ui-button-text { 63 | font-size: .9em; 64 | padding: .1em 1em; 65 | } -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-bg_diagonals-thick_20_666666_40x40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-bg_diagonals-thick_20_666666_40x40.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-bg_flat_10_000000_40x100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-bg_flat_10_000000_40x100.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-bg_glass_100_f6f6f6_1x400.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-bg_glass_100_f6f6f6_1x400.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-bg_glass_100_fdf5ce_1x400.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-bg_glass_100_fdf5ce_1x400.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-bg_glass_65_ffffff_1x400.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-bg_glass_65_ffffff_1x400.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-bg_gloss-wave_35_f6a828_500x100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-bg_gloss-wave_35_f6a828_500x100.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-bg_highlight-soft_75_ffe45c_1x100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-bg_highlight-soft_75_ffe45c_1x100.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-icons_222222_256x240.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-icons_222222_256x240.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-icons_228ef1_256x240.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-icons_228ef1_256x240.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-icons_ef8c08_256x240.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-icons_ef8c08_256x240.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-icons_ffd27a_256x240.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-icons_ffd27a_256x240.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/css/ui-lightness/images/ui-icons_ffffff_256x240.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/jquery-autocomplete-example/css/ui-lightness/images/ui-icons_ffffff_256x240.png -------------------------------------------------------------------------------- /jquery-autocomplete-example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /jquery-autocomplete-example/js/lib/jquery.serialize-json.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Serialize form fields into JSON 4 | **/ 5 | 6 | (function($){ 7 | 8 | $.fn.serializeJSON = function(){ 9 | var json = {} 10 | var form = $(this); 11 | form.find('input, select').each(function(){ 12 | var val 13 | if (!this.name) return; 14 | 15 | if ('radio' === this.type) { 16 | if (json[this.name]) { return; } 17 | 18 | json[this.name] = this.checked ? this.value : ''; 19 | } else if ('checkbox' === this.type) { 20 | val = json[this.name]; 21 | 22 | if (!this.checked) { 23 | if (!val) { json[this.name] = ''; } 24 | } else { 25 | json[this.name] = 26 | typeof val === 'string' ? [val, this.value] : 27 | $.isArray(val) ? $.merge(val, [this.value]) : 28 | this.value; 29 | } 30 | } else { 31 | json[this.name] = this.value; 32 | } 33 | }) 34 | return json; 35 | } 36 | 37 | })(jQuery) -------------------------------------------------------------------------------- /jquery-autocomplete-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | examples.spring-mvc-examples 4 | jquery-autocomplete-example 5 | 1.0.0-SNAPSHOT 6 | pom 7 | 8 | 9 | 10 | maven-assembly-plugin 11 | 2.3 12 | 13 | 14 | package 15 | 16 | single 17 | 18 | 19 | 20 | project 21 | 22 | 23 | 24 | 25 | 26 | 27 | maven-antrun-plugin 28 | 1.7 29 | 30 | 31 | package 32 | 33 | 34 | 37 | 38 | 39 | 40 | run 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /jquery-json-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | examples.spring-mvc-examples 4 | jquery-json-example 5 | 1.0.0-SNAPSHOT 6 | war 7 | 8 | 9 | 10 | maven-compiler-plugin 11 | 12 | 1.6 13 | 1.6 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /jquery-json-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | jquery-json-example 6 | 7 | index.html 8 | 9 | 10 | -------------------------------------------------------------------------------- /jquery-json-example/src/main/webapp/books/addForm.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Add Book 6 | 7 | 8 | 13 | 14 | 15 |

    Add Book

    16 |
    17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
    Title
    Author
    30 |
    31 |
    32 | 33 | 34 | -------------------------------------------------------------------------------- /jquery-json-example/src/main/webapp/books/editForm.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Edit Book 6 | 7 | 8 | 9 | 10 | 16 | 17 | 18 |

    Edit Book

    19 |
    20 |
    21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
    Title
    Author
    34 |
    35 |
    36 | book: 37 | 38 | 39 | -------------------------------------------------------------------------------- /jquery-json-example/src/main/webapp/books/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 10 | Book List 11 | 12 | 13 |

    Book List

    14 |

    add book

    15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
    TitleAuthor 
    edit
    31 |
    32 | books: 33 | 34 | 35 | -------------------------------------------------------------------------------- /jquery-json-example/src/main/webapp/books/view.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | View Book 6 | 7 | 8 | 9 | 12 | 13 | 14 |

    View Book

    15 |

    list

    16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
    Title
    Author
    26 |
    27 | 28 |
    29 |
    30 | book: 31 | 32 | 33 | -------------------------------------------------------------------------------- /jquery-json-example/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /jquery-json-example/src/main/webapp/js/books.js: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * global variables 3 | */ 4 | 5 | var bookServiceUrl = 'http://localhost:8080/rest-app-example/library/books/'; 6 | 7 | /******************************************************************************* 8 | * index.html functions 9 | */ 10 | 11 | function onLoadList() { 12 | $.getJSON( 13 | bookServiceUrl, 14 | function(books) { 15 | if (books.length == 0) { 16 | $('#bookList').replaceWith('

    Library contains no books.

    '); 17 | } else { 18 | $.each(books, function(i, book) { 19 | var tr = $('#book_0').clone(); 20 | tr.attr('id', 'book_' + (i + 1)); 21 | var viewLink = tr.find('td.bookTitle').find('a'); 22 | viewLink.attr('href', viewLink.attr('href') + book.bookId); 23 | viewLink.html(book.title); 24 | tr.find('td.bookAuthor').html(book.author); 25 | var editLink = tr.find('td.editBook').find('a'); 26 | editLink.attr('href', editLink.attr('href') + book.bookId); 27 | $('#bookListContent').append(tr); 28 | }); 29 | $('#book_0').remove(); 30 | $('#bookData').replaceWith(JSON.stringify(books)); 31 | } 32 | }); 33 | } 34 | 35 | /******************************************************************************* 36 | * addForm.html functions 37 | */ 38 | 39 | function onSubmitAdd() { 40 | $.post( 41 | bookServiceUrl, 42 | $('form').serializeArray(), 43 | function() { window.location = 'index.html'; }, 44 | 'json'); 45 | return false; 46 | } 47 | 48 | /******************************************************************************* 49 | * editForm.html functions 50 | */ 51 | 52 | function onLoadEditForm() { 53 | var bookId = $.url().param('bookId'); 54 | if (bookId == null) { 55 | $('#editForm').replaceWith('

    Missing bookId.

    '); 56 | } else { 57 | $.getJSON( 58 | bookServiceUrl + 'book/' + bookId, 59 | function(book) { 60 | $('#title').attr('value', book.title); 61 | $('#author').attr('value', book.author); 62 | $('#bookId').attr('value', book.bookId); 63 | $('#bookData').replaceWith(JSON.stringify(book)); 64 | }); 65 | } 66 | } 67 | 68 | function onSubmitEdit() { 69 | var book = $('form').serializeJSON(); 70 | delete book.submit; 71 | $.ajax( 72 | bookServiceUrl + 'book/' + $('#bookId').attr('value'), { 73 | type: 'PUT', 74 | contentType: 'application/json', 75 | data: JSON.stringify(book), 76 | processData: false, 77 | success: function() { window.location = 'index.html'; } 78 | }); 79 | return false; 80 | } 81 | 82 | /******************************************************************************* 83 | * view.html functions 84 | */ 85 | 86 | function onLoadView() { 87 | var bookId = $.url().param('bookId'); 88 | if (bookId == null) { 89 | $('#bookView').replaceWith('

    Missing bookId.

    '); 90 | } else { 91 | $.getJSON( 92 | bookServiceUrl + 'book/' + bookId, 93 | function(book) { 94 | $('#title').html(book.title); 95 | $('#author').html(book.author); 96 | $('#editForm').attr('action', $('#editForm').attr('action') + book.bookId); 97 | $('#bookData').replaceWith(JSON.stringify(book)); 98 | }); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /jquery-json-example/src/main/webapp/js/lib/jquery.serialize-json.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Serialize form fields into JSON 4 | **/ 5 | 6 | (function($){ 7 | 8 | $.fn.serializeJSON = function(){ 9 | var json = {} 10 | var form = $(this); 11 | form.find('input, select').each(function(){ 12 | var val 13 | if (!this.name) return; 14 | 15 | if ('radio' === this.type) { 16 | if (json[this.name]) { return; } 17 | 18 | json[this.name] = this.checked ? this.value : ''; 19 | } else if ('checkbox' === this.type) { 20 | val = json[this.name]; 21 | 22 | if (!this.checked) { 23 | if (!val) { json[this.name] = ''; } 24 | } else { 25 | json[this.name] = 26 | typeof val === 'string' ? [val, this.value] : 27 | $.isArray(val) ? $.merge(val, [this.value]) : 28 | this.value; 29 | } 30 | } else { 31 | json[this.name] = this.value; 32 | } 33 | }) 34 | return json; 35 | } 36 | 37 | })(jQuery) -------------------------------------------------------------------------------- /json-ajax-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | examples.spring-mvc-examples 4 | json-ajax-example 5 | 1.0.0-SNAPSHOT 6 | war 7 | 8 | 9 | 10 | maven-compiler-plugin 11 | 12 | 1.6 13 | 1.6 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /json-ajax-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | json-ajax-example 6 | 7 | index.html 8 | 9 | 10 | -------------------------------------------------------------------------------- /json-ajax-example/src/main/webapp/addForm.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Add Book 6 | 7 | 8 | 9 | 10 |

    Add Book

    11 |
    12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
    Title
    Author
    25 |
    26 |
    27 | 28 | 29 | -------------------------------------------------------------------------------- /json-ajax-example/src/main/webapp/editForm.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Edit Book 6 | 7 | 8 | 9 | 10 |

    Edit Book

    11 |
    12 | 13 | 14 | -------------------------------------------------------------------------------- /json-ajax-example/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Book List 8 | 9 | 10 |

    Book List

    11 |

    add book

    12 |
    13 | 14 | 15 | -------------------------------------------------------------------------------- /json-ajax-example/src/main/webapp/view.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | View Book 6 | 7 | 8 | 9 | 10 |

    View Book

    11 |

    list

    12 |
    13 | 14 | 15 | -------------------------------------------------------------------------------- /json-binding-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | examples.spring-mvc-examples 5 | json-binding-example 6 | 1.0.0-SNAPSHOT 7 | war 8 | 9 | 10 | 11 | maven-compiler-plugin 12 | 13 | 1.6 14 | 1.6 15 | 16 | 17 | 18 | 19 | 20 | 21 | javax.servlet 22 | servlet-api 23 | 2.5 24 | 25 | 26 | javax.servlet 27 | jstl 28 | 1.2 29 | 30 | 31 | org.springframework 32 | spring 33 | 1.2.7 34 | 35 | 36 | org.codehaus.jackson 37 | jackson-core-asl 38 | 1.9.7 39 | 40 | 41 | org.codehaus.jackson 42 | jackson-mapper-asl 43 | 1.9.7 44 | 45 | 46 | commons-lang 47 | commons-lang 48 | 2.6 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /json-binding-example/src/main/java/examples/controller/BookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | import javax.servlet.http.HttpServletResponse; 5 | 6 | import org.apache.commons.lang.Validate; 7 | import org.springframework.web.servlet.ModelAndView; 8 | import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 9 | 10 | import examples.data.Book; 11 | import examples.data.Library; 12 | import examples.reader.HttpRequestReader; 13 | 14 | public class BookController extends MultiActionController { 15 | 16 | private Library library; 17 | private HttpRequestReader jsonRequestReader; 18 | 19 | public void setLibrary(Library library) { 20 | this.library = library; 21 | } 22 | 23 | public void setJsonRequestReader(HttpRequestReader jsonRequestReader) { 24 | this.jsonRequestReader = jsonRequestReader; 25 | } 26 | 27 | public ModelAndView addForm(HttpServletRequest request, HttpServletResponse response) { 28 | return new ModelAndView("book/edit", "book", new Book()); 29 | } 30 | 31 | public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception { 32 | Book book = bind(request); 33 | library.saveBook(book); 34 | return new ModelAndView("redirect:book.form?action=view", "bookId", book.getBookId()); 35 | } 36 | 37 | private Book bind(HttpServletRequest request) throws Exception { 38 | Book book = new Book(); 39 | bind(request, book); 40 | return book; 41 | } 42 | 43 | public ModelAndView saveJson(HttpServletRequest request, HttpServletResponse response) throws Exception { 44 | Book book = bindJson(request); 45 | library.saveBook(book); 46 | return new ModelAndView("redirect:book.form?action=view", "bookId", book.getBookId()); 47 | } 48 | 49 | private Book bindJson(HttpServletRequest request) throws Exception { 50 | return jsonRequestReader.read(Book.class, request); 51 | } 52 | 53 | public ModelAndView view(HttpServletRequest request, HttpServletResponse response) { 54 | return new ModelAndView("book/view", "book", getBook(request)); 55 | } 56 | 57 | public ModelAndView viewJson(HttpServletRequest request, HttpServletResponse response) { 58 | return new ModelAndView("book/json-view", "book", getBook(request)); 59 | } 60 | 61 | public ModelAndView editForm(HttpServletRequest request, HttpServletResponse response) { 62 | return new ModelAndView("book/edit", "book", getBook(request)); 63 | } 64 | 65 | private Book getBook(HttpServletRequest request) { 66 | String bookId = request.getParameter("bookId"); 67 | Validate.notNull(bookId, "missing bookId parameter"); 68 | return library.getBook(Integer.parseInt(bookId)); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /json-binding-example/src/main/java/examples/data/Book.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.apache.commons.lang.builder.ToStringBuilder; 6 | 7 | public class Book { 8 | 9 | private String title; 10 | private String author; 11 | 12 | private Integer bookId; 13 | 14 | public String getTitle() { 15 | return title; 16 | } 17 | 18 | public void setTitle(String title) { 19 | this.title = title; 20 | } 21 | 22 | public String getAuthor() { 23 | return author; 24 | } 25 | 26 | public void setAuthor(String author) { 27 | this.author = author; 28 | } 29 | 30 | public Integer getBookId() { 31 | return bookId; 32 | } 33 | 34 | public void setBookId(Integer bookId) { 35 | this.bookId = bookId; 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | return HashCodeBuilder.reflectionHashCode(this); 41 | } 42 | 43 | @Override 44 | public boolean equals(Object obj) { 45 | return EqualsBuilder.reflectionEquals(this, obj); 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return ToStringBuilder.reflectionToString(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /json-binding-example/src/main/java/examples/data/Library.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class Library { 7 | 8 | private int nextBookId = 1; 9 | private Map books = new HashMap(); 10 | 11 | public Book getBook(int bookId) { 12 | return books.get(bookId); 13 | } 14 | 15 | public void saveBook(Book book) { 16 | Integer bookId = book.getBookId(); 17 | if (bookId == null) { 18 | bookId = nextBookId++; 19 | book.setBookId(bookId); 20 | } 21 | books.put(bookId, book); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /json-binding-example/src/main/java/examples/reader/HttpRequestReader.java: -------------------------------------------------------------------------------- 1 | package examples.reader; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | public interface HttpRequestReader { 6 | 7 | T read(Class clazz, HttpServletRequest request) throws Exception; 8 | } 9 | -------------------------------------------------------------------------------- /json-binding-example/src/main/java/examples/reader/json/JsonHttpRequestReader.java: -------------------------------------------------------------------------------- 1 | package examples.reader.json; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import org.codehaus.jackson.map.ObjectMapper; 6 | 7 | import examples.reader.HttpRequestReader; 8 | 9 | public class JsonHttpRequestReader implements HttpRequestReader { 10 | 11 | private ObjectMapper objectMapper; 12 | 13 | public void setObjectMapper(ObjectMapper objectMapper) { 14 | this.objectMapper = objectMapper; 15 | } 16 | 17 | @Override 18 | public T read(Class clazz, HttpServletRequest request) throws Exception { 19 | return objectMapper.readValue(request.getInputStream(), objectMapper.getTypeFactory().constructType(clazz)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /json-binding-example/src/main/java/examples/view/JsonMarshallingView.java: -------------------------------------------------------------------------------- 1 | package examples.view; 2 | 3 | import java.util.Map; 4 | 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpServletResponse; 7 | 8 | import org.codehaus.jackson.map.ObjectMapper; 9 | import org.springframework.web.servlet.View; 10 | 11 | @SuppressWarnings("rawtypes") 12 | public class JsonMarshallingView implements View { 13 | 14 | private static final String CONTENT_TYPE = "application/json; charset=UTF-8"; 15 | 16 | private ObjectMapper objectMapper; 17 | private String modelName; 18 | 19 | public void setObjectMapper(ObjectMapper objectMapper) { 20 | this.objectMapper = objectMapper; 21 | } 22 | 23 | public void setModelName(String modelName) { 24 | this.modelName = modelName; 25 | } 26 | 27 | @Override 28 | public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { 29 | response.setContentType(CONTENT_TYPE); 30 | objectMapper.writeValue(response.getOutputStream(), model.get(modelName)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /json-binding-example/src/main/webapp/WEB-INF/example-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | 13 | bookController 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 32 | 33 | 34 | 35 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /json-binding-example/src/main/webapp/WEB-INF/jsp/book/edit.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | Add Book 11 | 12 | 13 | Edit Book 14 | 15 | 16 | 17 | 18 | 19 | 20 |

    Add Book

    21 |
    22 | 23 |

    Edit Book

    24 |
    25 |
    26 |
    27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
    Title
    Author
    40 | 41 | " /> 42 | 43 | 44 |
    45 |
    46 | request: 47 | 48 |
    49 | model: 50 | 51 |
    52 | 53 | 54 | -------------------------------------------------------------------------------- /json-binding-example/src/main/webapp/WEB-INF/jsp/book/view.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | View Book 9 | 10 | 11 |

    View Book

    12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
    Title
    Author
    22 |
    23 | 24 | " /> 25 | 26 |
    27 |
    28 | request: 29 | 30 |
    31 | model: 32 | 33 |
    34 | 35 | 36 | -------------------------------------------------------------------------------- /json-binding-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | json-binding-example 6 | 7 | example 8 | org.springframework.web.servlet.DispatcherServlet 9 | 1 10 | 11 | 12 | example 13 | *.form 14 | 15 | 16 | index.html 17 | 18 | -------------------------------------------------------------------------------- /json-binding-example/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /json-binding-example/src/test/resources/save-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/json-binding-example/book.form?action=saveJsonPOST{"title":"test","author":"test2","bookId":1} 3 | -------------------------------------------------------------------------------- /json-binding-example/src/test/resources/view-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/json-binding-example/book.form?action=viewJson&bookId=1GET 3 | -------------------------------------------------------------------------------- /json-view-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | examples.spring-mvc-examples 5 | json-view-example 6 | 1.0.0-SNAPSHOT 7 | war 8 | 9 | 10 | 11 | maven-compiler-plugin 12 | 13 | 1.6 14 | 1.6 15 | 16 | 17 | 18 | 19 | 20 | 21 | javax.servlet 22 | servlet-api 23 | 2.5 24 | 25 | 26 | javax.servlet 27 | jstl 28 | 1.2 29 | 30 | 31 | org.springframework 32 | spring 33 | 1.2.7 34 | 35 | 36 | org.codehaus.jackson 37 | jackson-core-asl 38 | 1.9.7 39 | 40 | 41 | org.codehaus.jackson 42 | jackson-mapper-asl 43 | 1.9.7 44 | 45 | 46 | commons-lang 47 | commons-lang 48 | 2.6 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /json-view-example/src/main/java/examples/controller/BookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | import javax.servlet.http.HttpServletResponse; 5 | 6 | import org.apache.commons.lang.Validate; 7 | import org.springframework.web.servlet.ModelAndView; 8 | import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 9 | 10 | import examples.data.Book; 11 | import examples.data.Library; 12 | 13 | public class BookController extends MultiActionController { 14 | 15 | private Library library; 16 | 17 | public void setLibrary(Library library) { 18 | this.library = library; 19 | } 20 | 21 | public ModelAndView addForm(HttpServletRequest request, HttpServletResponse response) { 22 | return new ModelAndView("book/edit", "book", new Book()); 23 | } 24 | 25 | public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception { 26 | Book book = bind(request); 27 | library.saveBook(book); 28 | return new ModelAndView("redirect:book.form?action=view", "bookId", book.getBookId()); 29 | } 30 | 31 | private Book bind(HttpServletRequest request) throws Exception { 32 | Book book = new Book(); 33 | bind(request, book); 34 | return book; 35 | } 36 | 37 | public ModelAndView view(HttpServletRequest request, HttpServletResponse response) { 38 | return new ModelAndView("book/view", "book", getBook(request)); 39 | } 40 | 41 | public ModelAndView viewJson(HttpServletRequest request, HttpServletResponse response) { 42 | return new ModelAndView("book/json-view", "book", getBook(request)); 43 | } 44 | 45 | public ModelAndView editForm(HttpServletRequest request, HttpServletResponse response) { 46 | return new ModelAndView("book/edit", "book", getBook(request)); 47 | } 48 | 49 | private Book getBook(HttpServletRequest request) { 50 | String bookId = request.getParameter("bookId"); 51 | Validate.notNull(bookId, "missing bookId parameter"); 52 | return library.getBook(Integer.parseInt(bookId)); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /json-view-example/src/main/java/examples/data/Book.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.apache.commons.lang.builder.ToStringBuilder; 6 | 7 | public class Book { 8 | 9 | private String title; 10 | private String author; 11 | 12 | private Integer bookId; 13 | 14 | public String getTitle() { 15 | return title; 16 | } 17 | 18 | public void setTitle(String title) { 19 | this.title = title; 20 | } 21 | 22 | public String getAuthor() { 23 | return author; 24 | } 25 | 26 | public void setAuthor(String author) { 27 | this.author = author; 28 | } 29 | 30 | public Integer getBookId() { 31 | return bookId; 32 | } 33 | 34 | public void setBookId(Integer bookId) { 35 | this.bookId = bookId; 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | return HashCodeBuilder.reflectionHashCode(this); 41 | } 42 | 43 | @Override 44 | public boolean equals(Object obj) { 45 | return EqualsBuilder.reflectionEquals(this, obj); 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return ToStringBuilder.reflectionToString(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /json-view-example/src/main/java/examples/data/Library.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class Library { 7 | 8 | private int nextBookId = 1; 9 | private Map books = new HashMap(); 10 | 11 | public Book getBook(int bookId) { 12 | return books.get(bookId); 13 | } 14 | 15 | public void saveBook(Book book) { 16 | Integer bookId = book.getBookId(); 17 | if (bookId == null) { 18 | bookId = nextBookId++; 19 | book.setBookId(bookId); 20 | } 21 | books.put(bookId, book); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /json-view-example/src/main/java/examples/view/JsonMarshallingView.java: -------------------------------------------------------------------------------- 1 | package examples.view; 2 | 3 | import java.util.Map; 4 | 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpServletResponse; 7 | 8 | import org.codehaus.jackson.map.ObjectMapper; 9 | import org.springframework.web.servlet.View; 10 | 11 | @SuppressWarnings("rawtypes") 12 | public class JsonMarshallingView implements View { 13 | 14 | private static final String CONTENT_TYPE = "application/json; charset=UTF-8"; 15 | 16 | private ObjectMapper objectMapper; 17 | private String modelName; 18 | 19 | public void setObjectMapper(ObjectMapper objectMapper) { 20 | this.objectMapper = objectMapper; 21 | } 22 | 23 | public void setModelName(String modelName) { 24 | this.modelName = modelName; 25 | } 26 | 27 | @Override 28 | public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { 29 | response.setContentType(CONTENT_TYPE); 30 | objectMapper.writeValue(response.getOutputStream(), model.get(modelName)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /json-view-example/src/main/webapp/WEB-INF/example-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | 13 | bookController 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 32 | 33 | 34 | 35 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /json-view-example/src/main/webapp/WEB-INF/jsp/book/edit.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | Add Book 11 | 12 | 13 | Edit Book 14 | 15 | 16 | 17 | 18 | 19 | 20 |

    Add Book

    21 |
    22 | 23 |

    Edit Book

    24 |
    25 |
    26 |
    27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
    Title
    Author
    40 | 41 | " /> 42 | 43 | 44 |
    45 |
    46 | request: 47 | 48 |
    49 | model: 50 | 51 |
    52 | 53 | 54 | -------------------------------------------------------------------------------- /json-view-example/src/main/webapp/WEB-INF/jsp/book/view.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | View Book 9 | 10 | 11 |

    View Book

    12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
    Title
    Author
    22 |
    23 | 24 | " /> 25 | 26 |
    27 |
    28 | request: 29 | 30 |
    31 | model: 32 | 33 |
    34 | 35 | 36 | -------------------------------------------------------------------------------- /json-view-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | json-view-example 6 | 7 | example 8 | org.springframework.web.servlet.DispatcherServlet 9 | 1 10 | 11 | 12 | example 13 | *.form 14 | 15 | 16 | index.html 17 | 18 | -------------------------------------------------------------------------------- /json-view-example/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /json-view-example/src/test/resources/view-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/json-binding-example/book.form?action=viewJson&bookId=1GET 3 | -------------------------------------------------------------------------------- /multi-action-controller-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | examples.spring-mvc-examples 5 | example-multi-action-controller 6 | 1.0.0-SNAPSHOT 7 | war 8 | 9 | 10 | 11 | maven-compiler-plugin 12 | 13 | 1.6 14 | 1.6 15 | 16 | 17 | 18 | 19 | 20 | 21 | javax.servlet 22 | servlet-api 23 | 2.5 24 | 25 | 26 | javax.servlet 27 | jstl 28 | 1.2 29 | 30 | 31 | org.springframework 32 | spring 33 | 1.2.7 34 | 35 | 36 | commons-lang 37 | commons-lang 38 | 2.6 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /multi-action-controller-example/src/main/java/examples/controller/BookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | import javax.servlet.http.HttpServletResponse; 5 | 6 | import org.apache.commons.lang.Validate; 7 | import org.springframework.web.servlet.ModelAndView; 8 | import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 9 | 10 | import examples.data.Book; 11 | import examples.data.Library; 12 | 13 | public class BookController extends MultiActionController { 14 | 15 | private Library library; 16 | 17 | public void setLibrary(Library library) { 18 | this.library = library; 19 | } 20 | 21 | public ModelAndView addForm(HttpServletRequest request, HttpServletResponse response) { 22 | return new ModelAndView("book/edit", "book", new Book()); 23 | } 24 | 25 | public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception { 26 | Book book = bind(request); 27 | library.saveBook(book); 28 | return new ModelAndView("redirect:book.form?action=view", "bookId", book.getBookId()); 29 | } 30 | 31 | private Book bind(HttpServletRequest request) throws Exception { 32 | Book book = new Book(); 33 | bind(request, book); 34 | return book; 35 | } 36 | 37 | public ModelAndView view(HttpServletRequest request, HttpServletResponse response) { 38 | return new ModelAndView("book/view", "book", getBook(request)); 39 | } 40 | 41 | public ModelAndView editForm(HttpServletRequest request, HttpServletResponse response) { 42 | return new ModelAndView("book/edit", "book", getBook(request)); 43 | } 44 | 45 | private Book getBook(HttpServletRequest request) { 46 | String bookId = request.getParameter("bookId"); 47 | Validate.notNull(bookId, "missing bookId parameter"); 48 | return library.getBook(Integer.parseInt(bookId)); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /multi-action-controller-example/src/main/java/examples/data/Book.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.apache.commons.lang.builder.ToStringBuilder; 6 | 7 | public class Book { 8 | 9 | private String title; 10 | private String author; 11 | 12 | private Integer bookId; 13 | 14 | public String getTitle() { 15 | return title; 16 | } 17 | 18 | public void setTitle(String title) { 19 | this.title = title; 20 | } 21 | 22 | public String getAuthor() { 23 | return author; 24 | } 25 | 26 | public void setAuthor(String author) { 27 | this.author = author; 28 | } 29 | 30 | public Integer getBookId() { 31 | return bookId; 32 | } 33 | 34 | public void setBookId(Integer bookId) { 35 | this.bookId = bookId; 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | return HashCodeBuilder.reflectionHashCode(this); 41 | } 42 | 43 | @Override 44 | public boolean equals(Object obj) { 45 | return EqualsBuilder.reflectionEquals(this, obj); 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return ToStringBuilder.reflectionToString(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /multi-action-controller-example/src/main/java/examples/data/Library.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class Library { 7 | 8 | private int nextBookId = 1; 9 | private Map books = new HashMap(); 10 | 11 | public Book getBook(int bookId) { 12 | return books.get(bookId); 13 | } 14 | 15 | public void saveBook(Book book) { 16 | Integer bookId = book.getBookId(); 17 | if (bookId == null) { 18 | bookId = nextBookId++; 19 | book.setBookId(bookId); 20 | } 21 | books.put(bookId, book); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /multi-action-controller-example/src/main/webapp/WEB-INF/example-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | 13 | bookController 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /multi-action-controller-example/src/main/webapp/WEB-INF/jsp/book/edit.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Add Book 12 | 13 | 14 | Edit Book 15 | 16 | 17 | 18 | 19 | 20 | 21 |

    Add Book

    22 |
    23 | 24 |

    Edit Book

    25 |
    26 |
    27 |
    28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
    Title
    Author
    41 | 42 | " /> 43 | 44 | 45 |
    46 |
    47 | request: 48 | 49 |
    50 | model: 51 | 52 |
    53 | 54 | 55 | -------------------------------------------------------------------------------- /multi-action-controller-example/src/main/webapp/WEB-INF/jsp/book/view.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | 9 | View Book 10 | 11 | 12 |

    View Book

    13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 |
    Title
    Author
    23 |
    24 | 25 | " /> 26 | 27 |
    28 |
    29 | request: 30 | 31 |
    32 | model: 33 | 34 |
    35 | 36 | 37 | -------------------------------------------------------------------------------- /multi-action-controller-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | multi-action-controller-example 6 | 7 | example 8 | org.springframework.web.servlet.DispatcherServlet 9 | 1 10 | 11 | 12 | example 13 | *.form 14 | 15 | 16 | index.html 17 | 18 | 19 | -------------------------------------------------------------------------------- /multi-action-controller-example/src/main/webapp/css/styles.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | font-family: Verdana, Arial, sans-serif; 4 | font-size: 90%; 5 | } 6 | -------------------------------------------------------------------------------- /multi-action-controller-example/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | examples.spring-mvc-examples 5 | spring-mvc-examples 6 | 1.0.0-SNAPSHOT 7 | pom 8 | 9 | hello-spring-mvc 10 | multi-action-controller-example 11 | json-view-example 12 | json-binding-example 13 | spring3-example 14 | spring3-json-example 15 | rest-app-example 16 | json-ajax-example 17 | jquery-json-example 18 | hibernate-example 19 | hibernate-search-example 20 | jquery-autocomplete-example 21 | ember-example 22 | 23 | 24 | -------------------------------------------------------------------------------- /rest-app-example/README.md: -------------------------------------------------------------------------------- 1 | REST Application Example 2 | ======================== 3 | 4 | Simple example of a RESTful web application that exposes both HTML and JSON interfaces using Spring 3.x and Spring-MVC. 5 | 6 | ### The Example Application 7 | 8 | This example builds on the application state machine of the previous examples by adding a list state, so the application 9 | can view, add, or edit multiple books: 10 | 11 | ![Library State Diagram](https://github.com/jfarr/spring-mvc-examples/raw/master/rest-app-example/src/site/library_state_diagram.png) 12 | 13 | **Library Application State Diagram** 14 | 15 | The example implements the two web application interfaces using two different controller classes that 16 | both derive from AbstractBookController: 17 | 18 | * HtmlBookController - implements the application above by rendering HTML views and accepting HTML encoded data (form-urlencoded and multipart/form) 19 | * JsonBookController - implements a subset of the application states by rendering JSON views and accepting JSON encoded data: 20 | * list, view - renders JSON views of these application states 21 | * save - accepts JSON encoded data and persists the application state 22 | 23 | AbstractBookController implements the basic book persistence (CRUD operations). The derived classes implement the application state 24 | changes (building the next model state and selecting the view to render). 25 | -------------------------------------------------------------------------------- /rest-app-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | examples.spring-mvc-examples 5 | rest-app-example 6 | 1.0.0-SNAPSHOT 7 | war 8 | 9 | 3.1.1.RELEASE 10 | 11 | 12 | 13 | 14 | maven-compiler-plugin 15 | 16 | 1.6 17 | 1.6 18 | 19 | 20 | 21 | 22 | 23 | 24 | javax.servlet 25 | servlet-api 26 | 2.5 27 | provided 28 | 29 | 30 | javax.servlet 31 | jstl 32 | 1.2 33 | 34 | 35 | org.springframework 36 | spring-core 37 | ${spring.version} 38 | 39 | 40 | org.springframework 41 | spring-webmvc 42 | ${spring.version} 43 | 44 | 45 | org.codehaus.jackson 46 | jackson-core-asl 47 | 1.9.7 48 | 49 | 50 | org.codehaus.jackson 51 | jackson-mapper-asl 52 | 1.9.7 53 | 54 | 55 | commons-lang 56 | commons-lang 57 | 2.6 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /rest-app-example/src/main/java/examples/controller/AbstractBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.web.bind.annotation.ResponseStatus; 8 | 9 | import examples.data.Book; 10 | import examples.data.Library; 11 | 12 | public abstract class AbstractBookController { 13 | 14 | private Library library; 15 | 16 | @Autowired 17 | public void setLibrary(Library library) { 18 | this.library = library; 19 | } 20 | 21 | @ResponseStatus(HttpStatus.NOT_FOUND) 22 | @SuppressWarnings("serial") 23 | public class NotFoundException extends Exception { 24 | } 25 | 26 | protected List getBooks() { 27 | return library.getBooks(); 28 | } 29 | 30 | protected Book getBook(int bookId) throws NotFoundException { 31 | Book book = library.getBook(bookId); 32 | if (book == null) { 33 | throw new NotFoundException(); 34 | } 35 | return book; 36 | } 37 | 38 | protected void saveBook(Book book) { 39 | library.saveBook(book); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /rest-app-example/src/main/java/examples/controller/HtmlBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.ModelAttribute; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | import org.springframework.web.servlet.ModelAndView; 9 | 10 | import examples.data.Book; 11 | 12 | @Controller 13 | @RequestMapping("/books") 14 | public class HtmlBookController extends AbstractBookController { 15 | 16 | @RequestMapping(value = "/", method = RequestMethod.GET) 17 | public ModelAndView list() { 18 | return new ModelAndView("book/list", "books", getBooks()); 19 | } 20 | 21 | @RequestMapping(value = "/", method = RequestMethod.POST) 22 | public ModelAndView save(@ModelAttribute("book") Book book) { 23 | saveBook(book); 24 | return new ModelAndView("redirect:/library/books/"); 25 | } 26 | 27 | @RequestMapping("/addForm") 28 | public ModelAndView addForm() { 29 | return new ModelAndView("book/edit", "book", new Book()); 30 | } 31 | 32 | @RequestMapping("/book/{bookId}") 33 | public ModelAndView view(@PathVariable int bookId) throws NotFoundException { 34 | return new ModelAndView("book/view", "book", getBook(bookId)); 35 | } 36 | 37 | @RequestMapping("/book/{bookId}/editForm") 38 | public ModelAndView editForm(@PathVariable int bookId) throws NotFoundException { 39 | return new ModelAndView("book/edit", "book", getBook(bookId)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /rest-app-example/src/main/java/examples/controller/JsonBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import org.springframework.web.bind.annotation.ResponseBody; 11 | 12 | import examples.data.Book; 13 | 14 | @Controller 15 | @RequestMapping("/books") 16 | public class JsonBookController extends AbstractBookController { 17 | 18 | @RequestMapping(value = "/", method = RequestMethod.GET, headers = "Accept=application/json") 19 | public @ResponseBody List list() { 20 | return getBooks(); 21 | } 22 | 23 | @RequestMapping(value = "/", method = RequestMethod.POST, headers = "Content-type=application/json") 24 | public @ResponseBody Book add(@RequestBody Book book) { 25 | saveBook(book); 26 | return book; 27 | } 28 | 29 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.PUT, headers = "Content-type=application/json") 30 | public @ResponseBody Book update(@PathVariable int bookId, @RequestBody Book book) { 31 | book.setBookId(bookId); 32 | saveBook(book); 33 | return book; 34 | } 35 | 36 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.GET, headers = "Accept=application/json") 37 | public @ResponseBody Book view(@PathVariable int bookId) throws NotFoundException { 38 | return getBook(bookId); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /rest-app-example/src/main/java/examples/data/Book.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.apache.commons.lang.builder.ToStringBuilder; 6 | 7 | public class Book { 8 | 9 | private String title; 10 | private String author; 11 | 12 | private Integer bookId; 13 | 14 | public String getTitle() { 15 | return title; 16 | } 17 | 18 | public void setTitle(String title) { 19 | this.title = title; 20 | } 21 | 22 | public String getAuthor() { 23 | return author; 24 | } 25 | 26 | public void setAuthor(String author) { 27 | this.author = author; 28 | } 29 | 30 | public Integer getBookId() { 31 | return bookId; 32 | } 33 | 34 | public void setBookId(Integer bookId) { 35 | this.bookId = bookId; 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | return HashCodeBuilder.reflectionHashCode(this); 41 | } 42 | 43 | @Override 44 | public boolean equals(Object obj) { 45 | return EqualsBuilder.reflectionEquals(this, obj); 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return ToStringBuilder.reflectionToString(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /rest-app-example/src/main/java/examples/data/Library.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | import org.springframework.stereotype.Repository; 10 | 11 | @Repository 12 | public class Library { 13 | 14 | private int nextBookId = 1; 15 | private Map books = new HashMap(); 16 | 17 | public Book getBook(int bookId) { 18 | return books.get(bookId); 19 | } 20 | 21 | public void saveBook(Book book) { 22 | Integer bookId = book.getBookId(); 23 | if (bookId == null) { 24 | bookId = nextBookId++; 25 | book.setBookId(bookId); 26 | } 27 | books.put(bookId, book); 28 | } 29 | 30 | public List getBooks() { 31 | List books = new ArrayList(); 32 | for (Integer bookId : getSortedBookIds()) { 33 | books.add(this.books.get(bookId)); 34 | } 35 | return books; 36 | } 37 | 38 | private List getSortedBookIds() { 39 | List bookIds = new ArrayList(this.books.keySet()); 40 | Collections.sort(bookIds); 41 | return bookIds; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /rest-app-example/src/main/java/examples/filter/AccessControlFilter.java: -------------------------------------------------------------------------------- 1 | package examples.filter; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.Filter; 6 | import javax.servlet.FilterChain; 7 | import javax.servlet.FilterConfig; 8 | import javax.servlet.ServletException; 9 | import javax.servlet.ServletRequest; 10 | import javax.servlet.ServletResponse; 11 | import javax.servlet.http.HttpServletResponse; 12 | 13 | /** 14 | * Implements Server-Side Access Control for Cross-Origin Resource Sharing. 15 | * 16 | * @see https://developer.mozilla.org/en-US/docs/Server-Side_Access_Control 17 | * @see http://dvcs.w3.org/hg/cors/raw-file/tip/Overview.html 18 | * 19 | * @author jfarr 20 | */ 21 | public class AccessControlFilter implements Filter { 22 | 23 | @Override 24 | public void init(FilterConfig filterConfig) throws ServletException { 25 | } 26 | 27 | @Override 28 | public void destroy() { 29 | } 30 | 31 | @Override 32 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 33 | throws IOException, ServletException { 34 | HttpServletResponse httpResponse = (HttpServletResponse) response; 35 | httpResponse.setHeader("Access-Control-Allow-Origin", "*"); 36 | httpResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); 37 | httpResponse.setHeader("Access-Control-Allow-Headers", "Content-Type"); 38 | chain.doFilter(request, response); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /rest-app-example/src/main/webapp/WEB-INF/example-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /rest-app-example/src/main/webapp/WEB-INF/jsp/book/edit.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | <c:out value="${title}" /> 14 | 15 | 16 | 17 |

    18 | 19 |
    20 |
    21 |
    22 | 23 |
    24 |
    25 |
    26 | 27 |
    28 |
    29 | 30 |
    31 |
    32 | 33 | 34 | 35 |
    36 |
    37 | request: 38 | 39 |
    40 |
    41 | model: 42 |
    43 | book: 44 | 45 |
    46 | 47 | 48 | -------------------------------------------------------------------------------- /rest-app-example/src/main/webapp/WEB-INF/jsp/book/list.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | Book List 11 | 12 | 13 | 14 |

    Book List

    15 | add book 16 |
    17 | 18 | 19 |

    Library has no books.

    20 |
    21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 |
    TitleAuthor 
    ">edit
    38 |
    39 |
    40 |
    41 |
    42 | request: 43 | 44 |
    45 |
    46 | model: 47 |
    48 | books: 49 | 50 |
    51 | 52 | 53 | -------------------------------------------------------------------------------- /rest-app-example/src/main/webapp/WEB-INF/jsp/book/view.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | View Book 13 | 14 | 15 | 16 |

    View Book

    17 |

    list

    18 | 19 |
    20 |
    21 |
    22 | 23 |
    24 |
    25 |
    26 | 27 |
    28 |
    29 | 30 |
    31 |
    32 | 33 |
    34 |
    35 | request: 36 | 37 |
    38 |
    39 | model: 40 |
    41 | book: 42 | 43 |
    44 | 45 | 46 | -------------------------------------------------------------------------------- /rest-app-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | spring-mvc-example 6 | 7 | AccessControlFilter 8 | examples.filter.AccessControlFilter 9 | 10 | 11 | AccessControlFilter 12 | /* 13 | 14 | 15 | example 16 | org.springframework.web.servlet.DispatcherServlet 17 | 1 18 | 19 | 20 | example 21 | /library/* 22 | 23 | 24 | index.html 25 | 26 | -------------------------------------------------------------------------------- /rest-app-example/src/main/webapp/css/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: Verdana, Arial, sans-serif; 3 | font-size: 90%; 4 | } 5 | 6 | #list-view table { 7 | margin: 1em 0; 8 | border-collapse: collapse; 9 | width: 100%; 10 | } 11 | 12 | #list-view table th { 13 | background-color: #cccccc; 14 | text-align: left; 15 | } 16 | 17 | #list-view .commands { 18 | width: 35px; 19 | } 20 | 21 | fieldset { 22 | padding: 0; 23 | border: 0; 24 | margin-top: 25px; 25 | } 26 | 27 | .field+.field { 28 | padding-top: 5px; 29 | } 30 | 31 | label { 32 | font-weight: bold; 33 | } 34 | 35 | input[type=text] { 36 | width: 450px; 37 | } 38 | 39 | input[type=submit] { 40 | border: 1px outset; 41 | width: 50px; 42 | } 43 | -------------------------------------------------------------------------------- /rest-app-example/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /rest-app-example/src/site/library_state_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/rest-app-example/src/site/library_state_diagram.png -------------------------------------------------------------------------------- /rest-app-example/src/site/library_state_diagram.zargo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/rest-app-example/src/site/library_state_diagram.zargo -------------------------------------------------------------------------------- /rest-app-example/src/test/resources/add-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/rest-app-example/library/books/POST{"title":"test","author":"added!"} 3 | -------------------------------------------------------------------------------- /rest-app-example/src/test/resources/list-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/rest-app-example/library/books/GET
    3 | -------------------------------------------------------------------------------- /rest-app-example/src/test/resources/update-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/rest-app-example/library/books/book/1/PUT{"title":"test","author":"updated!","bookId":1} 3 | -------------------------------------------------------------------------------- /rest-app-example/src/test/resources/view-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/rest-app-example/library/books/book/1/GET
    3 | -------------------------------------------------------------------------------- /spring3-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | examples.spring-mvc-examples 5 | spring3-example 6 | 1.0.0-SNAPSHOT 7 | war 8 | 9 | 3.1.1.RELEASE 10 | 11 | 12 | 13 | 14 | maven-compiler-plugin 15 | 16 | 1.6 17 | 1.6 18 | 19 | 20 | 21 | 22 | 23 | 24 | javax.servlet 25 | servlet-api 26 | 2.5 27 | 28 | 29 | javax.servlet 30 | jstl 31 | 1.2 32 | 33 | 34 | org.springframework 35 | spring-core 36 | ${spring.version} 37 | 38 | 39 | org.springframework 40 | spring-webmvc 41 | ${spring.version} 42 | 43 | 44 | commons-lang 45 | commons-lang 46 | 2.6 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring3-example/src/main/java/examples/controller/BookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.web.bind.annotation.ModelAttribute; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestParam; 8 | import org.springframework.web.servlet.ModelAndView; 9 | 10 | import examples.data.Book; 11 | import examples.data.Library; 12 | 13 | @Controller 14 | @RequestMapping("/book.form") 15 | public class BookController { 16 | 17 | private Library library; 18 | 19 | @Autowired 20 | public void setLibrary(Library library) { 21 | this.library = library; 22 | } 23 | 24 | @RequestMapping("/") 25 | public ModelAndView addForm() { 26 | return new ModelAndView("book/edit", "book", new Book()); 27 | } 28 | 29 | @RequestMapping(params="action=save") 30 | public ModelAndView save(@ModelAttribute("book") Book book) throws Exception { 31 | library.saveBook(book); 32 | return new ModelAndView("redirect:book.form?action=view", "bookId", book.getBookId()); 33 | } 34 | 35 | @RequestMapping(params="action=view") 36 | public ModelAndView view(@RequestParam Integer bookId) { 37 | return new ModelAndView("book/view", "book", library.getBook(bookId)); 38 | } 39 | 40 | @RequestMapping(params="action=editForm") 41 | public ModelAndView editForm(@RequestParam Integer bookId) { 42 | return new ModelAndView("book/edit", "book", library.getBook(bookId)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /spring3-example/src/main/java/examples/data/Book.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.apache.commons.lang.builder.ToStringBuilder; 6 | 7 | public class Book { 8 | 9 | private String title; 10 | private String author; 11 | 12 | private Integer bookId; 13 | 14 | public String getTitle() { 15 | return title; 16 | } 17 | 18 | public void setTitle(String title) { 19 | this.title = title; 20 | } 21 | 22 | public String getAuthor() { 23 | return author; 24 | } 25 | 26 | public void setAuthor(String author) { 27 | this.author = author; 28 | } 29 | 30 | public Integer getBookId() { 31 | return bookId; 32 | } 33 | 34 | public void setBookId(Integer bookId) { 35 | this.bookId = bookId; 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | return HashCodeBuilder.reflectionHashCode(this); 41 | } 42 | 43 | @Override 44 | public boolean equals(Object obj) { 45 | return EqualsBuilder.reflectionEquals(this, obj); 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return ToStringBuilder.reflectionToString(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /spring3-example/src/main/java/examples/data/Library.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.springframework.stereotype.Repository; 7 | 8 | @Repository 9 | public class Library { 10 | 11 | private int nextBookId = 1; 12 | private Map books = new HashMap(); 13 | 14 | public Book getBook(int bookId) { 15 | return books.get(bookId); 16 | } 17 | 18 | public void saveBook(Book book) { 19 | Integer bookId = book.getBookId(); 20 | if (bookId == null) { 21 | bookId = nextBookId++; 22 | book.setBookId(bookId); 23 | } 24 | books.put(bookId, book); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring3-example/src/main/webapp/WEB-INF/example-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /spring3-example/src/main/webapp/WEB-INF/jsp/book/edit.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 5 | 6 | 7 | 8 | 9 | 10 | 11 | <c:out value="${title}" /> 12 | 13 | 14 | 15 |

    16 | 17 |
    18 |
    19 |
    20 | 21 |
    22 |
    23 |
    24 | 25 |
    26 |
    27 | 28 |
    29 |
    30 | 31 | 32 | 33 | 34 |
    35 |
    36 | request: 37 | 38 |
    39 |
    40 | model: 41 |
    42 | book: 43 | 44 |
    45 | 46 | 47 | -------------------------------------------------------------------------------- /spring3-example/src/main/webapp/WEB-INF/jsp/book/view.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 5 | 6 | 7 | 8 | 9 | View Book 10 | 11 | 12 | 13 |

    View Book

    14 | 15 |
    16 |
    17 |
    18 | 19 |
    20 |
    21 |
    22 | 23 |
    24 |
    25 | 26 |
    27 |
    28 | 29 | 30 |
    31 |
    32 | request: 33 | 34 |
    35 |
    36 | model: 37 |
    38 | book: 39 | 40 |
    41 | 42 | 43 | -------------------------------------------------------------------------------- /spring3-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | spring3-example 6 | 7 | example 8 | org.springframework.web.servlet.DispatcherServlet 9 | 1 10 | 11 | 12 | example 13 | *.form 14 | 15 | 16 | index.html 17 | 18 | -------------------------------------------------------------------------------- /spring3-example/src/main/webapp/css/styles.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | font-family: Verdana, Arial, sans-serif; 4 | font-size: 90%; 5 | } 6 | 7 | fieldset { 8 | padding: 0; 9 | border: 0; 10 | margin-top: 25px; 11 | } 12 | 13 | .field + .field { 14 | padding-top: 5px; 15 | } 16 | 17 | label { 18 | font-weight:bold; 19 | } 20 | 21 | input[type=text] { 22 | width: 450px; 23 | } 24 | 25 | input[type=submit] { 26 | border: 1px outset; 27 | width: 50px; 28 | } 29 | -------------------------------------------------------------------------------- /spring3-example/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /spring3-json-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | examples.spring-mvc-examples 5 | spring3-json-example 6 | 1.0.0-SNAPSHOT 7 | war 8 | 9 | 3.1.1.RELEASE 10 | 11 | 12 | 13 | 14 | maven-compiler-plugin 15 | 16 | 1.6 17 | 1.6 18 | 19 | 20 | 21 | 22 | 23 | 24 | javax.servlet 25 | servlet-api 26 | 2.5 27 | provided 28 | 29 | 30 | javax.servlet 31 | jstl 32 | 1.2 33 | 34 | 35 | org.springframework 36 | spring-core 37 | ${spring.version} 38 | 39 | 40 | org.springframework 41 | spring-webmvc 42 | ${spring.version} 43 | 44 | 45 | org.codehaus.jackson 46 | jackson-core-asl 47 | 1.9.7 48 | 49 | 50 | org.codehaus.jackson 51 | jackson-mapper-asl 52 | 1.9.7 53 | 54 | 55 | commons-lang 56 | commons-lang 57 | 2.6 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /spring3-json-example/src/main/java/examples/controller/AbstractBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.ResponseStatus; 8 | 9 | import examples.data.Book; 10 | import examples.data.Library; 11 | 12 | @Controller 13 | @RequestMapping("/books") 14 | public abstract class AbstractBookController { 15 | 16 | private Library library; 17 | 18 | @Autowired 19 | public void setLibrary(Library library) { 20 | this.library = library; 21 | } 22 | 23 | @ResponseStatus(HttpStatus.NOT_FOUND) 24 | @SuppressWarnings("serial") 25 | public class NotFoundException extends Exception { 26 | } 27 | 28 | protected Book getBook(int bookId) throws NotFoundException { 29 | Book book = library.getBook(bookId); 30 | if (book == null) { 31 | throw new NotFoundException(); 32 | } 33 | return book; 34 | } 35 | 36 | protected void saveBook(Book book) { 37 | library.saveBook(book); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /spring3-json-example/src/main/java/examples/controller/HtmlBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.ModelAttribute; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | import org.springframework.web.servlet.ModelAndView; 9 | 10 | import examples.data.Book; 11 | 12 | @Controller 13 | @RequestMapping("/books") 14 | public class HtmlBookController extends AbstractBookController { 15 | 16 | 17 | @RequestMapping("/") 18 | public ModelAndView addForm() { 19 | return new ModelAndView("book/edit", "book", new Book()); 20 | } 21 | 22 | @RequestMapping(value = "/", method = RequestMethod.POST) 23 | public ModelAndView save(@ModelAttribute("book") Book book) throws Exception { 24 | saveBook(book); 25 | return new ModelAndView("redirect:book/" + book.getBookId()); 26 | } 27 | 28 | @RequestMapping("/book/{bookId}") 29 | public ModelAndView view(@PathVariable int bookId) throws NotFoundException { 30 | return new ModelAndView("book/view", "book", getBook(bookId)); 31 | } 32 | 33 | @RequestMapping("/book/{bookId}/editForm") 34 | public ModelAndView editForm(@PathVariable int bookId) throws NotFoundException { 35 | return new ModelAndView("book/edit", "book", getBook(bookId)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spring3-json-example/src/main/java/examples/controller/JsonBookController.java: -------------------------------------------------------------------------------- 1 | package examples.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.PathVariable; 5 | import org.springframework.web.bind.annotation.RequestBody; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | import org.springframework.web.bind.annotation.ResponseBody; 9 | 10 | import examples.data.Book; 11 | 12 | @Controller 13 | @RequestMapping("/books") 14 | public class JsonBookController extends AbstractBookController { 15 | 16 | @RequestMapping(value = "/", method = RequestMethod.POST, headers = "Content-type=application/json") 17 | public @ResponseBody Book add(@RequestBody Book book) { 18 | saveBook(book); 19 | return book; 20 | } 21 | 22 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.PUT, headers = "Content-type=application/json") 23 | public @ResponseBody Book update(@PathVariable int bookId, @RequestBody Book book) { 24 | book.setBookId(bookId); 25 | saveBook(book); 26 | return book; 27 | } 28 | 29 | @RequestMapping(value = "/book/{bookId}", method = RequestMethod.GET, headers = "Accept=application/json") 30 | public @ResponseBody Book view(@PathVariable int bookId) throws NotFoundException { 31 | return getBook(bookId); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /spring3-json-example/src/main/java/examples/data/Book.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.apache.commons.lang.builder.ToStringBuilder; 6 | 7 | public class Book { 8 | 9 | private String title; 10 | private String author; 11 | 12 | private Integer bookId; 13 | 14 | public String getTitle() { 15 | return title; 16 | } 17 | 18 | public void setTitle(String title) { 19 | this.title = title; 20 | } 21 | 22 | public String getAuthor() { 23 | return author; 24 | } 25 | 26 | public void setAuthor(String author) { 27 | this.author = author; 28 | } 29 | 30 | public Integer getBookId() { 31 | return bookId; 32 | } 33 | 34 | public void setBookId(Integer bookId) { 35 | this.bookId = bookId; 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | return HashCodeBuilder.reflectionHashCode(this); 41 | } 42 | 43 | @Override 44 | public boolean equals(Object obj) { 45 | return EqualsBuilder.reflectionEquals(this, obj); 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return ToStringBuilder.reflectionToString(this); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /spring3-json-example/src/main/java/examples/data/Library.java: -------------------------------------------------------------------------------- 1 | package examples.data; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.springframework.stereotype.Repository; 7 | 8 | @Repository 9 | public class Library { 10 | 11 | private int nextBookId = 1; 12 | private Map books = new HashMap(); 13 | 14 | public Book getBook(int bookId) { 15 | return books.get(bookId); 16 | } 17 | 18 | public void saveBook(Book book) { 19 | Integer bookId = book.getBookId(); 20 | if (bookId == null) { 21 | bookId = nextBookId++; 22 | book.setBookId(bookId); 23 | } 24 | books.put(bookId, book); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring3-json-example/src/main/webapp/WEB-INF/example-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /spring3-json-example/src/main/webapp/WEB-INF/jsp/book/edit.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 7 | 8 | 9 | 10 | <c:out value="${title}" /> 11 | 12 | 13 |

    14 | 15 |
    16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 |
    Title
    Author
    29 | 30 | " /> 31 | 32 |
    33 |
    34 | request: 35 | 36 |
    37 |
    38 | model: 39 |
    40 | book: 41 | 42 |
    43 | 44 | 45 | -------------------------------------------------------------------------------- /spring3-json-example/src/main/webapp/WEB-INF/jsp/book/view.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | 8 | View Book 9 | 10 | 11 | 12 |

    View Book

    13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 |
    Title
    Author
    23 |
    24 | 25 |
    26 |
    27 | request: 28 | 29 |
    30 |
    31 | model: 32 |
    33 | book: 34 | 35 |
    36 | 37 | 38 | -------------------------------------------------------------------------------- /spring3-json-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | spring3-json-example 6 | 7 | example 8 | org.springframework.web.servlet.DispatcherServlet 9 | 1 10 | 11 | 12 | example 13 | /library/* 14 | 15 | 16 | index.html 17 | 18 | 19 | -------------------------------------------------------------------------------- /spring3-json-example/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /spring3-json-example/src/test/resources/save-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/spring3-json-example/library/books/book/1PUT{"title":"test","author":"test2","bookId":1} 3 | -------------------------------------------------------------------------------- /spring3-json-example/src/test/resources/view-json.rcq: -------------------------------------------------------------------------------- 1 | 2 | 1.1http://localhost:8080/spring3-json-example/library/books/book/1GET
    3 | -------------------------------------------------------------------------------- /src/site/library_http_state_diagram.zargo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/src/site/library_http_state_diagram.zargo -------------------------------------------------------------------------------- /src/site/library_state_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/src/site/library_state_diagram.png -------------------------------------------------------------------------------- /src/site/library_state_diagram.zargo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jfarr/spring-mvc-examples/ff4892df1f7e37e9358e43af14cb396719d25c42/src/site/library_state_diagram.zargo --------------------------------------------------------------------------------