Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 10 ■ Showing Your Spring Application on the Web Location ${doc.location}: The idea of this view is to iterate over all the documents saved in the database (In-memory HSQL), and display them one by one. But how are you going to accomplish this? Well, first you need to add some dependencies to the build.gradle file. Listing 10-7 shows you how to do that. Listing 10-7. build.gradle apply plugin: 'jetty' //... more configuration options dependencies { //... more dependencies compile 'org.springframework:spring-webmvc:4.0.5.RELEASE' runtime 'jstl:jstl:1.2' } //... more dependencies Running this application as a web application will require a web container. In your case, you will use Jetty, a light container. The definition is in Listing 10-7: apply plugin: 'jetty'. Also, you are going to require a JSTL (Java Standard Tag Library) runtime that will help you to add logic embedded in your page and in this case to iterate through the list returned by the controller. Of course, you need to have the Spring Web MVC extension dependency at compile time. Now if you run as gradle :ch10:jettyRun, you should get the following output: ..... 10:35:26.324 [main] DEBUG o.s.web.servlet.DispatcherServlet - Published WebApplicationContext of servlet 'mydocuments' as ServletContext attribute with name [org.springframework.web.servlet. FrameworkServlet.CONTEXT.mydocuments] 10:35:26.325 [main] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'mydocuments': initialization completed in 3881 ms 10:35:26.325 [main] DEBUG o.s.web.servlet.DispatcherServlet - Servlet 'mydocuments' configured successfully > Building 80% > :ch10:jettyRun > Running at http://localhost:8080/ch10 139
Chapter 10 ■ Showing Your Spring Application on the Web ■ ■Note Remember if you are using the book’s companion source code, at the root of the project you can run gradle :ch10:jettyRun; if you are in the ch10 folder, just run gradle jettyRun. Now you can go to the following URL: http://localhost:8080/ch10/mydocuments/search/all. Figure 10-2 shows the result. Figure 10-2. Result of Rendering all.jspx The following segment shows the output of the logs used by the Jetty container when I accessed the web page. You can see the JDBC calls in action and how they are being rendered. 10:38:27.615 [1141619556@qtp-568919009-0] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'mydocuments' processing GET request for [/ch10/mydocuments/search/all] 10:38:27.616 [1141619556@qtp-568919009-0] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /search/all 10:38:27.618 [1141619556@qtp-568919009-0] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public java.lang.String com.apress.isf.spring.web.SearchController. searchAll(org.springframework.ui.Model)] 10:38:27.618 [1141619556@qtp-568919009-0] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'searchController' 140
- Page 87 and 88: Chapter 7 ■ Testing Your Spring A
- Page 89 and 90: Chapter 8 Give Advice to Your Sprin
- Page 91 and 92: Chapter 8 ■ Give Advice to Your S
- Page 93 and 94: Chapter 8 ■ Give Advice to Your S
- Page 95 and 96: Chapter 8 ■ Give Advice to Your S
- Page 97 and 98: Chapter 8 ■ Give Advice to Your S
- Page 99 and 100: Chapter 8 ■ Give Advice to Your S
- Page 101 and 102: Chapter 8 ■ Give Advice to Your S
- Page 103 and 104: Chapter 8 ■ Give Advice to Your S
- Page 105 and 106: Chapter 8 ■ Give Advice to Your S
- Page 107 and 108: Chapter 8 ■ Give Advice to Your S
- Page 109 and 110: Chapter 8 ■ Give Advice to Your S
- Page 111 and 112: Chapter 9 ■ Adding Persistence to
- Page 113 and 114: Chapter 9 ■ Adding Persistence to
- Page 115 and 116: Chapter 9 ■ Adding Persistence to
- Page 117 and 118: Chapter 9 ■ Adding Persistence to
- Page 119 and 120: Chapter 9 ■ Adding Persistence to
- Page 121 and 122: Chapter 9 ■ Adding Persistence to
- Page 123 and 124: Chapter 9 ■ Adding Persistence to
- Page 125 and 126: Chapter 9 ■ Adding Persistence to
- Page 127 and 128: Chapter 9 ■ Adding Persistence to
- Page 129 and 130: Chapter 9 ■ Adding Persistence to
- Page 131 and 132: Chapter 9 ■ Adding Persistence to
- Page 133 and 134: Chapter 10 ■ Showing Your Spring
- Page 135 and 136: Chapter 10 ■ Showing Your Spring
- Page 137: Chapter 10 ■ Showing Your Spring
- Page 141 and 142: Chapter 10 ■ Showing Your Spring
- Page 143 and 144: Chapter 10 ■ Showing Your Spring
- Page 145 and 146: Chapter 11 ■ Integrating Your Spr
- Page 147 and 148: Chapter 11 ■ Integrating Your Spr
- Page 149 and 150: Chapter 11 ■ Integrating Your Spr
- Page 151 and 152: Chapter 11 ■ Integrating Your Spr
- Page 153 and 154: Chapter 11 ■ Integrating Your Spr
- Page 155 and 156: Chapter 11 ■ Integrating Your Spr
- Page 157 and 158: Chapter 11 ■ Integrating Your Spr
- Page 159 and 160: Chapter 11 ■ Integrating Your Spr
- Page 161 and 162: Chapter 11 ■ Integrating Your Spr
- Page 163 and 164: Chapter 11 ■ Integrating Your Spr
- Page 165 and 166: Chapter 11 ■ Integrating Your Spr
- Page 167 and 168: Chapter 12 Exposing a REST API Nowa
- Page 169 and 170: Chapter 12 ■ Exposing a REST API
- Page 171 and 172: Chapter 12 ■ Exposing a REST API
- Page 173 and 174: Chapter 12 ■ Exposing a REST API
- Page 175 and 176: Chapter 12 ■ Exposing a REST API
- Page 177 and 178: Chapter 12 ■ Exposing a REST API
- Page 179 and 180: HTTP-DELETE Chapter 12 ■ Exposing
- Page 181 and 182: Chapter 13 Adding E-mail and Schedu
- Page 183 and 184: Chapter 13 ■ Adding E-mail and Sc
- Page 185 and 186: Chapter 13 ■ Adding E-mail and Sc
- Page 187 and 188: Chapter 13 ■ Adding E-mail and Sc
Chapter 10 ■ Showing Your <strong>Spring</strong> Application on the Web<br />
<br />
Location<br />
${doc.location}:<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
The idea of this view is to iterate over all the documents saved in the database (In-memory HSQL), and display<br />
them one by one. But how are you going to accomplish this? Well, first you need to add some dependencies to the<br />
build.gradle file. Listing 10-7 shows you how to do that.<br />
Listing 10-7. build.gradle<br />
apply plugin: 'jetty'<br />
//... more configuration options<br />
dependencies {<br />
//... more dependencies<br />
compile 'org.springframework:spring-webmvc:4.0.5.RELEASE'<br />
runtime 'jstl:jstl:1.2'<br />
}<br />
//... more dependencies<br />
Running this application as a web application will require a web container. In your case, you will use Jetty, a<br />
light container. The definition is in Listing 10-7: apply plugin: 'jetty'. Also, you are going to require a JSTL (Java<br />
Standard Tag Library) runtime that will help you to add logic embedded in your page and in this case to iterate<br />
through the list returned by the controller. Of course, you need to have the <strong>Spring</strong> Web MVC extension dependency at<br />
compile time.<br />
Now if you run as gradle :ch10:jettyRun, you should get the following output:<br />
.....<br />
10:35:26.324 [main] DEBUG o.s.web.servlet.DispatcherServlet - Published WebApplicationContext of<br />
servlet 'mydocuments' as ServletContext attribute with name [org.springframework.web.servlet.<br />
<strong>Framework</strong>Servlet.CONTEXT.mydocuments]<br />
10:35:26.325 [main] INFO o.s.web.servlet.DispatcherServlet - <strong>Framework</strong>Servlet 'mydocuments':<br />
initialization completed in 3881 ms<br />
10:35:26.325 [main] DEBUG o.s.web.servlet.DispatcherServlet - Servlet 'mydocuments' configured<br />
successfully<br />
> Building 80% > :ch10:jettyRun > Running at http://localhost:8080/ch10<br />
139