springMVC实现返回json对象的curd操作

springMVC实现返回json对象的curd操作

控制层

@Controller
public class stuController {
    @Autowired
    private StudentDao studentDaoImpl;
//    静态初始化当前工具类
    public static stuController stuController;
//    在方法上加上注解@PostConstruct,这样就会在bean初始化之后被spring容器执行
//    (bean初始化包括:实例化Bean,并装配bean的属性(依赖注入))
    @PostConstruct
    public void init(){
        stuController=this;
    }
//    查看全部信息
    @GetMapping (value = "/stu")
    @ResponseBody
    public List<Student> show(){
        return studentDaoImpl.findAll();
    }
//    按id查找学生
    @GetMapping (value = "/stu/{id}")
    @ResponseBody
    public Student show(@PathVariable("id") Integer stuId){
        return studentDaoImpl.findById(stuId);
    }
//    添加学生
    @PostMapping(value = "/stu")
    @ResponseBody
    public String add(Student stu){
        studentDaoImpl.add(stu);
        return "success";
    }
    //删除学生
    @DeleteMapping (value = "/stu/{id}")
    @ResponseBody
    public String add( @PathVariable("id") Integer stuId){
        studentDaoImpl.delete(stuId);
        return "success";
    }
    //修改学生信息
    @PutMapping("/stu")
    @ResponseBody
    public String update(Student stu){
        studentDaoImpl.update(stu);
        return "success";
    }
}

jsp展示页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/9/21 0021
  Time: 17:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-5.1.0-dist/css/bootstrap.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jQuery/jquery-3.6.0.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap-5.1.0-dist/js/bootstrap.js"></script>

<html>
<head>
    <title>Title</title>
</head>
<body>
<div id="container">
    <h2>学生信息列表</h2>
    <table class="table table-striped" title="table">
        <tr >
            <td>学号</td>
            <td>姓名</td>
            <td>性别</td>
            <td>地址</td>
            <td>电话</td>
            <td>操作</td>
        </tr>
    </table>
</div>
<div>
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
        添加
    </button>

    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">添加</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form>
                        <input type="hidden" name="stuId">
                        <div class="mb-3">
                            <label for="exampleFormControlInput1" class="form-label">姓名</label>
                            <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="stuName" name="stuName">
                        </div>
                        <div class="mb-3">
                            <label for="exampleFormControlInput1" class="form-label">性别</label>
                            <input type="radio"  name="stuSex" value="男" title="man">男
                            <input type="radio"  name="stuSex" value="女" title="famale">女
                        </div>
                        <div class="mb-3">
                            <label for="exampleFormControlInput1" class="form-label">住址</label>
                            <input type="email" class="form-control"  placeholder="address" name="address">
                        </div>
                        <div class="mb-3">
                            <label for="exampleFormControlInput1" class="form-label">电话</label>
                            <input type="email" class="form-control"  placeholder="phone" name="phone">
                        </div>

                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" title="savechange">Save changes</button>
                </div>
            </div>
        </div>
    </div>

</div>

js

<script>
    $(function (){
   
        $(function (){
   
            $.ajax(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值