Spring part 1: Getting Started with Gradle

Table of Contents

  1. Overview
  2. Project Setup
    • Gradle Config : build.gradle
    • Configuration : web.xml
    • Context.xml
    • JSP
    • Controller


1. Overview
Spring is prominent framework developed by Rod Johnson in 2003. It is modular, light weight, open source framework and inversion of control container for the Java platform. Spring is based on POJO model, loose coupling and is non-invasive.


2. Project Setup
We’ll start by creating simple spring web application with gradle (project automation tool built upon concepts of Apache Ant and Apache Maven and is Groovy based DSL) in Eclipse IDE.

File > New > Dynamic Web Project

Provide a suitable project name and proceed, check generate web.xml and finish.


2.a. Gradle Config : build.gradle
Gradle is project automation tool built upon concepts of Apache Ant and Apache Maven and is Groovy based DSL(Domain Specific Language) instead of the more traditional XML form of declaring the project configuration. Unlike Maven, Gradle uses a directed acyclic graph (“DAG”) to determine the order in which tasks can be run.

apply plugin : 'eclipse'
apply plugin : 'java'
apply plugin : 'war'</span>

ext {
springVersion = '4.0.3.RELEASE'
logbackClassickVersion= '1.1.1'
}

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
compile ("commons-codec:commons-codec:1.9")
compile ("commons-logging:commons-logging:1.1.1")
// Spring framework jars
compile ("org.springframework:spring-web:${springVersion}")
compile ("org.springframework:spring-webmvc:${springVersion}")
compile ("org.springframework:spring-context:${springVersion}")
compile ("org.springframework:spring-expression:${springVersion}")
compile ("org.springframework:spring-beans:${springVersion}")
compile ("org.springframework:spring-core:${springVersion}")
compile ("org.springframework:spring-aop:${springVersion}")
compile ("jstl:jstl:1.2")
}

task wrapper(type: Wrapper){
gradleVersion = '1.11'
}

The ‘plugins’ provide focused functionality to the Gradle.
Java Plugin – compiling, testing and publishing Java based software
Eclipse Plugin – integrating with the Eclipse IDE for development
War Plugin – project war generation

2.b. Configuration : web.xml
A deployment descriptor describes how a component, module or application (such as a web application or enterprise application) should be deployed. It directs a deployment tool to deploy a module or application with specific container options, security settings and describes specific configuration requirements. source

&lt;servlet&gt;
&lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
&lt;init-param&gt;
&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
&lt;param-value&gt;/WEB-INF/servlet-context.xml&lt;/param-value&gt;
&lt;/init-param&gt;
&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;
&lt;context-param&gt;
&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
&lt;param-value&gt;/WEB-INF/application-context.xml&lt;/param-value&gt;
&lt;/context-param&gt;</span>

&lt;servlet-mapping&gt;
&lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;listener&gt;
&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
&lt;/listener&gt;

The DispatcherServlet creates its own WebApplicationContext and is a central servlet that manages handlers/controllers/view-resolvers and offers functionality facilitating the development of web applications.
Basically, ContextLoaderListener creates a root web-application-context for the web-application and puts it in the ServletContext. It can be used to load and unload the spring-managed beans irrespective of what technology is being used in the controller layer.
source

2.c. Configuration: context.xml
application-context.xml

&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
&lt;import resource="controllers.xml" /&gt;
&lt;/beans&gt;

servlet-context.xml

&lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
&lt;beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
&lt;beans:property name="prefix" value="/WEB-INF/jsp/" /&gt;
&lt;beans:property name="suffix" value=".jsp" /&gt;
&lt;/beans:bean&gt;
&lt;/beans:beans&gt;

“InternalResourceViewResolver” resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/jsp directory.


2.d. Pages
Create all jsp file under WEB-INF/jsp directory.
index.jsp

&lt;body&gt;
&lt;h3&gt;Howdy&lt;/h3&gt;
&lt;a href='&lt;c:out value="/nextpage"/&gt;'&gt;Next Page&lt;/a&gt;
&lt;/body&gt;

nextpage.jsp

&lt;body&gt;
&lt;h3&gt;Welcome to Spring Framework.&lt;/h3&gt;
&lt;/body&gt;


2.e. Controllers.xml
A context file for defining application controllers.

&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
&lt;context:component-scan base-package="com.pj.ci.server" /&gt;
&lt;mvc:annotation-driven /&gt;
&lt;/beans&gt;

“component-scan” scan across the package to find and register the beans in application context.
“annotation-driven” declares support for spring MVC features like @RequestMapping, @Controller as well as declrative validation with @Valid and message body conversion with @RequestBody & @ResponseBody
MainController class

@Controller
public class MainController {</span>

@RequestMapping(value = "/", method = RequestMethod.GET)
public String getMainPage(ModelMap modelMap) {
return "index";
}

@RequestMapping(value = "/nextpage", method = RequestMethod.GET)
public String getNextPage(ModelMap modelMap) {
return "nextpage";
}
}

This controller redirects request for ‘/’ and ‘/nextpage’ to ‘index’ & ‘nextpage’ view mapped in WEB-INF/jsp directory. The application default page is ‘index.jsp’

Project compile & running
In the terminal or console, browse the project directory and execute below commands. To use ‘gradle’ command in console we’ll need to set GRADLE_HOME in system.

gradle eclipse

This command will locallize the classpath of project for Eclipse IDE.

gradle build

This command will compile and generate necessary artifacts for project.
* “gradle wrapper” command can also be executed to generate wrapper file for the project. For this, we’ll need to add ‘task wrapper’ in build.gradle. With wrapper file, we’ll be able to use “gradlew eclipse” or “gradlew build”.

Reference :
1. Spring docs : 4.0.3 Release
2. Gradle user guide

Leave a comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.