Spring-boot教程(十)使用jsp作前端界面

该文内容cv自:https://www.cnblogs.com/shyroke/p/8023238.html

一、概念

  • 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.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  结果

 

 

阅读更多
个人分类: spring-boot
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭