基于SpringMVC的Restful风格的增删改查--④更新员工信息

  1. 基本思想:点击某一个员工的更新链接,进入Spring的from表单,回显该员工数据 =》 submit后,交给handler做save处理 =》重定向至员工列表界面
  2. 注意的点:(1) 员工的lastName不可以被修改(需要使用modelattribute技术) (2) 需要将post请求转为PUT请求(需要借助HiddenHttpMethodFilter)
  3. 具体流程以及核心代码:
    (1) 链接进入form:
 <td><a  class="update" href="input/${emp.id}">Edit</a></td>

(2) form表单以及帮助回显的handler:

<body>
    <form:form action="emp" method="post" modelAttribute="employee">
 <c:if test="${ employee.id == null }">
      lastName: <form:input path="lastName" /> <br>
 </c:if>    
 <c:if test="${ employee.id != null }">
     <form:hidden path="id"/>
     <input type="hidden" name="_method" value="PUT">
 </c:if>
    email: <form:input path="email" /> <br>
        <%
        Map<Integer, String> genders = new HashMap<Integer, String>();
        genders.put(1, "Femal");
        genders.put(0, "Male");
        request.setAttribute("genders", genders);
        %>
    gender: <form:radiobuttons path="gender" items="${genders}" /> <br> 
    department: <form:select path="department.id" itemLabel="departmentName"
            items="${departments}" itemValue="id"> <br>
        </form:select>
    <input value="submit" type="submit">
    </form:form>
</body>
    @RequestMapping(value="input/{id}" ,method=RequestMethod.GET)
    public String inputemp(@PathVariable(value="id") Integer id,Employ employee,Map<String,Object> map){
        employee = employDao.getEmploy(id);
        map.put("employee",employee );
        map.put("departments", departmentDao.getAll());
        return "input";
    }

(3) 更新的handler(包括@modelAttribute)

@ModelAttribute
    public void getemploy(@RequestParam(value="id",required=false) Integer id ,Map<String,Object> map ){
        if(id != null){
        map.put("employee", employDao.getEmploy(id));
        }
        System.out.println(employDao.getEmploy(id));
    }


    @RequestMapping(value="input/emp",method=RequestMethod.PUT)
    public String putEmployee(@ModelAttribute(value="employee") Employ employee,Map<String,Object> map){
        map.put("departments", departmentDao.getAll());
        map.put("employee", employee);
        employDao.save(employee);
        return "redirect:/springmvc/testAllEmploy";
    }
  1. 资料(SpringMVC注解@RequestParam全面解析):
    http://825635381.iteye.com/blog/2196911
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring MVC 是基于 MVC 设计模式的 web 框架,可以很方便地实现 RESTful 风格的 API。下面是 Spring MVC 实现 RESTful API 的增删改查操作的示例代码: 1. 添加(Create)操作 ```java @RequestMapping(value = "/user", method = RequestMethod.POST) @ResponseBody public User add(@RequestBody User user) { // 保存用户信息到数据库 userRepository.save(user); return user; } ``` 2. 查询(Retrieve)操作 ```java @RequestMapping(value = "/user/{id}", method = RequestMethod.GET) @ResponseBody public User get(@PathVariable("id") Long id) { // 从数据库中获取指定 id 的用户信息 User user = userRepository.getOne(id); return user; } ``` 3. 更新(Update)操作 ```java @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT) @ResponseBody public User update(@PathVariable("id") Long id, @RequestBody User user) { // 获取指定 id 的用户信息 User originalUser = userRepository.getOne(id); // 更新用户信息 originalUser.setUsername(user.getUsername()); originalUser.setPassword(user.getPassword()); // 保存用户信息到数据库 userRepository.save(originalUser); return originalUser; } ``` 4. 删除(Delete)操作 ```java @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE) @ResponseBody public void delete(@PathVariable("id") Long id) { // 从数据库中删除指定 id 的用户信息 userRepository.deleteById(id); } ``` 其中,`User` 是一个实体类,`userRepository` 是一个 JPA 接口,用于操作数据库。以上代码中,`@RequestMapping` 注解用于指定 API 的 URL,`@RequestBody` 注解用于将请求体中的 JSON 数据转换成 Java 对象,`@ResponseBody` 注解用于将 Java 对象转换成 JSON 数据返回给客户端。`@PathVariable` 注解用于获取 URL 中的参数值。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值