Create Spring MVC Project On The Eclipse, Create A Spring MVC Project
Create Spring MVC Project On The Eclipse, Create A Spring MVC Project
Name: Lesson1SpringMVC
Group: com.ptithcm
Artifact: Lesson1SpringMVC
Description: Lesson 1 Spring MVC
Package: com.ptithcm.lesson1
Select the technologies and libraries to be used:
Web
<groupId>com.demo</groupId>
<artifactId>LearnSpringMVCWithRealApps</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>LearnSpringMVCWithRealApps</name>
<description>Learn Spring MVC with Real Apps</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Configure application.properties
1. spring.mvc.view.prefix = /WEB-INF/views/
2. spring.mvc.view.suffix = .jsp
3. spring.mvc.static-path-pattern=/resources/**
4. server.port=9596
Create Controller
Create new package named com.demo.controllers. In this package, create new controller name
HomeController as below:
package com.ptithcm.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping(value = "home")
public class HomeController {
@RequestMapping(method = RequestMethod.GET)
public String index() {
return "home/index";
}
Create View
Create new folders with path webapp\WEB-INF\views in src\main. In views folder, create new
folder named demo. In demo folder, create new views as below:
Index View
Create new jsp file named index.jsp
Run Application
Select LearnSpringMVCWithRealAppsApplication.java file in com.demo package, right click and
select Run As/Spring Boot App menu
Output
Output
Output