1. 程式人生 > >SpringBoot--02.SpringBoot2.0搭建Web專案

SpringBoot--02.SpringBoot2.0搭建Web專案

一、靜態資源訪問

SpringBoot入門參考https://blog.csdn.net/sswqzx/article/details/84560202

SpringBoot提供 了靜態資源預設配置:

Spring Boot預設提供靜態資源目錄位置需置於classpath下,目錄名需符合如下規則:
/static
/public
/resources	
/META-INF/resources

舉例:我們可以在src/main/resources/目錄下建立static,在該位置放置一個圖片檔案。啟動程式後,嘗試訪問http://localhost:8080/test.jpg。如能顯示圖片,配置成功。

二、渲染Web頁面

在之前的示例中,我們都是通過@RestController來處理請求,所以返回的內容為json物件。那麼如果需要渲染html頁面的時候

現在我們就使用模板引擎來實現

Spring Boot提供了預設配置的模板引擎主要有以下幾種:

•	Thymeleaf
•	FreeMarker
•	Velocity
•	Groovy
•	Mustache
Spring Boot建議使用這些模板引擎,避免使用JSP,
若一定要使用JSP將無法實現Spring Boot的多種特性,具體可見後文:支援JSP的配置,
當你使用上述模板引擎中的任何一個,它們預設的模板配置路徑為:src/main/resources/templates。當然也可以修改這個路徑,
具體如何修改,可在後續各模板引擎的配置屬性中查詢並修改。

三、使用Freemarker模板渲染web檢視

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>

    <!--springBoot父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>


    <groupId>com.day01springBoot</groupId>
    <artifactId>SpringBoot01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <!--jdk版本-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--web啟動器-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 引入freeMarker的依賴包. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!--jsp渲染檢視-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

    </dependencies>

</project>

2、後臺程式碼

application.java

package com.springboot;

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

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:52 2018/11/27
 */

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

HelloController.java

package com.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:53 2018/11/27
 */
@Controller
public class HelloController {

    @RequestMapping("/index")
    public String testFreemarker(Map<String, Object> map){
        map.put("name","java程式設計師!");
        return "freemarker";
    }
}

 

3、前臺程式碼

\src\main\resources\templates\freemarker.ftl(這個.ftl字尾名也可以改。在Freemarker配置檔案application.properties裡改)

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
${name}
</body>
</html>

 

4、Freemarker遍歷集合

HelloController.java

package com.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:53 2018/11/27
 */
@Controller
public class HelloController {

    @RequestMapping("/freemarkerIndex")
    public String index(Map<String, Object> result) {
        result.put("name", "ssw");
        result.put("sex", "0");
        List<String> listResult = new ArrayList<String>();
        listResult.add("zhangsan");
        listResult.add("lisi");
        listResult.add("linux");
        result.put("listResult", listResult);
        return "other";
    }
}

application.java

package com.springboot;

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

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:52 2018/11/27
 */

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

other.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title>首頁</title>
</head>
<body>
${name}
<#if sex=="0">
            男
<#elseif sex=="2">
            女
<#else>
        其他

</#if>
	 <#list listResult as user>
         ${user}
     </#list>
</body>
</html>

5、Freemarker配置檔案

#############################################
###FREEMARKER (FreeMarkerAutoConfiguration)
#############################################
#Freemarker配置
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.prefix=
#spring.freemarker.suffix=.ftl
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/
#comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved

四、使用Jsp渲染web檢視(不用)

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>

    <!--springBoot父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>


    <groupId>com.day01springBoot</groupId>
    <artifactId>SpringBoot01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <!--jdk版本-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--web啟動器-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 引入freeMarker的依賴包. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!--jsp渲染檢視-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

    </dependencies>

</project>

 

2、配置Freemarker的配置檔案application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

3、後臺程式碼

application.java

package com.springboot;

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

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:52 2018/11/27
 */

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

HelloController.java

package com.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:53 2018/11/27
 */
@Controller
public class HelloController {

    @RequestMapping("/jspindex")
    public String jspadmin(){
        return "admin";
    }
}