SpringMVC使用restful实现员工修改

2025-11-06 16:06:18

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实现员工修改

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢