Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!