springboot2.0(二)

2022-10-13

三、 web开发

3.1静态资源访问

在我们开发web应用的时候,需要引用大量的js、css、图片等静态资源。

默认配置

spring boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:

/static

/public

/resources

/meta-inf/resources

举例:我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件。启动程序后,尝试访问http://localhost:8080/d.jpg。如能显示图片,配置成功。

3.2、渲染web页面

 

渲染web页面

在之前的示例中,我们都是通过@restcontroller来处理请求,所以返回的内容为json对象。那么如果需要渲染html页面的时候,要如何实现呢?

模板引擎

在动态html实现上spring boot依然可以完美胜任,并且提供了多种模板引擎的默认配置支持,所以在推荐的模板引擎下,我们可以很快的上手开发动态网站。

spring boot提供了默认配置的模板引擎主要有以下几种:

  • thymeleaf
  • freemarker
  • velocity
  • groovy
  • mustache

spring boot建议使用这些模板引擎,避免使用jsp,若一定要使用jsp将无法实现spring boot的多种特性,具体可见后文:支持jsp的配置

当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resources/templates。当然也可以修改这个路径,具体如何修改,可在后续各模板引擎的配置属性中查询并修改。

3.3使用freemarker模板引擎渲染web视图

3.3.1pom文件引入

<!-- 引入freemarker的依赖包. -->

<dependency>

<groupid>org.springframework.boot</groupid>

<artifactid>spring-boot-starter-freemarker</artifactid>

</dependency>

3.3.2、后台代码

src/main/resources/创建一个templates文件夹,后缀*.ftl

@requestmapping("/index")

public string index(map<string, object> map) {

    map.put("name","美丽的天使...");

   return "index";

}

 

3.3.3、前台代码

 

<!doctype html>

<html>

<head lang="en">

<meta charset="utf-8" />

<title></title>

</head>

<body>

  ${name}

</body>

</html>

3.3.4freemarker其他用法

@requestmapping("/freemarkerindex")

public string index(map<string, object> result) {

result.put("name", "xiaohong");

result.put("sex", "0");

list<string> listresult = new arraylist<string>();

listresult.add("zhangsan");

listresult.add("lisi");

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>

 

3.3.5freemarker配置

新建application.properties文件

########################################################

###freemarker (freemarkerautoconfiguration)

########################################################

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

 

3.4、使用jsp渲染web视图

3.4.1pom文件引入以下依赖

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

 

3.4.2、在application.properties创建以下配置

spring.mvc.view.prefix=/web-inf/jsp/

spring.mvc.view.suffix=.jsp

 

3.4.3、后台代码

@controller

public class indexcontroller {

@requestmapping("/index")

public string index() {

return "index";

}

}

 

 

 

 

注意:创建springboot整合jsp,一定要为war类型,否则找不到页面.

不要把jsp页面存放在resources// jsp 不能被访问到

 

 

 

 

 

 

 

 

 

 

3.5全局捕获异常

@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;

}

}

《springboot2.0(二).doc》

下载本文的Word格式文档,以方便收藏与打印。