SpringMVC使用restful实现员工修改

 时间:2026-02-22 11:50:53

1、在数据列表列加上编辑地址:

SpringMVC使用restful实现员工修改

2、在控制层实现编辑后数据回显

@RequestMapping(value="/emp/{id}", method=RequestMethod.GET)

    public String input(@PathVariable("id") Integer id, Map<String, Object> map){

        map.put("employee", employeeDao.get(id));

        map.put("departments", departmentDao.getDepartments());

        return "input";

    }

SpringMVC使用restful实现员工修改

3、在列表页面点击编辑按钮,查看数据是否可以回显

SpringMVC使用restful实现员工修改

4、当表单是修改的时候,lastName字段是不能修改的:

<c:if test="${employee.id == null }">

            <!-- path 属性对应 html 表单标签的 name 属性值 -->

            LastName: <form:input path="lastName"/>

        </c:if>

        <c:if test="${employee.id != null }">

            <form:hidden path="id"/>

            <input type="hidden" name="_method" value="PUT"/>

            <%-- 对于 _method 不能使用 form:hidden 标签, 因为 modelAttribute 对应的 bean 中没有 _method 这个属性 --%>

            <%-- 

            <form:hidden path="_method" value="PUT"/>

            --%>

        </c:if>

SpringMVC使用restful实现员工修改

5、实现修改操作的控制层代码。

@ModelAttribute

    public void getEmployee(@RequestParam(value="id",required=false) Integer id,

            Map<String, Object> map){

        if(id != null){

            map.put("employee", employeeDao.get(id));

        }

    }

    

    @RequestMapping(value="/emp", method=RequestMethod.PUT)

    public String update(Employee employee){

        employeeDao.save(employee);

        

        return "redirect:/emps";

    }

SpringMVC使用restful实现员工修改

6、修改列表中的一条记录查看是否能够修改成功。数据能够修改,并且跳转到了数据列表页面。

SpringMVC使用restful实现员工修改

  • SpringMVC使用restful实现员工数据的新增
  • SpringMVC使用restful实现员工列表的查询
  • springmvc实现restful接口
  • SpringMVC关于拦截器的使用
  • SpringMVC中实现数据的格式化
  • 热门搜索
    铎怎么读 猪舌头的做法大全 science怎么读 黑椒汁的做法 rar文件怎么打开 苹果手机id怎么注册 烤箱烤鸡腿的做法 鳕鱼怎么做好吃 准考证怎么打印 怎么写小说