您的位置:首页 > 编程语言 > Java开发

[置顶] WEB开发----Spring Boot项目快速搭建

2017-08-30 12:27 1056 查看
在上一篇,通过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>
&
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


效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: