• 吴裕雄天生自然Spring BootSpring Boot对JSP的支持


    1.创建Spring Boot Web应用ch5_4
    2.修改pom.xml文件,添加Servlet、Tomcat和JSTL依赖
    3.设置Web应用ch5_4的上下文路径及页面配置信息
    4.创建实体类Book
    5.创建控制器类ThymeleafController
    6.整理脚本样式静态文件
    7.View视图页面
    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>
        <groupId>com.jsp</groupId>
        <artifactId>SpringBoot-JSP</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <!-- 声明项目配置依赖编码格式为 utf-8 -->
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <fastjson.version>1.2.24</fastjson.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>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <!-- 由于commons-fileupload组件不属于Spring Boot,所以需要加上版本 -->
                <version>1.3.3</version>
            </dependency>
    
            <!-- 添加Servlet依赖 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <scope>provided</scope>
                <!--provided 被依赖包理论上可以参与编译、测试、运行等阶段,相当于compile, 但是在打包阶段做了exclude的动作。适用场景:例如, 
                    如果我们在开发一个web 应用, 在编译时我们需要依赖 servlet-api.jar,但是在运行时我们不需要该 jar包, 因为这个jar包已由应用服务器提供,此时我们需要使用 
                    provided 进行范围修饰。 -->
            </dependency>
            <!-- 添加Tomcat依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <!-- Jasper是Tomcat使用的引擎, 使用tomcat-embed-jasper可以将Web应用在内嵌的tomcat下运行 -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>
            <!-- 添加JSTL依赖 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <!-- 如果没有指定 scope值,该元素的默认值为 compile。 被依赖包需要参与到当前项目的编译,测试,打包,运行等阶段。 打包的时候通常会包含被依赖包。 -->
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!-- 引入JSTL标签 -->
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <title>JSP测试</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-theme.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">第一个基于JSP技术的Spring Boot Web应用</h3>
            </div>
        </div>
        <div class="container">
            <div>
                <h4>图书列表</h4>
            </div>
            <div class="row">
                <div class="col-md-4 col-sm-6">
                    <!-- 使用EL表达式 -->
                    <a href=""> <img src="images/${aBook.picture}" alt="图书封面"
                        style="height: 180px;  40%;" />
                    </a>
                    <div class="caption">
                        <h4>${aBook.bname}</h4>
                        <p>${aBook.author}</p>
                        <p>${aBook.isbn}</p>
                        <p>${aBook.price}</p>
                        <p>${aBook.publishing}</p>
                    </div>
                </div>
                <!-- 使用JSTL标签forEach循环取出集合数据 -->
                <c:forEach var="book" items="${books}">
                    <div class="col-md-4 col-sm-6">
                        <a href=""> <img src="images/${book.picture}" alt="图书封面"
                            style="height: 180px;  40%;" />
                        </a>
                        <div class="caption">
                            <h4>${book.bname}</h4>
                            <p>${book.author}</p>
                            <p>${book.isbn}</p>
                            <p>${book.price}</p>
                            <p>${book.publishing}</p>
                        </div>
                    </div>
                </c:forEach>
            </div>
        </div>
    </body>
    </html>
    server.servlet.context-path=/ch5_4
    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
    package com.ch.ch5_4.model;
    
    public class Book {
        String isbn;
        Double price;
        String bname;
        String publishing;
        String author;
        String picture;
    
        public Book(String isbn, Double price, String bname, String publishing, String author, String picture) {
            super();
            this.isbn = isbn;
            this.price = price;
            this.bname = bname;
            this.publishing = publishing;
            this.author = author;
            this.picture = picture;
        }
    
        public String getIsbn() {
            return isbn;
        }
    
        public void setIsbn(String isbn) {
            this.isbn = isbn;
        }
    
        public Double getPrice() {
            return price;
        }
    
        public void setPrice(Double price) {
            this.price = price;
        }
    
        public String getBname() {
            return bname;
        }
    
        public void setBname(String bname) {
            this.bname = bname;
        }
    
        public String getPublishing() {
            return publishing;
        }
    
        public void setPublishing(String publishing) {
            this.publishing = publishing;
        }
    
        public String getAuthor() {
            return author;
        }
    
        public void setAuthor(String author) {
            this.author = author;
        }
    
        public String getPicture() {
            return picture;
        }
    
        public void setPicture(String picture) {
            this.picture = picture;
        }
    }
    package com.ch.ch5_4.controller;
    
    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import com.ch.ch5_4.model.Book;
    
    @Controller
    public class ThymeleafController {
        @RequestMapping("/")
        public String index(Model model) {
            Book teacherGeng = new Book("9787302464259", 59.5, "Java 2实用教程(第5版)", "清华大学出版社", "耿祥义", "073423-02.jpg");
            List<Book> chenHeng = new ArrayList<Book>();
            Book b1 = new Book("9787302529118", 69.8, "Java Web开发从入门到实战(微课版)", "清华大学出版社", "陈恒", "082526-01.jpg");
            chenHeng.add(b1);
            Book b2 = new Book("9787302502968", 69.8, "Java EE框架整合开发入门到实战——Spring+Spring MVC+MyBatis(微课版)", "清华大学出版社", "陈恒",
                    "079720-01.jpg");
            chenHeng.add(b2);
            model.addAttribute("aBook", teacherGeng);
            model.addAttribute("books", chenHeng);
            return "index";
        }
    }
    package com.ch.ch5_4;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Ch54Application {
        public static void main(String[] args) {
            SpringApplication.run(Ch54Application.class, args);
        }
    }

  • 相关阅读:
    浏览器图片渲染优化教程
    C#把 DataTable转换为Model实体
    .NET静态变量与静态方法并发的问题
    三种方法查看MySQL数据库的版本
    Mysql 存储引擎 InnoDB与Myisam的主要区别
    dotnet core 出现Can not find runtime target for framework '.NETCoreApp,Version=v1.6' 的解决办法
    细说 C# 中的 IEnumerable和IEnumerator接口
    ASP.NET CORE dotnet run 命令使用debug方式运行
    安装了插件情况下 强制开启火狐浏览器的多进程功能
    JSON 字符串中的中括号和大括号区别详解
  • 原文地址:https://www.cnblogs.com/tszr/p/15334090.html
Copyright © 2020-2023  润新知