SpringBoot开发——整合jsp与如何发送put请求

一、pom.xml

<!-- 继承父包 -->
  <parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.3.1.RELEASE</version>
  </parent>

  <dependencies>
    <!-- web -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>

    <!-- 整合jsp -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>

    <!-- jstl -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

  </dependencies>

二、application.yml

spring:
  mvc:
    # 视图解析器
    view:
      prefix: /
      suffix: .jsp
    # 允许发送put请求的设置开启
    hiddenmethod:
      filter:
        enabled: true

三、Application启动类

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

四、StudentRepository

public interface StudentRepository {
    Collection<Student> findAll();
    Student findById(long id);
    void updateOrSave(Student student);
    void deleteById(long id);
}

五、StudentRepositoryImpl

@Repository
public class StudentRepositoryImpl implements StudentRepository {
    private static HashMap<Long, Student> map=new HashMap<>();
    // 模拟数据库
    static {
        map.put(1L,new Student(1L,"王五"));
        map.put(2L, new Student(2L, "李四"));
    }
    @Override
    public Collection<Student> findAll() {
        return map.values();
    }

    @Override
    public Student findById(long id) {
        return map.get(id);
    }

    @Override
    public void updateOrSave(Student student) {
        map.put(student.getId(), student);
    }

    @Override
    public void deleteById(long id) {
        map.remove(id);
    }

}

六、StudentHandler

@Controller
@RequestMapping("/student")
public class StudentHandler {
    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("list",studentRepository.findAll());
        return modelAndView;
    }
    @GetMapping("/findById/{id}")
    public ModelAndView findById(@PathVariable("id") long id){
        ModelAndView modelAndView = new ModelAndView("update");
        modelAndView.addObject(studentRepository.findById(id));
        return modelAndView;
    }
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") long id){
        studentRepository.deleteById(id);
        return "redirect:/student/findAll";
    }
    @PostMapping("/save")
    public String save(Student student){
        studentRepository.updateOrSave(student);
        return "redirect:/student/findAll";
    }
    @PutMapping("/update")
    public String update(Student student){
        studentRepository.updateOrSave(student);
        return "redirect:/student/findAll";
    }


}

七、index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <table>
        <tr>
            <th>id</th>
            <th>姓名</th>
        </tr>

        <c:forEach items="${list}" var="student">
            <tr>
                <td>${student.id}</td>
                <td>${student.name}</td>
                <td>
                    <a href="/student/findById/${student.id}">修改</a>
                </td>
                <td>
                    <a href="/student/deleteById/${student.id}">删除</a>
                </td>
            </tr>
        </c:forEach>
            <tr><td><a href="/save.jsp">添加</a></td> </tr>
    </table>
</body>
</html>

八、update.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/student/update" method="post">
        <input type="hidden"  name="_method" value="put">
        <table>
            <tr>
                <td>id</td>
                <td><input name="id" value="${student.id}" readonly></td>
            </tr>
            <tr>
                <td>姓名</td>
                <td><input name="name" value="${student.name} "></td>
            </tr>
            <tr><td><input type="submit" value="提交"></td></tr>
        </table>
    </form>
</body>
</html>

九、save.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/student/save" method="post">

    id:<input name="id" ><br>
    姓名:<input name="name"><br>
   <input type="submit" value="提交">

</form>
</body>
</html>

十、jsp如何发送put请求

在发送POST请求时携带一个name="_method" 的隐藏域,值为DELETE 或PUT

<input type="hidden"  name="_method" value="put">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值