继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Spring Boot Simple MVC View

holdtom
关注TA
已关注
手记 1846
粉丝 240
获赞 991


1.pom.xml文件

文件名: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>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.1.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.huinongtx</groupId>

<artifactId>springboot2</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>springboot2</name>

<description>Demo project for Spring Boot</description>

<properties>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

2.配置文件

配置文件名称:application.properties

配置文件内容:

server.port=8081

spring.thymeleaf.cache=false

spring.thymeleaf.enabled=true

spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.suffix=.html

spring.application.name=springboot2

3.控制器

包名:com.huinongtx.springboot2

类名:SimpleController

类内容:

package com.huinongtx.springboot2.controller;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

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

/**

* Created by dengdashuai on 2018/12/10.

*/

@Controller

public class SimpleController {

    @Value("${spring.application.name}")

    String appName;

    @GetMapping("/")

    public String homePage(Model model){

        model.addAttribute("appName",appName);

        return "home";

    }

}

4.视图

目录名:templates

文件名:home.html

文件内容:

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Home Page</title>

</head>

<body>

    <h1>Hello !</h1>

    <p>Welcome to <span th:text="${appName}">Our App</span></p>

</body>

</html>

5.应用入口

类名:Springboot2Application

类内容:

package com.huinongtx.springboot2;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class Springboot2Application {

public static void main(String[] args) {

SpringApplication.run(Springboot2Application.class, args);

}

}

6.项目目录结构

7.启动服务并访问

http://localhost:8081/

QQ截图20181210152249.png

©著作权归作者所有:来自51CTO博客作者素颜猪的原创作品,如需转载,请注明出处,否则将追究法律责任


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP