环境
  Java1.8
  Spring Boot 1.3.2

一、静态资源访问

动静分离:动态服务和前台页面图片分开,静态资源可以使用CDN加速;
Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:
/static
/public
/resources
/META-INF/resources
举例:我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件。启动程序后,尝试访问http://localhost:8080/d.jpg。如能显示图片,配置成功。

二、全局异常捕获
首先定义一个切面处理类 用来处理运行时异常报错:

  1. package com.wjy.controller;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.springframework.web.bind.annotation.ControllerAdvice;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9.  
  10. @ControllerAdvice
  11. public class GlobalExceptionHandler {
  12.  
  13. @ExceptionHandler(RuntimeException.class)
  14. @ResponseBody
  15. public Map<String, Object> resultError() {
  16. Map<String, Object> result = new HashMap<String, Object>();
  17. result.put("errorCode", "500");
  18. result.put("errorMsg", "系统错误!");
  19. return result;
  20. }
  21.  
  22. }

@ControllerAdvice 是controller的一个辅助类,最常用的就是作为全局异常处理的切面类;
@ControllerAdvice 可以指定扫描范围;
@ControllerAdvice 约定了几种可行的返回值:
  返回 String,表示跳到某个 view
  返回 modelAndView
  返回 model + @ResponseBody:将model类转换成json格式

然后写一个方法抛出异常:

  1. package com.wjy.controller;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9.  
  10. @EnableAutoConfiguration
  11. @RestController
  12. public class HelloController {
  13.  
  14. @RequestMapping("/hello")
  15. public String hello() {
  16. return "hello world";
  17. }
  18.  
  19. @RequestMapping("/getMap")
  20. public Map<String, Object> getMap() {
  21. Map<String, Object> result = new HashMap<String, Object>();
  22. result.put("errorCode", "200");
  23. result.put("errorMsg", "成功..");
  24. int a = 1/0;
  25. return result;
  26. }
  27.  
  28. }

验证:

三、渲染WEB页面
Spring Boot整合页面建议优先选择模板引擎,不建议选用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性。
模板引擎:使用伪HTML作动态页面模板,方便开发,另外提高搜索引擎搜索,Spring Boot提供了默认配置的模板引擎主要有以下几种:
• Thymeleaf
• FreeMarker
• Velocity
• Groovy
• Mustache
当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resources/templates。

1、使用Freemaker渲染WEB视图
(1)POM引入依赖

  1. <!-- 引入freeMarker的依赖包. -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-freemarker</artifactId>
  5. </dependency>

(2)在src/main/resources/创建一个templates文件夹,里面创建动态页面模板,后缀为*.ftl
比如:index.ftl

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8" />
  5. <title>首页</title>
  6. </head>
  7. <body>
  8. ${name}
  9. <#if sex==1>

  10. <#elseif sex==2>

  11. <#else>
  12. 其他
  13.  
  14. </#if>
  15. <#list userlist as user>
  16. ${user}
  17. </#list>
  18. </body>
  19. </html>

(3)创建后台代码

  1. package com.wjy.controller;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9.  
  10. //注意这里是 @Controller 不是@RestController
  11. @Controller
  12. public class FmindexController {
  13.  
  14. @RequestMapping("/fmindex")
  15. public String index(Map<String, Object> result) {
  16. System.out.println("IndexController....index");
  17. result.put("name", "yushengjun");
  18. result.put("sex", 1);
  19. List<String> list = new ArrayList<String>();
  20. list.add("zhangsan");
  21. list.add("lisi");
  22. result.put("userlist", list);
  23. return "index";
  24. }
  25.  
  26. }

注意:ftl的名字要和java 返回字符串的名字相同

验证:

(4)freemaker配置
使用application.properties文件,默认在src/main/resources目录下

  1. ########################################################
  2. ###FREEMARKER (FreeMarkerAutoConfiguration)
  3. ########################################################
  4. spring.freemarker.allow-request-override=false
  5. spring.freemarker.cache=true
  6. spring.freemarker.check-template-location=true
  7. spring.freemarker.charset=UTF-8
  8. spring.freemarker.content-type=text/html
  9. spring.freemarker.expose-request-attributes=false
  10. spring.freemarker.expose-session-attributes=false
  11. spring.freemarker.expose-spring-macro-helpers=false
  12. #spring.freemarker.prefix=
  13. #spring.freemarker.request-context-attribute=
  14. #spring.freemarker.settings.*=
  15. spring.freemarker.suffix=.ftl
  16. spring.freemarker.template-loader-path=classpath:/templates/
  17. #comma-separated list
  18. #spring.freemarker.view-names= # whitelist of view names that can be resolved

2、使用JSP渲染WEB视图
(1)maven项目必须是war类型   eclipse  4.7  +  jdk1.8

然后依次修改如下配置:

(1.1)在 Java Build Path的libraries中修改为  jdk1.8
(1.2)在Java Compiler 中修改  修改为1.8
(1.3)在Project Facet中修改   Dynamic Web Module 2.5 ---> 3.1      java 1.5---->1.8

(2)POM引入依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.3.3.RELEASE</version>
  5. </parent>
  6. <dependencies>
  7. <!-- SpringBoot 核心组件 -->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-web</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-tomcat</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.apache.tomcat.embed</groupId>
  18. <artifactId>tomcat-embed-jasper</artifactId>
  19. </dependency>
  20. <!-- 强制添加依赖 否则可能在解析JSP报错:No Java compiler available -->
  21. <dependency>
  22. <groupId>org.eclipse.jdt.core.compiler</groupId>
  23. <artifactId>ecj</artifactId>
  24. <version>4.6.1</version>
  25. <scope>provided</scope>
  26. </dependency>
  27. </dependencies>
  28.  
  29. <!-- 不加下面这个build 可能update project不起作用 -->
  30. <build>
  31. <plugins>
  32. <plugin>
  33. <groupId>org.apache.maven.plugins</groupId>
  34. <artifactId>maven-compiler-plugin</artifactId>
  35. <version>3.1</version>
  36. <configuration>
  37. <source>1.8</source>
  38. <target>1.8</target>
  39. </configuration>
  40. </plugin>
  41. </plugins>
  42. </build>

最后  maven  -->  Update Project...

(3)application.properties创建以下配置

  1. spring.mvc.view.prefix=/WEB-INF/jsp/
  2. spring.mvc.view.suffix=.jsp

(4)代码

页面:

  1. <%@ page language="java" contentType="text/html; UTF-8" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>Insert title here</title>
  7. </head>
  8. <body>
  9. Spring Boot 整合 JSP
  10. </body>
  11. </html>

后台:

  1. package com.wjy.app;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. import org.springframework.context.annotation.ComponentScan;
  6.  
  7. @EnableAutoConfiguration
  8. @ComponentScan(basePackages="com.wjy.controller")
  9. public class App {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(App.class, args);
  13. }
  14.  
  15. }
  1. package com.wjy.controller;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5.  
  6. @Controller
  7. public class IndexController {
  8.  
  9. @RequestMapping("/index")
  10. public String index() {
  11. return "index";
  12. }
  13. }

验证:

【Spring Boot学习之二】WEB开发的更多相关文章

  1. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  2. Spring Boot学习笔记二

    Spring Boot入门第二篇 第一天的详见:https://www.cnblogs.com/LBJLAKERS/p/12001253.html 同样是新建一个pring Initializer快速 ...

  3. Spring Boot学习(二):配置文件

    目录 前言 方式1:通过配置绑定对象的方式 方式2:@Value("${blog.author}")的形式获取属性值 相关说明 注解@Value的说明 参考 前言 Spring B ...

  4. spring Boot 学习(二、Spring Boot与缓存)

    一.概述1. 大多应用中,可通过消息服务中间件来提升系统异步通信.扩展解耦能力 2. 消息服务中两个重要概念: 消息代理(message broker)和目的地(destination) 当消息发送者 ...

  5. Spring Boot学习笔记:项目开发中规范总结

    Spring Boot在企业开发中使用的很广泛,不同的企业有不同的开发规范和标准.但是有些标准都是一致的. 项目包结构 以下是一个项目常见的包结构 以上是一个项目的基本目录结构,不同的项目结构会有差异 ...

  6. Spring Boot学习笔记(二二) - 与Mybatis集成

    Mybatis集成 Spring Boot中的JPA部分默认是使用的hibernate,而如果想使用Mybatis的话就需要自己做一些配置.使用方式有两种,第一种是Mybatis官方提供的 mybat ...

  7. Spring Boot学习(二)搭建一个简易的Spring Boot工程

    第一步:新建项目 新建一个SpringBoot工程 修改项目信息 勾选项目依赖和工具 选择好项目的位置,点击[Finish] 第二步:项目结构分析 新建好项目之后的结构如下图所示,少了很多配置文件: ...

  8. Spring Boot学习总结二

    Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化.除此之外,Redis还提供一些类 ...

  9. spring boot 学习笔记(二) 构建web支持jsp

    一.必须将项目打包成war包 <packaging>war</packaging> 二.pom.xml加入依赖包 <dependency> <groupId& ...

随机推荐

  1. wordpress去掉category的另一个方法

    今天ytkah的客户问wordpress网站一直去不掉分类url中的/category/,他说已经按ytkah之前的方法设置了还是不起作用,进入网站后台发现,他们的网站有安装yoast,然后就大概知道 ...

  2. 装饰器vue-property-decorator

    接触到了新的vue项目,使用vue+ts+vue-property-decotator来进行项目的简化,一时间语法没有看懂,所以花时间学习这个装饰器的包. 1.装饰器 @Component(optio ...

  3. cortex-m系列的区别(图解)及今日碎片学习笔记

    下图转自https://www.cnblogs.com/luckytimor/p/6747026.html 该系列的结构都是哈佛结构,而且目前可以使用keil来开发,而且keil mdk不能开发R.A ...

  4. Js 日期字符串分别截取 年 月 日 时 分 秒

    function shijiantime(times){ var timearr = times.replace(" ", ":").replace(/\:/g ...

  5. 17-Flutter移动电商实战-首页_楼层区域的编写

    1.楼层标题组件 该组件非常简单,只接收一个图片地址,然后显示即可: class FloorTitle extends StatelessWidget {  final String picture_ ...

  6. Xamarin NuGet 缓存包导致 already added : Landroid/support/annotation/AnimRes 问题解决方案

    在VS中打开您遇到问题的解决方案. 转到工具> NuGet包管理器>包管理器设置 - >常规,然后点击“清除所有NuGet缓存” 这应该返回一个错误,因为你有一个项目打开,但如果没有 ...

  7. 【luoguP2994】[USACO10OCT]吃晚饭的时候Dinner Time

    题目链接 按顺序对于每个座位找一个最近的同时编号最小的牛就行了 #include<iostream> #include<cstring> #include<cstdio& ...

  8. nginx 配置ssl

    单向SSL配置实例: server{ listen ssl; server_name www..com; root /data/wwwroot/www..com/ ; index index.html ...

  9. Pymysql+Pandas+Sqlalchemy数据库更新脚本编写

    #导入需要的包,使用pymysql操作数据库 #pandas包很强大,可以直接读取数据表,创建dataframe数据结构,同时可以直接将dataframe导出到数据库 #sqlalchemy创建引擎, ...

  10. Fluent的summary功能

    在Fluent计算当中,出现错误,大家经常在求助的时候问得很笼统和宽泛,这里介绍一下Fluent的summary功能,大家可以在求助的时候附上生成的文件,这样更加便于别人帮助你发现问题 然后在算例目录 ...