Spring-boot 配置返回頁面
一.jsp返回頁面:
1.專案結構圖:
這是打成war包,因為springboot打jar包,webapp檔案不會被打進去,雖然可以通過外掛實現,但是有點麻煩,這裡打成war包執行
也可通過 java -jar xx.war的形式執行
2.pom檔案,依賴上都帶有註釋:
注意<packaging>war</packaging>
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wj</groupId> <artifactId>spring-boot-web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>spring-boot-web</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <shiro.version>1.2.4</shiro.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--jsp頁面使用jstl標籤--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--Provided start--> <!--War包部署到外部的Tomcat中已經包含了這些, 所以需要新增以下依賴 否則會和內嵌的Tomcat 容器發生衝突 --> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-tomcat</artifactId>--> <!--<scope>provided</scope>--> <!--</dependency>--> <!--用於編譯jsp--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!--Provided End--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Json包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.16</version> </dependency> <!-- 只需引入spring-boot-devtools 即可實現熱部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 這個需要為 true 熱部署才有效 --> </dependency> <!-- Shiro Start --> <!-- SECURITY begin --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.3</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-cas</artifactId> <version>${shiro.version}</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>${shiro.version}</version> </dependency> <!-- SECURITY end --> <!-- Shiro End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.手動建立webapp資料夾,springboot預設的根路徑是就是這個(如果還學要使用內部資料夾,properties檔案需要配置)
4.application:
配置頁面所在路徑,從webapp之後開始寫
最後一句話是jsp熱部署
5.新增servleteInitializer檔案,因為springboot缺少web.xml檔案,若打包成war包,則需要繼承 org.springframework.boot.context.web.SpringBootServletInitializer類,覆蓋其config(SpringApplicationBuilder)方法
當然也可以加進去一個最簡單的web.xml(只有根節點的定義,而沒有子元素),防止因缺乏web.xml檔案而部署失敗
6.controller和要返回的jsp :不要使用responseBody註解
package com.wj.springbootweb; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; /** * @Author: wj * @Date: 2018/12/22 11:51 * @Version 1.0 */ @Controller public class HomeController { @GetMapping("/") public String home(ModelMap map){ map.addAttribute("name", "fanfan"); return "index"; } }
<%--
Created by IntelliJ IDEA.
User: fairy
Date: 4/6/2018
Time: 9:56 AM
To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../../base.jsp" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Hello ${name}
</body>
</html>
因為加入了 熱部署,所以修改jsp 直接重新整理頁面即可
注意,因為pom中添加了下面這個依賴,所以可以使用外部tomcat執行,在application中如果指定了 專案埠號和地址,則打包後在外接tomacat中將不起作用,tomcat會根據專案目錄名以及你部署的tomcat埠號訪問
如果是用java -jar xxx.war的方式,則按著你application配置檔案中的配置去訪問
二.使用thymeleaf模板
這裡使用jar包形式
1.pom需要新增thymeleaf依賴:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 打包成war包必備 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 新增對jsp檢視解析的支援 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- 只需引入spring-boot-devtools 即可實現熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- Json包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.16</version>
</dependency>
<!--mybaties 依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--引入druid資料來源-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<!-- 熱部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2.專案結構:
3.html檔案放入classpath:templates中
4.application.yml:
server:
#埠號
port: 8888
servlet:
context-path: /springboot
jsp:
init-parameters:
development: true
tomcat:
max-threads: 800
uri-encoding: utf-8
#mybaties 配置
mybatis:
type-aliases-package: com.springboot.springboot.entity
#mapper-locations: classpath:mapper/*.xml
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo
username: root
password: weidaye123
# mysql驅動
driver-class-name: com.mysql.jdbc.Driver
# druid連線池
type: com.alibaba.druid.pool.DruidDataSource
#連線池補充配置,應用到上面的資料來源當中
#初始化大小,最大、最小值
initialSize: 5
#### thymeleaf配置 #######
#thymeleaf start
thymeleaf:
mode: LEGACYHTML5
encoding: UTF-8
content-type: text/html
cache: false
5.controller和html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>親愛的<span th:text="${name}"></span>,你好!</h4>
</body>
</html>
package com.springboot.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
/**
* @Author: wj
* @Date: 2018/12/22 11:03
* @Version 1.0
*/
@Controller
@RequestMapping( "/page")
public class PageTestController {
@RequestMapping(value="/home")
public String goHome(Map<String, Object> map){
map.put("name", "fanfan");
System.out.println("1");
return "index";
}
}
thymelfa熱部署較為複雜
1.pom新增依賴
2.配置檔案中新增
3.頁面修改後ctrl+f9編譯一下重新整理頁面才會好使。