:::: MENU ::::

Spring MVC


Spring MVC is a framework that separates components of an application in three layers (model, view and controller).

Installation

It is necessary to use the dependencies of this framework, adding them into our project manually or using a manager. By using Maven we can add the following configuration to our pom.xml file:


	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
		</dependency>
	</dependencies>

	<properties>
		<java.version>1.9</java.version>
	</properties>


	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

Thymeleaf and the prototypation

We can use a Java template engine like Thymeleaf. By doing this we will be able to previsualize our HTML files without the need of executing tge application and doing as many changes as we want without the need of redeployment the application. This although it is not possible with the JSP technology.

Example

We are going to create an application that by acceding to the http://localhost:8080/myurl path it returns us a page with a established message in a Java component.

In first place we create the template (using the Thymeleaf namespace). We locate this file in the src/main/resources/templates path.

myPage.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1" />
<title>My title</title>
</head>
<body>
	

</body>
</html>

The content of the ${myAttribute} element will be established in a Java component.

Now we create the controller, through which and by the access URL it will execute a correct methos.

MyController.java

package com.luisgomezcaballero.spring_mvc_demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyController {

	@RequestMapping(&quot;/myurl&quot;)
	public String myMethod(Model model) {
		model.addAttribute(&quot;myAttribute&quot;, &quot;Hello World!&quot;);
		return &quot;myPage&quot;;
	}

}

Lastly, to be able to start the application, we create a Main class.

Main.java

package com.luisgomezcaballero.spring_mvc_demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main  {

	public static void main(String[] args) {
		SpringApplication.run(Main .class, args);
	}

}

In order to execute this app, right click on the Main class, Run As/Java Application. It will start the embebbed Tomcat server and we will be able to access to http://localhost:8080/myurl, where we will see the message we stabliahed in the controller.

Repository

The content of this project is available at https://github.com/luisgomezcaballero/spring-mvc-demo.


So, what do you think ?