Spring Boot(二)应用实例
Spring Boot(二)
一 、 Spring Boot Web 应用实例
- 这是一个Spring Boot web应用程序示例,使用嵌入式Tomcat + JSP模板,并将包作为可执行文件WAR文件发布。
- 使用到的技术如下:
- Spring Boot 1.4.2.RELEASE
- Spring 4.3.4.RELEASE
- Tomcat Embed 8.5.6
- Maven 3
- Java 8
- 项目目录
- 在这个示例中,是使用 Eclipse 来创建一个 Maven 项目,打开 Eclipse ,按照以下步骤:File -> New -> Other… 如下所示:
- 最终创建以下文件夹目录结构如下所示:
项目依赖
在POM.xml文件中有相应的注释
<?xml version="1.0" encoding="UTF-8"?> <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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>spring-boot-web-jsp</artifactId><packaging>war</packaging><name>Spring Boot Web JSP Example</name><description>Spring Boot Web JSP Example</description><url>http://www.yiibai.com</url><version>1.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.2.RELEASE</version></parent><properties><java.version>1.8</java.version></properties><dependencies><!-- This is a web application --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Tomcat embedded container--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><!-- JSTL for JSP --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></dependency><!-- Need this to compile JSP --><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><scope>provided</scope></dependency><!-- Need this to compile JSP,tomcat-embed-jasper version is not working, no idea why --><dependency><groupId>org.eclipse.jdt.core.compiler</groupId><artifactId>ecj</artifactId><version>4.6.1</version><scope>provided</scope></dependency><!-- Optional, test for static content, bootstrap CSS--><dependency><groupId>org.webjars</groupId><artifactId>bootstrap</artifactId><version>3.3.7</version></dependency></dependencies><build><plugins><!-- Package as an executable jar/war --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> </project>
执行后 , 显示项目依赖关系:
F:\worksp\springboot\springboot-jsp> mvn dependency:tree[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Spring Boot Web JSP Example 1.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ spring-boot-web-jsp --- [INFO] org.springframework.boot:spring-boot-web-jsp:war:1.0 [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.4.2.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.4.2.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot:jar:1.4.2.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.4.2.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:1.4.2.RELEASE:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.1.7:compile [INFO] | | | | +- ch.qos.logback:logback-core:jar:1.1.7:compile [INFO] | | | | \- org.slf4j:slf4j-api:jar:1.7.21:compile [INFO] | | | +- org.slf4j:jcl-over-slf4j:jar:1.7.21:compile [INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.21:compile [INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.21:compile [INFO] | | +- org.springframework:spring-core:jar:4.3.4.RELEASE:compile [INFO] | | \- org.yaml:snakeyaml:jar:1.17:runtime [INFO] | +- org.hibernate:hibernate-validator:jar:5.2.4.Final:compile [INFO] | | +- javax.validation:validation-api:jar:1.1.0.Final:compile [INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile [INFO] | | \- com.fasterxml:classmate:jar:1.3.3:compile [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.4:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.4:compile [INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.8.4:compile [INFO] | +- org.springframework:spring-web:jar:4.3.4.RELEASE:compile [INFO] | | +- org.springframework:spring-aop:jar:4.3.4.RELEASE:compile [INFO] | | +- org.springframework:spring-beans:jar:4.3.4.RELEASE:compile [INFO] | | \- org.springframework:spring-context:jar:4.3.4.RELEASE:compile [INFO] | \- org.springframework:spring-webmvc:jar:4.3.4.RELEASE:compile [INFO] | \- org.springframework:spring-expression:jar:4.3.4.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.4.2.RELEASE:provided [INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.6:provided [INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.6:provided [INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.6:provided [INFO] +- javax.servlet:jstl:jar:1.2:compile [INFO] +- org.apache.tomcat.embed:tomcat-embed-jasper:jar:8.5.6:provided [INFO] +- org.eclipse.jdt.core.compiler:ecj:jar:4.6.1:provided [INFO] \- org.webjars:bootstrap:jar:3.3.7:compile [INFO] \- org.webjars:jquery:jar:1.11.1:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.327 s [INFO] Finished at: 2017-01-21 T16:57:00+08:00 [INFO] Final Memory: 20M/309M [INFO] ------------------------------------------------------------------------
Spring Boot
这个SpringBootServletInitializer执行传统的WAR部署运行SpringApplication SpringBootWebApplication.java文件内容如下所示 -
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer;@SpringBootApplication public class SpringBootWebApplication extends SpringBootServletInitializer {@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(SpringBootWebApplication.class); }public static void main(String[] args) throws Exception {SpringApplication.run(SpringBootWebApplication.class, args); }}
一个简单的Spring控制器类
import java.util.Map;import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class WelcomeController {// inject via application.properties@Value("${welcome.message:test}")private String message = "Hello World";@RequestMapping("/")public String welcome(Map<String, Object> model) {model.put("message", this.message);return "welcome";}}
JSP + 资源 + 静态文件
对于JSP文件,把它们全放入到 src/main/webapp/WEB-INF/jsp/ 目录中。
src/main/webapp/WEB-INF/jsp/welcome.jsp文件的内容如下所示 -<!DOCTYPE html> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html lang="en"> <head> <meta charset="UTF-8"><!-- Access the bootstrap Css like this,Spring boot will handle the resource mapping automcatically --><link rel="stylesheet" type="text/css" href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" /><!--<spring:url value="/css/main.css" var="springCss" /><link href="${springCss}" rel="stylesheet" />--><c:url value="/css/main.css" var="jstlCss" /><link href="${jstlCss}" rel="stylesheet" /> </head> <body><nav class="navbar navbar-inverse"><div class="container"><div class="navbar-header"><a class="navbar-brand" href="#">Spring Boot</a></div><div id="navbar" class="collapse navbar-collapse"><ul class="nav navbar-nav"><li class="active"><a href="#">Home</a></li><li><a href="#about">About</a></li></ul></div></div></nav><div class="container"><div class="starter-template"><h1>Spring Boot Web JSP Example</h1><h2>Message: ${message}</h2></div></div><script type="text/javascript" src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script> </body> </html>
对于像CSS或Javascript这样的静态文件,放在 /src/main/resources/static/ 目录中。/src/main/resources/static/css/main.css文件的内容如下所示 -
h1{color:#0000FF; } h2{color:#FF0000; }
对于属性文件,放在 /src/main/resources/ 目录下, /src/main/resources/application.properties文件的内容如下所示 -
spring.mvc.view.prefix: /WEB-INF/jsp/ spring.mvc.view.suffix: .jspwelcome.message: Hello Yiibai//Spring Boot约定过配置,不需要像这样声明资源映射。资源映射只是自动处理。
运行实例:
在终端下进入 F:\worksp\springboot\springboot-jsp 目录,执行以下命令,应该会看到以下结果 -
F:\worksp\springboot\springboot-jsp>mvn spring-boot:run [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Spring Boot Web JSP Example 1.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) > test-compile @ springboot-jsp >>> [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springboot-jsp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springboot-jsp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ springboot-jsp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory F:\worksp\springboot\springboot-jsp\springboot-jsp\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ springboot-jsp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] <<< spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) < test-compile @ springboot-jsp <<< [INFO] [INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) @ springboot-jsp ---. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/ ::Spring Boot:: (v1.4.2.RELEASE)
- 之后,将项目发布到 Tomcat 中,在项目上点击右键,在弹出的选项中选择“Run As -> Ru On Server” ,使用浏览器访问:http://localhost:8080/springboot-jsp/ ,如果程序没有出错误,那应该会看到以下结果 -
- **注:**JSP限制
- 不能创建可执行jar来运行这个嵌入式Tomcat + JSP Web示例,因为Tomcat中有一个硬编码文件模式。 阅读以下这篇文章:Spring Boot JSP限制。
Spring Boot(二)应用实例相关推荐
- Spring Boot(二):Web 综合开发
Spring Boot(二):Web 综合开发 上篇文章介绍了 Spring Boot 初级教程:Spring Boot(一):入门篇,方便大家快速入门.了解实践 Spring Boot 特性:本篇文 ...
- Spring Boot 整合 FreeMarker 实例
前言 在之前的文章Spring Boot 整合 Thymeleaf中,我们学习了如何将模板 Thymeleaf 整合到 Spring Boot 中,那今天我们就来看看,另一个老牌的开源免费模板引擎 - ...
- Java+Spring Boot 二手书交易系统
目录 1 系统简介 2 系统相关技术 2.1 Java EE 2.2 Springboot框架 2.3 Maven技术 2.4 Tomcat服务器 2.5 MySQL 3 需求分析 3.1 需求概述 ...
- Spring Boot - 自动配置实例解读
文章目录 Pre 启用 debug=true输出自动配置 HttpEncodingAutoConfiguration 什么情况下,Spring Boot 会自动装配 HttpEncodingAutoC ...
- spring boot(二):web综合开发
上篇文章介绍了Spring boot初级教程:spring boot(一):入门篇,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续为大家介绍spring boot的其它 ...
- Spring Boot SLF4J日志实例
From: https://blog.csdn.net/lxh18682851338/article/details/78560295 默认情况下,SLF4j日志记录包含在Spring Boot We ...
- Java笔记-Spring Boot SSL(https)实例
此篇博文记录了在web程序中使用自签名的SSL(HTTPS)证书及创建SSL认证. SSL关键的配置 Spring Boot中HTTPS的配置(application.properties) serv ...
- (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控
http://www.ityouknow.com/springboot/2018/02/11/spring-boot-admin.html 上一篇文章<Spring Boot(十九):使用 Sp ...
- (转)Spring Boot(二):Web 综合开发
http://www.ityouknow.com/springboot/2016/02/03/spring-boot-web.html 上篇文章介绍了 Spring Boot 初级教程:Spring ...
- spring boot (二) web swagger2
2019独角兽企业重金招聘Python工程师标准>>> 在上一篇文章中我们介绍了spring boot是个啥,以及spring boot starter都干了啥,让大家对spring ...
最新文章
- 95后女程序员一下班就溜,拒绝加班!下班玩消失,不回信息!leader吐槽:95后都这么有个性吗?...
- R语言编写自定义函数、创建使用ggplot2生成图标(icon)的主题(theme)函数、使用ggplot2以及自定义的图标主题函数创建箱图(boxplot)图标、ggsave保存图标(png、svg
- 在Ubuntu中安装Visual Studio Code
- 【模型解读】浅析RNN到LSTM
- Android推送通知指南(转)
- xml层级工具_.NET的类型层次查看工具,ClassHierarchyViewer,0.3.0.1
- el-select下拉框组件el-option如何使用v-for动态渲染问题 - 方法篇
- Android演示Stack(课下作业)
- python教程七牛云_python-django框架中使用七牛云
- net4.0 程序没反应_笔记本触摸板没反应原因 笔记本触摸板没反应解决方法【详解】...
- luoguP3912 素数个数
- BicycleGAN详解与实现
- Python搭建简易HTTP服务(3.x版本和2.x版本的)
- WebSocket websockets
- android系统 限制应用安装程序,安卓手机不能安装软件是什么原因 安卓手机不能安装软件解决方法...
- f1c100s rootfs调试记录
- html页面会出现浏览器崩溃,电脑出现页面崩溃怎么解决
- 唱响艾泽拉斯_战争篇
- 最新的 CocoaPods 的使用教程 上传podspec
- Java对base64编解码总结
热门文章
- 算法——海量数据(5%)
- sql server 数据脚本生成工具
- linux如何生成so文件,新人问个问题,莫见笑:关于如何生成so文件,大家多多捧场啊...
- C - 3 求正弦值
- mysql 删除多余帐号_安装完mysql数据库后的优化(删除多余用户和数据库)
- Java黑皮书课后题第4章:*4.7(顶点坐标)假设一个正五边形的中心位于(0,0),其中一个点位于0点位置。编写程序,提示用户输入正五边形外接圆的半径,显示p1到p5的5个坐标,保留两位小数
- 为什么(12)式,km不能直接相乘?而要让域k先乘一个代数A里面的单位元,再作用在群M上呢?...
- 如何胜任一个小型公司的技术总监?我的感想
- 开始食用grpc(之一)
- javascript基础07