Maison  >  Article  >  Java  >  Springboot 2.0.8 Introduction à la méthode pour accéder à la page jsp (avec code)

Springboot 2.0.8 Introduction à la méthode pour accéder à la page jsp (avec code)

不言
不言avant
2019-03-08 15:55:254097parcourir

Le contenu de cet article concerne la méthode de saut de page jsp dans springboot 2.0.8 (avec code). Il a une certaine valeur de référence. J'espère que cela sera utile. vous aide.

Ceci est un tutoriel pour accéder à la page jsp seul. Plus tard, je présenterai la coexistence du html et du jsp. J'ai temporairement annoté le modèle et la configuration de thymeleaf

1 Créer des résultats de répertoire et jsp. fichiers

2 Configurer le modèle et le code de retour de retour

@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.configuration du fichier application.yml paramètres du modèle 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. Le fichier pom.xml ajoute le support tomcat et jsp S'il existe un package rack thymeleaf, il est temporairement commenté Ici, seul le saut jsp est écrit ( Lorsqu'il existe un package rack thymeleaf, le retour. template donnera la priorité au saut vers la page 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. L'expression EL est utilisée directement dans la page jsp

<%@ 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. Ceci est généralement configuré par défaut dans la Structure du Projet, sinon, vous devez l'ajouter

Démarrez-le simplement comme ceci

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer