zoukankan      html  css  js  c++  java
  • (九)SpringBoot之使用jsp

    一、概念

    • jsp应该尽量避免使用,原因如下:
    1. jsp只能打包为:war格式,不支持jar格式,只能在标准的容器里面跑(tomcat,jetty都可以)
    2. 内嵌的Jetty目前不支持JSPs
    3. Undertow不支持jsps
    4. jsp自定义错误页面不能覆盖spring boot 默认的错误页面

    二、案例

      2.1  引入maven依赖

    <dependencies>
            <!-- 除去logback支持 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- 使用log4j2,该包paren标签中已经依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
    
            <!-- 引入freemarker包 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
        <!-- 引入jsp -->
            <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>
            </dependency>
            
    
        </dependencies>
    •  jsp和jstl依赖必须都引入。

      2.2  配置application.properties

    #主配置文件,配置了这个会优先读取里面的属性覆盖主配置文件的属性
    spring.profiles.active=dev
    server.port=8888
        
    logging.config=classpath:log4j2-dev.xml
    spring.mvc.view.prefix: /WEB-INF/templates/
    spring.mvc.view.suffix: .jsp

      2.3  编写控制器

    package com.shyroke.controller;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping(value = "/index")
    public class IndexController {
    
    
        @RequestMapping(value = "/login")
        public ModelAndView index(ModelAndView modelAndView) {
            modelAndView.setViewName("index");
            
            List<String> userList=new ArrayList<String>();
            userList.add("admin");
            userList.add("user1");
            userList.add("user2");
            
            modelAndView.addObject("userList", userList);
            return modelAndView;
        }
    }

      2.4  index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    
    <!DOCTYPE html>
    <html>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="/static/css/index.css" rel="stylesheet">
    <script type="text/javascript" src="/static/jars/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="/static//js/index.js"></script>
    
    </head>
    <body>
        <h2>首页<h2>
        
        <c:forEach items="${userList}" var="user">
            userName: ${user}<br>
        </c:forEach>
    
        <button class="a">click me</button>
    </body>
    </html>

      2.5  js和css

       2.6  结果

     

  • 相关阅读:
    html+css
    HTML的矢量图转换文字
    js初级——复习html+css-下拉标志
    js初级——复习html+css
    四方定理(递归) --java
    进制转换模板
    最大值最小化问题 和最小值最大化问题 ---(二分)
    分治法---循环日程表问题
    全排列 ---java
    并查集---java模板
  • 原文地址:https://www.cnblogs.com/shyroke/p/8023238.html
Copyright © 2011-2022 走看看