WEB开发----Spring Boot项目快速搭建
来源:互联网 发布:二元期权数据 编辑:程序博客网 时间:2024/05/19 09:49
在上一篇,通过maven创建了一个web项目,Maven项目的搭建
今天在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> </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 @EnableAutoConfigurationpublic 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
效果:
- WEB开发----Spring Boot项目快速搭建
- 快速搭建Spring Boot项目
- Spring Boot 项目快速搭建
- Spring Boot开发Web项目
- Spring Boot 开发web 项目
- 【Spring Boot】使用Spring Boot来搭建Java web项目以及开发过程
- 【Spring Boot教程】快速搭建spring boot项目工程
- MyBatis + spring boot 搭建web项目
- Spring Boot(二):快速搭建web项目或微服务
- Spring boot快速搭建
- Spring boot 快速搭建
- Spring boot快速搭建
- Spring Boot集成MyBatis开发Web项目
- 使用Spring Boot开发Web项目
- 使用Spring Boot开发Web项目
- 使用Spring Boot开发Web项目
- 使用Spring Boot开发Web项目
- 使用Spring Boot开发Web项目
- Maven之Failure: Address already in use: bind-yellowcong
- lua 正则表达式
- 树的存储结构(数据结构)
- 贝叶斯公式,最大似然和最大后验
- python 统计文件行数
- WEB开发----Spring Boot项目快速搭建
- SpringBoot简介-SpringBoot是什么可以做什么
- 数据结构链表基本操作
- socket详解
- 用友U8/T3各版本下载
- 比较详细Python正则表达式操作指南(re使用)
- 把JFinal项目上传到服务器
- Google Guava API学习笔记(1):基础
- windows 平台g++、gcc、gdb编译环境搭建及使用示范【详细步骤】