在咱们开发Web应用的时候,须要引用大量的js、css、图片等静态资源。css
默认配置html
Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合以下规则:java
/staticweb
/publicspring
/resourcesapache
/META-INF/resourcesjson
举例:咱们能够在src/main/resources/目录下建立static,在该位置放置一个图片文件。启动程序后,尝试访问http://localhost:8080/D.jpg。如能显示图片,配置成功。tomcat
渲染Web页面session
在以前的示例中,咱们都是经过@RestController来处理请求,因此返回的内容为json对象。那么若是须要渲染html页面的时候,要如何实现呢?mvc
模板引擎
在动态HTML实现上Spring Boot依然能够完美胜任,而且提供了多种模板引擎的默认配置支持,因此在推荐的模板引擎下,咱们能够很快的上手开发动态网站。
Spring Boot提供了默认配置的模板引擎主要有如下几种:
Spring Boot建议使用这些模板引擎,避免使用JSP,若必定要使用JSP将没法实现Spring Boot的多种特性,具体可见后文:支持JSP的配置
当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resources/templates。固然也能够修改这个路径,具体如何修改,可在后续各模板引擎的配置属性中查询并修改。
<!-- 引入freeMarker的依赖包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
在src/main/resources/建立一个templates文件夹,后缀为*.ftl
@RequestMapping("/index") public String index(Map<String, Object> map) { map.put("name","hello..."); return "index"; }
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title></title> </head> <body> ${name} </body> </html>
@RequestMapping("/freemarkerIndex") public String index(Map<String, Object> result) { result.put("name", "yushengjun"); result.put("sex", "0"); List<String> listResult = new ArrayList<String>(); listResult.add("zhangsan"); listResult.add("lisi"); listResult.add("hello"); result.put("listResult", listResult); return "index"; } <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title>首页</title> </head> <body> ${name} <#if sex=="1"> 男 <#elseif sex=="2"> 女 <#else> 其余 </#if> <#list userlist as user> ${user} </#list> </body> </html>
新建application.properties文件
这里使用properties配置文件,yml文件后面会写
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.prefix= #spring.freemarker.request-context-attribute= #spring.freemarker.settings.*= 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
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <!-- SpringBoot web 核心组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!-- SpringBoot 外部tomcat支持 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> </dependencies>
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
@Controller public class IndexController { @RequestMapping("/index") public String index() { return "index"; } }
注意:建立SpringBoot整合JSP,必定要为war类型,不然会找不到页面.
不要把JSP页面存放在resources// jsp 不能被访问到
模板引擎这块会单独写,期待关注!
@ExceptionHandler 表示拦截异常
@ControllerAdvice 是 controller 的一个辅助类,最经常使用的就是做为全局异常处理的切面类
@ControllerAdvice 能够指定扫描范围
@ControllerAdvice 约定了几种可行的返回值,若是是直接返回 model 类的话,须要使用 @ResponseBody 进行 json 转换
返回 String,表示跳到某个 view
返回 modelAndView
返回 model + @ResponseBody
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(RuntimeException.class) @ResponseBody public Map<String, Object> exceptionHandler() { Map<String, Object> map = new HashMap<String, Object>(); map.put("errorCode", "101"); map.put("errorMsg", "系統错误!"); return map; } }