Hello world: web + thymeleaf

 Prerequisites

Place files as shown in the following diagram



The file content of start_env.cmd

@echo off

set curr_path=%cd%

set soft_path=%curr_path%\soft

set JAVA_HOME=%soft_path%\jdk-21.0.3+9

set java_path=%JAVA_HOME%\bin

set M2_HOME=%soft_path%\apache-maven-3.9.7\bin

set gradle_path=%soft_path%\gradle-8.9\bin

set CATALINA_HOME=%soft_path%\apache-tomcat-10.1.26

set spcli_path=%soft_path%\spring-3.3.2\bin

set eclipse_path=%soft_path%\eclipse

set curl_path=%soft_path%\curl-8.8.0_3-win64-mingw\bin

set npp_path=%soft_path%\npp.8.6.9.portable.x64

set c7z_path=%soft_path%\7z

set hx_path=%soft_path%\helix-24.07-x86_64-windows

set pdf_path="%soft_path%\PDF-XChange Editor"

set NODE_HOME="%soft_path%\node-v22.5.1-win-x64"

set TTCLI_HOME=%soft_path%\ttcli\bin

set NODE_PATH=%curr_path%\node_modules

set SHORTCUT_PATH=%curr_path%\shortcut

echo JAVA Environment Start

cmd /k "set PATH=%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%M2_HOME%;%c7z_path%;%gradle_path%;%java_path%;%spcli_path%;%eclipse_path%;%npp_path%;%curl_path%;%hx_path%;%NODE_HOME%;%pdf_path%;%SHORTCUT_PATH%;%TTCLI_HOME%"



Guide


1. Double-click to run the start_env.bat file



2. Run the following commands in the terminal window


$ mkdir code

$ cd code

$ spring init -d=web,thymeleaf --build=maven -b=3.3.2 -j=21 demo


3. Create file src/main/java/com/example/demo/RootController.java

package com.example.demo;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

@RequestMapping("/")

public class RootController {

  @GetMapping

  public String index(Model model) {

    model.addAttribute("pageTitle", "Hello Thymeleaf");

    return "index";

  }

}


4. Create file src/main/resources/templates/index.html

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"

  xmlns:th="http://www.thymeleaf.org"

  lang="en">

<head>

  <meta charset="UTF-8">

  <title>Thymeleaf Demo</title>

</head>

<body>

  <h1 th:text="${pageTitle}">Thymeleaf Demo</h1>

</body>

</html>


5. Run the project

$ mvn spring-boot:run



6. Check


If you see 'Hello Thymeleaf' returned from the Controller in the browser, instead of the default 'Thymeleaf Demo', it means the execution was successful.


评论

此博客中的热门博文