reading-notes

Read: 11 - Spring

Spring MVC framework enables separation of modules namely Model, View and Controller and seamlessly handles the application integration. This enables the developer to create complex applications also using plain java classes. The model object can be passed between view and controller using maps. In this article, we will see how to set up a Spring MVC application in the Eclipse IDE and understand how to make applications.

The Spring MVC framework is comprised of the following components:


to build an web application with spring we:

  1. we will accept http Get requests at: http://localhost:8080/greeting
  2. if we have query string we will customize the greeting with an optional name parameter and the URL will be something like this: http://localhost:8080/greeting?name=User.
  3. we need firstly to generate a new project from Spring Initializr with the required dependencies. and inside the gradle build file we will see this in the dependencies sextion:
    dependencies {
     implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
     implementation 'org.springframework.boot:spring-boot-starter-web'
     developmentOnly 'org.springframework.boot:spring-boot-devtools'
     testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }
    
    • we can create the project manually from intellij
package com.example.servingwebcontent;

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

@Controller
public class GreetingController {

	@GetMapping("/greeting")
	public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
		model.addAttribute("name", name);
		return "greeting";
	}

}

The implementation of the method body relies on Thymeleaf technoloyg to perform server-side rendering of the HTML. Thymeleaf parses the greeting.html template and evaluates the th:text expression to render the value of the ${name} parameter that was set in the controller.

greeting.html file example:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head> 
    <title>Getting Started: Serving Web Content</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

when we Initializ our project this calss will be ready:

package com.example.servingwebcontent;

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

@SpringBootApplication
public class ServingWebContentApplication {

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

}

to run the priject we just press on SpringApplication.run() from Intellij or by /gradlew bootRun from the terminal.

we can build the JAR file by using ./gradlew build and then run the JAR file, as follows:

java -jar build/libs/gs-serving-web-content-0.1.0.jar