程序员开发实例大全宝库

网站首页 > 编程文章 正文

教你十分钟快速搭建springBoot项目实战 值得一看

zazugpt 2024-10-24 19:31:12 编程文章 19 ℃ 0 评论

教你十分钟快速搭建springBoot项目实战

首先申明,本文并没有原理性的东西(请自行百度),适合于刚接触springBoot并有一定javaweb开发基础的人群,大神和小白请出门左拐。' E# G' |& ?8 j0 |( X; S

一、使用工具, Q/ W# _3 t# g* T2 I+ \

Eclipse、Maven、springBoot8 ]2 K, [. J, w' C$ ?1 {

本文不会讲解Maven的安装使用以及Eclipse下如何创建一个Maven项目

6 G) _& I5 ^. R* {1 t

二、实现功能

1、springBoot项目项目结构

pom.xml 文件:

  • <projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

  • <modelVersion>4.0.0</modelVersion>

  • <groupId>com.acts.java</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.1.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>7 j! f$ ]. l9 j0 U3 y5 a

2、如何启动项目以及配置项目启动端口以及日志级别

  • #服务端口

  • 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=false1 b% m& F9 R O! U) g

3、启动springBoot项目并展示一个页面,对应的是templates下index.html页面。

  • /**

  • * 其实我是个演员

  • * 小柒

  • */

  • @SpringBootApplication

  • @Controller

  • publicclassApp{

  • privatestaticfinalLogger logger=Logger.getLogger(App.class);

  • @RequestMapping("/")

  • publicString greeting(){

  • return"index";

  • }

  • publicstaticvoid main(String[] args){

  • SpringApplication.run(App.class, args);

  • logger.info("项目启动 ");

  • }

  • }6 x6 M9 i3 S; a3 F

4、整合thymeleaf模版实现一个简单的页面,对应的是templates下hello.html页面。

  • @Controller

  • publicclassHelloController{

  • @RequestMapping("/hello")

  • publicString greeting(ModelMap map){

  • map.addAttribute("name","其实我是个演员");

  • map.addAttribute("host","http://blog.52itstyle.com");

  • return"hello";

  • }

  • }) Y/ }0 I8 ~$ b& g

5、如何统一配置404和500页面

  • @Controller

  • publicclassErrorInterceptorimplementsHandlerInterceptor{

  • /**

  • * preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用,SpringMVC中的Interceptor拦截器是链式的,可以同时存在

  • * 多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有的Interceptor中的preHandle方法都会在

  • * Controller方法调用之前调用。SpringMVC的这种Interceptor链式结构也是可以进行中断的,这种中断方式是令preHandle的返

  • * 回值为false,当preHandle的返回值为false的时候整个请求就结束了。

  • */

  • publicboolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler)

  • throwsException{

  • returntrue;// 只有返回true才会继续向下执行,返回false取消当前请求

  • }

  • /**

  • * 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之

  • * 后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行,也就是说在这个方法中你可以对ModelAndView进行操

  • * 作。这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor拦截器该方法反而会后调用,这跟struts2里面的拦截器的执行过程有点像,

  • * 只是Struts2里面的intercept方法中要手动的调用ActionInvocation的invoke方法,Struts2中调用ActionInvocation的invoke方法就是调用下一个Interceptor

  • * 或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前,要在Interceptor之后调用的内容都写在调用invoke方法之后。

  • */

  • publicvoid postHandle(HttpServletRequest request,HttpServletResponse response,Object handler,

  • ModelAndView modelAndView)throwsException{

  • if(response.getStatus()==500){

  • modelAndView.setViewName("/errorpage/500");

  • }elseif(response.getStatus()==404){

  • modelAndView.setViewName("/errorpage/404");

  • }

  • }

  • /**

  • * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行,

  • * 这个方法的主要作用是用于清理资源的,当然这个方法也只能在当前这个Interceptor的preHandle方法的返回值为true时才会执行。

  • */

  • publicvoid afterCompletion(HttpServletRequest request,HttpServletResponse response,Object handler,Exception ex)

  • throwsException{

  • }

享受享福的背后是别人不为知的艰辛

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表