再次记录一下SpringBoot MVC开发环境搭建,高手请略过~~~
程序员文章站
2022-05-29 19:35:55
...
新建SpringBoot工程:
添加依赖包(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 https://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.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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>
<!-- 如果不是Spring framwork或纯java框架,要引入Junit包,且mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<!-- 模板 -->
<!-- 访问html文件依赖begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 访问html文件依赖begin -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编写控制器:
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
//@RestController //这种方式将返回纯字符串
@Controller
public class HelloController {
//所有未定义路由定位(禁止该种写法使用,否则资源都变成返回的内容)
//@RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody String say() {
return "say:Hello World";
}
@RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody String sayHello() {
return "Hello World";
}
@RequestMapping(value = "/plot", method = RequestMethod.GET)
public String sayPlot(Model model) {
model.addAttribute("name","zhangsan");
model.addAttribute("age","28");
return "plot";
}
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
System.out.println("------ Hello Spring Boot Project!!! ------");
SpringApplication.run(DemoApplication.class, args);
}
}
准备模板、资源文件等:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Total fatalities by scale</title>
<script src="/plotly-latest.min.js"></script>
</head>
<body>
<div th:text="'用户姓名:' + ${name}" />
<div th:text="'年龄:' + ${age}" />
<div id='target' ></div>
<script>
var target_target = document.getElementById('target');
var layout = {
title: 'Total fatalities by scale',
height: 450,
width: 700,
};
var trace0 = {
labels: ['0','2','1','3','5','4','-9'],
values: [27302.0, 9114.0, 20057.0, 2624.0, 88.0, 703.0, 57.0],
type: 'pie',
name: '',
}
var data = [ trace0];
Plotly.newPlot(target_target, data, layout);
</script>
</body>
</html>
配置模板引擎(application.properties):
# 模板配置
# 这个开发配置为false,避免改了模板还要重启服务器
spring.thymeleaf.cache=false
# 这个是配置模板路径的,默认就是templates,可不用配置
spring.thymeleaf.prefix=classpath:/templates/
# 这个可以不配置,检查模板位置
spring.thymeleaf.check-template-location=true
# 下面3个不做解释了,可以不配置
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
# 模板的模式
spring.thymeleaf.mode=HTML
编译打包、运行:
mvn clean package
java -jar target\demo--.jar
浏览器结果: