首頁 > Java > java教程 > 主體

springboot 2.0.8 跳轉jsp頁面的方法介紹(附程式碼)

不言
發布: 2019-03-08 15:55:25
轉載
4106 人瀏覽過

這篇文章帶給大家的內容是關於springboot 2.0.8 跳轉jsp頁面的方法介紹(附代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

這是一篇單獨跳轉jsp頁面的教程,後續會介紹html和jsp同時存在的我暫時註釋了thymeleaf模版以及配置

1創建目錄結果和jsp文件 

2設定return 傳回模版及程式碼

@RequestMapping(value = "/testJsp", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
    public String testJsp(Model m, HttpServletRequest request, HttpServletResponse response){
        List<Map<String,Object>> list=userService.userQueryAll();
        request.setAttribute("list",list);
        log.info("进入了testJsp方法!");
        return "views/testJsp";
    }
登入後複製

3.application.yml 檔案設定 thymeleaf 模版參數

spring:
  dataSource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/db-test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&usessl=false
    username: root
    password: 123456
    driverClassName: com.mysql.jdbc.Driver
  mvc:
    view: #新版本 1.3后可以使用
      suffix: .jsp
      prefix: /WEB-INF/
  view: #老版本 1.4后被抛弃
    suffix: .jsp
    prefix: /WEB-INF/
登入後複製

4. pom.xml 文件加入tomcat和jsp支持,如果是有thymeleaf架包的暫時註解掉,這裡只寫jsp跳轉( 有thymeleaf架包的時候return模版會優先跳轉html頁面)

<!--tomcat支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--servlet依赖.-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!--jsp标签库-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
登入後複製

 5. jsp頁面這裡直接用了EL表達式

<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html >
<html lang="zh-CN">
<head>
    <title>Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script src="<%=basePath%>static/js/jquery-3.3.1.js" charset="utf-8"></script>
</head>
<body>
<table border="1">
    <thead>
    <tr>
        <th>序号</th>
        <th>名字</th>
        <th>号码</th>
        <th>创建时间</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${list}" var="listv" varStatus="status">
        <tr>
            <td>${listv.id}</td>
            <td>${listv.name}</td>
            <td>${listv.phone}</td>
            <td>${listv.create_time}</td>
        </tr>
    </c:forEach>
    </tbody>
</table>

</body>
<script type="text/javascript" charset="utf-8">

</script>
</html>
登入後複製

6. Project Structure裡面這個一般是預設配置好了的,如果沒有就要加上哦 

就這樣啟動就行了 效果圖如下

以上是springboot 2.0.8 跳轉jsp頁面的方法介紹(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!