[置顶] WEB开发----Spring Boot项目快速搭建
2017-08-30 12:27
1056 查看
在上一篇,通过maven创建了一个web项目,Maven项目的搭建
今天在maven项目的基础上将其修改成spring boot项目.
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
之后项目的目录结构是这样的:
页面效果:
添加错误页面资源(spring boot默认资源映射到static目录):
这样写好,如果访问的路径有错,或者别的错误,就是跳转到指定的页面
在pom.xml中添加对jsp的支持
在HomeController中添加一个跳转jsp页面的方法:
在webapp下面创建一个welcome.jsp文件
然后就可以在浏览器访问了
输入
效果:
今天在maven项目的基础上将其修改成spring boot项目.
1 .Spring boot介绍
从最根本上来讲,Spring Boot就是一些库的集合,它能够被任意项目的构建系统所使用。Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
2. spring boot搭建
2.1 spring boot 的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>test-spring-boot</groupId> <artifactId>test-springboot-web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <!-- spring boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> & d9b9 lt;/plugins> </build> </project>
2.2 创建应用程序入口
spring boot 内嵌tomcat服务器,可以通过application应用程序的mian方法启动, 创建一个App.class文件,作为程序的启动点package com; import java.net.InetAddress; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @Configuration //@Configuration注解该类,等价 与XML中配置beans;用@Bean标注方法等价于XML中配置bean @ComponentScan @EnableAutoConfiguration public class App { private static final Logger log = LoggerFactory.getLogger(App.class); public static void main(String[] args) throws Exception { //SpringApplication.run(App.class); SpringApplication app = new SpringApplication(App.class); Environment env = app.run(args).getEnvironment(); String protocol = "http"; if (env.getProperty("server.ssl.key-store") != null) { protocol = "https"; } log.info("\n----------------------------------------------------------\n\t" + "Application '{}' is running! Access URLs:\n\t" + "Local: \t\t{}://localhost:{}\n\t" + "External: \t{}://{}:{}\n\t" + "Profile(s): \t{}\n----------------------------------------------------------", env.getProperty("spring.application.name"), protocol, env.getProperty("server.port"), protocol, InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"), env.getActiveProfiles()); String configServerStatus = env.getProperty("configserver.status"); log.info("\n----------------------------------------------------------\n\t" + "Config Server: \t{}\n----------------------------------------------------------", configServerStatus == null ? "Not found or not setup for this application" : configServerStatus); } }
2.3 编写Hello Spring Boot
作为spring boot 入门,写一个hello欢迎来进行Spring boot项目的测试把.package com.rest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @Controller @RequestMapping("/home") public class HomeController { private static final Logger log = LoggerFactory.getLogger(HomeController.class); @RequestMapping("/hello") @ResponseBody public String home(){ log.info("执行/hello方法.."); return "Hello Spring Boot!"; } }
之后项目的目录结构是这样的:
2.4 测试效果
在浏览器中输入地址:http://127.0.0.1:8080/home/hello页面效果:
3 自定义错误页面
在App.class文件中,加入对错误处理的方法:/** *该方法已过时 */ @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/js/401.html"); ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/js/404.html"); ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/js/500.html"); container.addErrorPages(error401Page, error404Page, error500Page); } }; }
添加错误页面资源(spring boot默认资源映射到static目录):
这样写好,如果访问的路径有错,或者别的错误,就是跳转到指定的页面
4. 访问jsp页面
我在项目中添加一个application.properties文件,修改了一下端口,以及jsp页面的支持,不然不会支持jsp页面server.port=8081 #返回的前缀 目录对应src/main/webapp下 spring.mvc.view.prefix: / #返回的后缀 spring.mvc.view.suffix: .jsp
在pom.xml中添加对jsp的支持
<!-- 对jsp的支持 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
在HomeController中添加一个跳转jsp页面的方法:
@RequestMapping("/welcome") public String goWelcome(){ log.info("执行/welcome方法..跳转到welcome.jsp页面"); return "/welcome"; }
在webapp下面创建一个welcome.jsp文件
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>欢迎</title> </head> <style> html { padding: 30px 10px; font-size: 20px; line-height: 1.4; color: #737373; background: #f0f0f0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; } body { max-width: 550px; _width: 550px; padding: 30px 20px 50px; border: 1px solid #b3b3b3; border-radius: 4px; margin: 0 auto; box-shadow: 0 1px 10px #a7a7a7, inset 0 1px 0 #fff; background: #fcfcfc; } h2 { margin: 0 10px; font-size: 35px; text-align: center; } h4 { margin: 0 10px; font-size: 20px; text-align: center; } .container { max-width: 500px; _width: 500px; margin: 0 auto; } </style> <body> <div class="container"> <h2>Welcome Spring Boot!</h2> <h4>欢迎学习spring boot框架</h4> </div> </body> </html>
然后就可以在浏览器访问了
输入
http://localhost:8081/welcome.jsp 或http://localhost:8081/home/welcome
效果:
相关文章推荐
- JeeFast是一款基于SpringBoot+Mybatis-Plus+Bootstrap+Vue搭建的JAVA WEB快速开发平台
- JeeFast是一款基于SpringBoot+Mybatis-Plus+Bootstrap+Vue搭建的JAVA WEB快速开发平台
- Spring-Boot快速搭建web项目详细总结
- 通过Spring Boot + Mybatis + Redis快速搭建现代化Web项目
- Spring Boot(二):快速搭建web项目或微服务
- Spring Cloud搭建微服务架构----使用Spring boot开发web项目
- Spring Boot快速开发Web项目
- SpringBoot之1分钟快速搭建Web项目(一)
- 【SpringBoot探索一】SpringBoot快速搭建基础web项目
- start.spring.io快速搭建springboot项目页面解析介绍
- 一步步学习SpringBoot(一) 快速搭建一个web
- idea+maven+springboot搭建web简单项目经历小计(RestController)
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- [置顶] WEB开发----Maven项目的搭建
- 快速搭建Spring Boot项目
- IDEA 快速搭建springboot项目,返回HTML首页
- 详解用Spring Boot零配置快速创建web项目
- idea快速搭建springboot项目的操作方法
- 使用Spring Boot开发Web项目
- 教你十分钟快速搭建springBoot项目实战