1,新建SpringBoot項目
2,導入所需依賴(我這里直接貼pom文件咯)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.2.5.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>indi.lwc</groupId> 12 <artifactId>springboot-3</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>springboot-3</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 23 <!--web場景支持--> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-web</artifactId> 27 </dependency> 28 29 <!--springboot測試--> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-test</artifactId> 33 <scope>test</scope> 34 </dependency> 35 36 <!-- jsp支持 --> 37 <dependency> 38 <groupId>org.apache.tomcat.embed</groupId> 39 <artifactId>tomcat-embed-jasper</artifactId> 40 </dependency> 41 42 <!-- jstl標簽庫 --> 43 <dependency> 44 <groupId>javax.servlet</groupId> 45 <artifactId>jstl</artifactId> 46 </dependency> 47 48 <!-- springboot熱部署所需依賴包 --> 49 <dependency> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-devtools</artifactId> 52 <optional>true</optional> 53 <scope>true</scope> 54 </dependency> 55 56 </dependencies> 57 58 <build> 59 <plugins> 60 <plugin> 61 <groupId>org.springframework.boot</groupId> 62 <artifactId>spring-boot-maven-plugin</artifactId> 63 <version>1.4.2.RELEASE</version> 64 <configuration> 65 <!--如何沒有配置該項,則DevTools不會起作用,即應用不會restart--> 66 <fork>true</fork> 67 </configuration> 68 </plugin> 69 </plugins> 70 <!--打包時將jsp文件拷貝到META-INF目錄下--> 71 <resources> 72 <resource> 73 <!--指定處理哪個目錄下的資源文件--> 74 <directory>${basedir}/src/main/web</directory> 75 <!--注意此次必須要放在此目錄下才能被訪問到--> 76 <targetPath>META-INF/resources</targetPath> 77 <includes> 78 <!--所有文件,所有文件夾--> 79 <include>**/**</include> 80 </includes> 81 </resource> 82 83 <resource> 84 85 </resource> 86 <resource> 87 <directory>src/main/resources</directory> 88 <includes> 89 <include>**/**</include> 90 </includes> 91 </resource> 92 93 </resources> 94 </build> 95 96 </project>
3,SpringMVC配置
1 #配置默認端口 2 server.post=8080 3 #配置錯誤頁面 4 server.error.path=/error 5 # 配置session的過期時間 6 # m表示分鍾 s表示秒 7 server.servlet.session.timeout=30m 8 #設置項目的訪問路徑 9 server.servlet.context-path=/ 10 #設置字符編碼 11 spring.http.encoding.charset=UTF-8 12 #上面這些配置不配置其實也行的,它都有默認值得。 13 14 #springboot 視圖配置 15 #配置視圖訪問前綴 16 spring.mvc.view.prefix=/WEB-INF/jsp/ 17 #配置視圖訪問后綴 18 spring.mvc.view.suffix=.jsp
4,web常用注解(學過springMVC都應該知道)
@ Contrller 表示這是一個控制器(類似servlet)
@ RequestMapping 路由,處理請求地址映射
@ GetMapping 使用get方式處理請求地址映射
@ PostMapping 使用post方式處理請求地址映射
@ ResponseBody 異步請求返回JSON格式數據。
5,創建webApp目錄,創建hello.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <%--給頁面上的url填充地址(相對路徑)--%> 5 <base href="/helloworldjsp" /> 6 <title>HelloWorld_JSP</title> 7 <link rel="stylesheet" href="/static/css/hello.css"> 8 </head> 9 <body> 10 <h3 align="center">你想了解我嗎, 11 <br/> --springboot</h3> 12 <span class="hello-span">讓我們進入spring的世界吧</span> 13 <img src="/static/image/spring_boot.png" > 14 </body> 15 </html>
6,編寫Controller類
1 package indi.lwc.springboot3.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.ResponseBody; 7 8 import java.util.HashMap; 9 import java.util.Map; 10 11 @Controller 12 public class IndexController { 13 14 /** 15 * 返回json數據 16 * @return 17 */ 18 @RequestMapping("/getJson") 19 @ResponseBody 20 private Map<String,String> getJson(){ 21 Map<String,String> modelMap = new HashMap<>(); 22 modelMap.put("value1","asdfghjkl"); 23 modelMap.put("value2","qwertyuiop"); 24 return modelMap; 25 } 26 27 /** 28 * 返回jsp頁面 29 * @return 30 */ 31 @GetMapping("/helloworldjsp") 32 private String helloWorldJsp(){ 33 System.out.println("我即將前往神秘的JSP頁面"); 34 return "hello"; 35 } 36 37 38 39 40 }
如果你沒在pom文件配置打包方式則返回 jsp 頁面會有問題(404),通過查資料發現,1.4版本以下不配打包才能使用。官方推薦我們使用模板引擎技術,不推薦使用 jsp。
7,如果你的靜態資源是放在web-inf下面不配置資源管理是訪問不了的,我以前寫ssm框架就喜歡把靜態資源放在wen-inf下面;那時候只需在spring-web.xml文件配置靜態映射就可以。那換成SpringBoot呢?它是沒有xml文件配置的概念了。
ssm框架處理方式:
SpringBoot處理方式(改成類配置方式):
1 package indi.lwc.springboot3.mvc; 2 3 import org.springframework.context.annotation.Configuration; 4 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration; 5 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 6 import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 8 import sun.dc.pr.PRError; 9 10 //標明配置類 11 @Configuration 12 public class MyMvcConfig implements WebMvcConfigurer { 13 16 17 /** 18 * 資源處理 19 * @param registry 20 */ 21 @Override 22 public void addResourceHandlers(ResourceHandlerRegistry registry){ 23 registry.addResourceHandler("/image/**").addResourceLocations("/WEB-INF/"+"/static/"+"/image/"); 24 registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/"+"/static/"); 25 } 26 27
8,啟動SpirngBoot啟動類得到結果。