一、静态资源访问
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";
}
}