再次記錄一下SpringBoot MVC開發環境搭建,高手請略過~~~

新建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

瀏覽器結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章