-
Create New Project
-
Select Java and Web from the Option
-
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 )
- 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.
[Gradle Installation Guide] (https://gist.github.com/somecuitears/b92f6a8514363b646bca36641ae7ce09)
- 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") }
-
After gradle has completed downloading add package named java inside main package
-
Inside the java folder create your base package com.project
-
Create Controller and Model inside this package
-
Create resources folder under main directory
-
create WEB-INF folder and add dispatcher-servlet.xml Spring configuration file.
-
Open Project Structure and under Modules > Spring add dispatcher-servlet.xml as spring Application Context
-
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>
- 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>
-
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.
-
Final Project Structure :