0% found this document useful (0 votes)
37 views

Create Spring MVC Project On The Eclipse, Create A Spring MVC Project

Uploaded by

phamtanphat469
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Create Spring MVC Project On The Eclipse, Create A Spring MVC Project

Uploaded by

phamtanphat469
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Create Spring MVC Project

On the Eclipse, create a Spring MVC project

Enter Project Information:

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

Click Next button to show Site Information for project

Click Finish button to finish create Spring MVC project


Configure pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<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>

<!-- Spring MVC -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- JSTL tag lib -->


<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>

<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

<!-- Tomcat for JSP rendering -->


<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</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

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index View</title>
</head>
<body>
<h3>Welcome to Spring MVC</h3>
</body>
</html>

Structure of Spring MVC Project

Run Application
Select LearnSpringMVCWithRealAppsApplication.java file in com.demo package, right click and
select Run As/Spring Boot App menu

Access index method in demo controller with following url: http://localhost:9596/demo

Output

Access hi method in demo controller with following url: http://localhost:9596/demo/hi?


fullName=Kevin

Output

Access sum method in demo controller with following url: http://localhost:9596/demo/sum?


a=2&b=3

Output

You might also like