教你十分钟快速搭建springBoot项目实战
2017-08-03 00:00
846 查看
首先申明,本文并没有原理性的东西(请自行百度),适合于刚接触springBoot并有一定javaweb开发基础的人群,大神和小白请出门左拐。
##使用工具
Eclipse、Maven、springBoot
本文不会讲解Maven的安装使用以及Eclipse下如何创建一个Maven项目
##实现功能
###springBoot项目项目结构
pom.xml 文件:
###如何启动项目以及配置项目启动端口以及日志级别
###启动springBoot项目并展示一个页面,对应的是templates下index.html页面。
###整合thymeleaf模版实现一个简单的页面,对应的是templates下hello.html页面。
###如何统一配置404和500页面
###最终项目演示
##码云
https://git.oschina.net/52itstyle/springboot_demo
##使用工具
Eclipse、Maven、springBoot
本文不会讲解Maven的安装使用以及Eclipse下如何创建一个Maven项目
##实现功能
###springBoot项目项目结构
pom.xml 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.itstyle</groupId> <artifactId>springBoot_demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springBoot_demo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- spring-boot-starter-parent包含了大量配置好的依赖管理,在自己项目添加这些依赖的时候不需要写<version>版本号 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> </parent> <dependencies> <!-- 实现web功能 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 模版 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- 打包项目 mvn clean package --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <!-- mvn spring-boot:run 热部署启动 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.4.0.RELEASE</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
###如何启动项目以及配置项目启动端口以及日志级别
#服务端口 server.port=8080 #spring boot从控制台打印出来的日志级别只有ERROR, WARN 还有INFO,如果你想要打印debug级别的日志 #debug=true logging.level.root=INFO #logging.level.org.springframework.web=DEBUG #logging.level.org.hibernate=ERROR #thymeleaf start #spring.thymeleaf.prefix: /templates/ #spring.thymeleaf.suffix: .html #spring.thymeleaf.mode=HTML5 #spring.thymeleaf.encoding=UTF-8 #spring.thymeleaf.content-type=text/html #开发时关闭缓存,不然没法看到实时页面 #spring.thymeleaf.cache=false
###启动springBoot项目并展示一个页面,对应的是templates下index.html页面。
/** * 其实我是个演员 * 小柒 */ @SpringBootApplication @Controller public class App { private static final Logger logger = Logger.getLogger(App.class); @RequestMapping("/") public String greeting() { return "index"; } public static void main(String[] args) { SpringApplication.run(App.class, args); logger.info("项目启动 "); } }
###整合thymeleaf模版实现一个简单的页面,对应的是templates下hello.html页面。
@Controller public class HelloController { @RequestMapping("/hello") public String greeting(ModelMap map) { map.addAttribute("name", "其实我是个演员"); map.addAttribute("host", "https://blog.52itstyle.com"); return "hello"; } }
###如何统一配置404和500页面
@Controller public class ErrorInterceptor implements HandlerInterceptor { /** * preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用,SpringMVC中的Interceptor拦截器是链式的,可以同时存在 * 多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有的Interceptor中的preHandle方法都会在 * Controller方法调用之前调用。SpringMVC的这种Interceptor链式结构也是可以进行中断的,这种中断方式是令preHandle的返 * 回值为false,当preHandle的返回值为false的时候整个请求就结束了。 */ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true;// 只有返回true才会继续向下执行,返回false取消当前请求 } /** * 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之 * 后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行,也就是说在这个方法中你可以对ModelAndView进行操 * 作。这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor拦截器该方法反而会后调用,这跟Struts2里面的拦截器的执行过程有点像, * 只是Struts2里面的intercept方法中要手动的调用ActionInvocation的invoke方法,Struts2中调用ActionInvocation的invoke方法就是调用下一个Interceptor * 或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前,要在Interceptor之后调用的内容都写在调用invoke方法之后。 */ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { if(response.getStatus()==500){ modelAndView.setViewName("/errorpage/500"); }else if(response.getStatus()==404){ modelAndView.setViewName("/errorpage/404"); } } /** * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行, * 这个方法的主要作用是用于清理资源的,当然这个方法也只能在当前这个Interceptor的preHandle方法的返回值为true时才会执行。 */ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
###最终项目演示
##码云
https://git.oschina.net/52itstyle/springboot_demo
相关文章推荐
- 4000 教你十分钟快速搭建springBoot项目实战
- SpringBoot(一)_快速实战搭建项目
- 快速搭建spring-boot的HelloWord项目
- spring-boot(2) - RESTfull API简单项目的快速搭建
- spring-boot实战【01】:Hello World项目搭建
- Spring Boot(二):快速搭建web项目或微服务
- 快速搭建SpringBoot项目
- 【Spring Boot教程】快速搭建spring boot项目工程
- 整合阿里dubbo服务的项目搭建实战(springboot)
- springboot入门,简单能跑的项目快速搭建
- 快速搭建Spring Boot项目
- 构建微服务:快速搭建Spring Boot项目
- 【SpringBoot教程二】:RESTfull API简单项目的快速搭建
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(二)
- idea快速搭建springboot项目的操作方法
- 快速搭建springboot项目并整合easymybatis
- start.spring.io快速搭建springboot项目页面解析介绍
- SpringBoot1-spirng项目快速搭建+maven+spring的基本配置
- 快速搭建一个简单的springboot项目
- spring-boot实战【01】:Hello World项目搭建