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編譯一下刷新頁面纔會好使。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章