Spring Boot2之整合JSP

Spring Boot整合JSP

  • pom.xml

    1     <parent>

    2         <groupId>org.springframework.boot</groupId>

    3         <artifactId>spring-boot-starter-parent</artifactId>

    4         <version>2.0.7.RELEASE</version>

    5     </parent>

    6 

    7     <dependencies>

    8         <!-- web组件 -->

    9         <dependency>

   10             <groupId>org.springframework.boot</groupId>

   11             <artifactId>spring-boot-starter-web</artifactId>

   12             <version>2.0.7.RELEASE</version>

   13         </dependency>

   14 

   15         <!-- 整合jsp -->

   16         <dependency>

   17             <groupId>org.springframework.boot</groupId>

   18             <artifactId>spring-boot-starter-tomcat</artifactId>

   19         </dependency>

   20         <dependency>

   21             <groupId>org.apache.tomcat.embed</groupId>

   22             <artifactId>tomcat-embed-jasper</artifactId>

   23         </dependency>

   24 

   25         <!-- JSTL -->

   26         <dependency>

   27             <groupId>jstl</groupId>

   28             <artifactId>jstl</artifactId>

   29             <version>1.2</version>

   30         </dependency>

   31 

   32         <dependency>

   33             <groupId>org.projectlombok</groupId>

   34             <artifactId>lombok</artifactId>

   35             <version>1.18.12</version>

   36         </dependency>

   37     </dependencies>

  • 创建配置文件application.yml

    1 server:

    2     port: 8080

    3 spring:

    4     mvc:

    5         view:

    6             prefix: /

    7             suffix: .jsp

  • 创建Hanler

    1 package com.wiggin.controller;

    2 

    3 import com.wiggin.entity.Student;

    4 import com.wiggin.repository.StudentRepository;

    5 import org.springframework.beans.factory.annotation.Autowired;

    6 import org.springframework.stereotype.Controller;

    7 import org.springframework.web.bind.annotation.GetMapping;

    8 import org.springframework.web.bind.annotation.PathVariable;

    9 import org.springframework.web.bind.annotation.PostMapping;

   10 import org.springframework.web.bind.annotation.RequestMapping;

   11 import org.springframework.web.servlet.ModelAndView;

   12 

   13 

   14 @Controller

   15 @RequestMapping("/hello")

   16 public class HelloHandler {

   17         @Autowired

   18         private StudentRepository studentRepository;

   19         @GetMapping("/index")

   20         public ModelAndView index(){

   21                 ModelAndView modelAndView = new ModelAndView();

   22                 modelAndView.setViewName("index");

   23                 modelAndView.addObject("list",studentRepository.findAll());

   24                 return modelAndView;

   25         }

   26 

   27         @GetMapping("/deleteById/{id}")

   28         public String deleteById(@PathVariable("id") long id){

   29                 studentRepository.deleteById(id);

   30                 return "redirect:/hello/index";

   31         }

   32 

   33         @PostMapping("/save")

   34         public String save(Student student){

   35                 studentRepository.saveOrUpdate(student);

   36                 return "redirect:/hello/index";

   37         }

   38 

   39         @PostMapping("/update")

   40         public String update(Student student){

   41                 studentRepository.saveOrUpdate(student);

   42                 return "redirect:/hello/index";

   43         }

   44 

   45         @GetMapping("/findById/{id}")

   46         public ModelAndView findById(@PathVariable("id") long id){

   47                 ModelAndView modelAndView = new ModelAndView();

   48                 modelAndView.setViewName("update");

   49                 modelAndView.addObject("student",studentRepository.findById(id));

   50                 return modelAndView;

   51         }

   52 

   53 }

  • index.jsp

    1 <%--

    2     Created by IntelliJ IDEA.

    3     User: Administrator

    4     Date: 2020/8/8

    5     Time: 15:49

    6     To change this template use File | Settings | File Templates.

    7 --%>

    8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>

    9 <%@ page isELIgnored="false" %>

   10 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

   11 <html>

   12 <head>

   13         <title>Title</title>

   14 </head>

   15 <body>

   16         <h1>学生信息</h1>

   17         <table>

   18                 <th>学生标号</th>

   19                 <th>学生姓名</th>

   20                 <th>学生年龄</th>

   21                 <th>操作</th>

   22                 <th></th>

   23 

   24                 <tr>

   25                         <c:forEach items="${list}" var="student">

   26                                 <tr>

   27                                         <td>${student.id}</td>

   28                                         <td>${student.name}</td>

   29                                         <td>${student.age}</td>

   30                                         <td>

   31                                                 <a href="/hello/findById/${student.id}">修改</a>

   32                                                 <a href="/hello/deleteById/${student.id}">删除</a>

   33                                         </td>

   34                                 </tr>

   35                         </c:forEach>

   36                 </tr>

   37         </table>

   38         <a href="/save.jsp">添加</a>

   39 

   40 </body>

   41 </html>

  • save.jsp

    1 <%--

    2     Created by IntelliJ IDEA.

    3     User: Administrator

    4     Date: 2020/8/8

    5     Time: 16:18

    6     To change this template use File | Settings | File Templates.

    7 --%>

    8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>

    9 <html>

   10 <head>

   11         <title>Title</title>

   12 </head>

   13 <body>

   14         <form action="hello/save" method="post">

   15                 ID:<input type="text" name="id"><br>

   16                 Name:<input type="text" name="name"><br>

   17                 Age:<input type="text" name="age"><br>

   18                 <input type="submit" value="提交">

   19         </form>

   20 </body>

   21 </html>

  • update.jsp

    1 <%--

    2     Created by IntelliJ IDEA.

    3     User: Administrator

    4     Date: 2020/8/8

    5     Time: 16:18

    6     To change this template use File | Settings | File Templates.

    7 --%>

    8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>

    9 <html>

   10 <head>

   11         <title>Title</title>

   12 </head>

   13 <body>

   14         <form action="/hello/update" method="post">

   15                 ID:<input type="text" name="id" value="${student.id}" readonly><br>

   16                 Name:<input type="text" name="name" value="${student.name}" ><br>

   17                 Age:<input type="text" name="age" value="${student.age}" ><br>

   18                 <input type="submit" value="提交">

   19         </form>

   20 </body>

   21 </html>

 

posted @ 2020-08-13 22:47  wigginess  阅读(122)  评论(0编辑  收藏  举报