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。如能显示图片,配置成功。
在之前的示例中,我们都是通过@RestController来处理请求,所以返回的内容为json对象。那么如果需要渲染html页面的时候
现在我们就使用模板引擎来实现
Spring Boot提供了默认配置的模板引擎主要有以下几种:
• Thymeleaf
• FreeMarker
• Velocity
• Groovy
• Mustache
Spring Boot建议使用这些模板引擎,避免使用JSP,
若一定要使用JSP将无法实现Spring Boot的多种特性,具体可见后文:支持JSP的配置,
当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resources/templates。当然也可以修改这个路径,
具体如何修改,可在后续各模板引擎的配置属性中查询并修改。
项目结构:
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
com.day01springBoot
SpringBoot01
1.0-SNAPSHOT
war
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-starter-tomcat
org.apache.tomcat.embed
tomcat-embed-jasper
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 map){
map.put("name","java程序员!");
return "freemarker";
}
}
\src\main\resources\templates\freemarker.ftl(这个.ftl后缀名也可以改。在Freemarker配置文件application.properties里改)
${name}
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 result) {
result.put("name", "ssw");
result.put("sex", "0");
List listResult = new ArrayList();
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);
}
}
首页
${name}
<#if sex=="0">
男
<#elseif sex=="2">
女
<#else>
其他
#if>
<#list listResult as user>
${user}
#list>
#############################################
###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
结构:
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
com.day01springBoot
SpringBoot01
1.0-SNAPSHOT
war
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-starter-tomcat
org.apache.tomcat.embed
tomcat-embed-jasper
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
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";
}
}