spring boot入门
spring boot简化开发,省去了很多配置。搭建ssm框架过程中的配置都省去了,下面先看一个简单的例子。
先从官网下载项目,然后用myecplise导入项目。
一,https://start.spring.io/,如图:
然后导入,如图:(已经加了一些别的文件)
二,选择DemoApplication以spring boot App运行,如图:
出现这个标志,并且后面提示:Started DemoApplication in 2.345 seconds (JVM running for 2.783),表示启动成功。
三,新建一个controller类HelloWorldController
package com.example.demo; import java.text.DateFormat; import java.util.Date; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @RequestMapping("/hello") public String hello(){ return "hello spirng boot"; } }
访问:http://127.0.0.1:8080/hello,如图:
四,多环境切换:(结构如图)
application-dev.properties:server.port=8081
application-test.properties:server.port=8080
application.properties : spring.profiles.active=dev
这样表示引用文件application-dev.properties的配置。访问变成:http://127.0.0.1:8081/hello
五,配置热部署:
在pom.xml引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 这个需要为 true 热部署才有效 --> </dependency>>
这样改动代码,就不用重新启动了,而是实时改动。
六,访问jsp:
在pom.xml引入相关jar包:
<!-- web支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 部署tomcat,排除冲突 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- servlet依赖 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat的支持 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
完整的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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.16.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- web支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 这个需要为 true 热部署才有效 --> </dependency> <!-- 部署tomcat,排除冲突 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- servlet依赖 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat的支持 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在src-main新建webapp-WEB-INF-jsp。如图:
类HelloWorldController改成:
@Controller public class HelloWorldController { @RequestMapping("/hello") public String hello(Model m){ m.addAttribute("now",DateFormat.getDateTimeInstance().format(new Date())); return "hello"; //视图重定向hello.jsp } }
webapp-WEB-INF-jsp新建hello.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>Insert title here</title> </head> <body> Hi JSP. 现在时间是 ${now} <hr> <!--测试发现更改jsp文件,也需要重启启动类(清缓存也不行!) --> </body> </html>
访问:http://127.0.0.1:8081/hello。如图:
至此,jsp访问成功。