Skip to content

Instantly share code, notes, and snippets.

@pnminh
Forked from somecuitears/Gradle SpringMVC.md
Created January 10, 2018 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pnminh/d56e0dd5311d2e37d72cb47114545c38 to your computer and use it in GitHub Desktop.
Save pnminh/d56e0dd5311d2e37d72cb47114545c38 to your computer and use it in GitHub Desktop.
Spring MVC from Gradle in IntelliJ

Creating Gradle Project

  1. Create New Project

  2. Select Java and Web from the Option

  3. You will be asked to provide GroupID and ArtifactID

  • GroupID: it will identify your project uniquely across all projects. (E.g. com.project.model | com.project.plugins)
  • ArtifactID: is the name of the jar without version. If you created it then you can choose whatever name you want with lowercase letters and no strange symbols (E.g. maven | some-project )
  1. Click Next and Select use auto-import. It is recommended to select gradle_wrapper for novice user. This ensures that the project is build using same version by everyone. If you select local version of the gradle then same version of gradle must be available to all. It is usually placed in VC.
  1. Open build.gradle file from the project tab and add following.
  • Add buildscript

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")
        }
    }
    
  • Add following plugin

    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'org.springframework.boot'
    apply plugin: "war"
    war {
        baseName = 'SpringMVCExample'
        version = '1.0.0-BUILD-SNAPSHOT'
    }
    
  • Add compatibility

    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    
  • Add Dependencies

    dependencies {
        compile("org.springframework.boot:spring-boot-starter-thymeleaf")
        compile("org.springframework.boot:spring-boot-devtools")
        testCompile("junit:junit")
    }
    
  1. After gradle has completed downloading add package named java inside main package

  2. Inside the java folder create your base package com.project

  3. Create Controller and Model inside this package

  4. Create resources folder under main directory

  5. create WEB-INF folder and add dispatcher-servlet.xml Spring configuration file.

  6. Open Project Structure and under Modules > Spring add dispatcher-servlet.xml as spring Application Context

  7. Paste Following to the configuration xml file.

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans     
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 <context:component-scan base-package="com.**BASE**" />
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/" />
      <property name="suffix" value=".jsp" />
  </bean>
  1. Add web.xml in similar way and paste following inside
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
      version="3.1">
   <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
   </context-param>
   <servlet>
       <servlet-name>dispatcher</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
       <servlet-name>dispatcher</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>
 </web-app>
  1. Add Tomcat server from Run > Edit Configuration Scroll down to the bottom and add Local Tomcat Server Provide the name of the server and under Deployment tab click + sign and add Aritfact. An war exploded artifact will be added to the list.

  2. Final Project Structure :

Image

  1. More Information
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment