spring boot之web开发(二)

来源:互联网 发布:淘宝u站管理中心 编辑:程序博客网 时间:2024/05/29 09:44

1、静态资源访问

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

默认配置

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

/static

/public

/resources

/META-INF/resources

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

2、全局捕获异常

@ExceptionHandler表示拦截异常

·      @ControllerAdvice 是 controller 的一个辅助类,最常用的就是作为全局异常处理的切面类

·      @ControllerAdvice 可以指定扫描范围

·      @ControllerAdvice 约定了几种可行的返回值,如果是直接返回 model 类的话,需要使用 @ResponseBody 进行 json 转换

o   返回 String,表示跳到某个 view

o   返回 modelAndView

o   返回 model +@ResponseBody

 

@ControllerAdvice

publicclass GlobalExceptionHandler {

     @ExceptionHandler(RuntimeException.class)

     @ResponseBody

     publicMap<String, Object> exceptionHandler() {

           Map<String, Object> map = new HashMap<String, Object>();

           map.put("errorCode","101");

           map.put("errorMsg","系統错误!");

           returnmap;

     }

}

 

3、渲染Web页面

 

渲染Web页面

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

模板引擎

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

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

·      Thymeleaf

·      FreeMarker

·      Velocity

·      Groovy

·      Mustache

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

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

4、使用Freemarker模板引擎渲染web视图

4.1、pom文件引入:

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

<dependency>

<groupId>org.springframework.boot</groupId>

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

</dependency>

4.2、后台代码

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

     @RequestMapping("/index")

      public String index(Map<String, Object>map) {

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

            return"index";

      }

 

4.3、前台代码

 

<!DOCTYPE html>

<html>

<head lang="en">

<meta charset="UTF-8" />

<title></title>

</head>

<body>

        ${name}

</body>

</html>

4.4、Freemarker其他用法

@RequestMapping("/index")

      public String index(Map<String, Object>map) {

          map.put("name","###蚂蚁课堂###");

          map.put("sex",1);

          List<String> userlist=new ArrayList<String>();

          userlist.add("余胜军");

          userlist.add("张三");

          userlist.add("李四");

          map.put("userlist",userlist);

          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>

 

4.5、Freemarker配置

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

 

5、使用JSP渲染Web视图

5.1、pom文件引入以下依赖

<parent>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-starter-parent</artifactId>

           <version>1.3.3.RELEASE</version>

     </parent>

     <dependencies>

           <!-- SpringBoot核心组件 -->

           <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-web</artifactId>

           </dependency>

      <!-- tomcat 的支持.-->

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

<!--JSTLJSP Standard Tag LibraryJSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apachejakarta小组来维护的。-->

<dependency>

  <groupId>javax.servlet</groupId>

  <artifactId>jstl</artifactId>

  </dependency>

<!-- servlet 依赖.-->

  <dependency>

  <groupId>javax.servlet</groupId>

  <artifactId>javax.servlet-api</artifactId>

  <scope>provided</scope>

  </dependency>

 </dependencies>

 

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

# 页面默认前缀目录

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

# 响应页面默认后缀

spring.mvc.view.suffix=.jsp

# 自定义属性,可以在Controller中读取

application.hello=HelloAngel From application


 

5.3、后台代码

@Controller

publicclass IndexController {

      @RequestMapping("/index")

      public String index() {

            return"index";

      }

}

原创粉丝点击