对表单进行修改操作

一:提交一个带上id的修改请求

<td><a href="emp/${emp.empId }">修改</a> </td>

二:在controller中捕捉该请求

    /*
     * value="emp/{id} {id}是请求中带入的参数
     * method=RequestMethod.GET 捕获get请求的方法
     * @PathVariable("id") Integer id  捕获请求中的id
     * Map<String, Object> map  map是request中的一个map,里面可存值并且在转发页面回显
     */
    @RequestMapping(value="emp/{id}", method=RequestMethod.GET)
    public String updateUI(@PathVariable("id") Integer id, Map<String, Object> map){

        map.put("employee", employeeServce.get(id));
        map.put("departments", departmentService.getAll());

        return "add";
    }

三:在页面进行回显操作

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <title>My JSP 'index.jsp' starting page</title>
  </head>

  <body>
    <!--
        form是springMVC中自带的一种可以是表单回显方便的表达式
        modelAttrbute 必须要和map中放的实体名一致

      -->
    <form:form action="emp" method="POST" modelAttribute="employee">
        <!-- path 表示name -->
        <!-- id为空则是修改方法 -->
        <c:if test="${employee.empId == null }">
            name:<form:input path="empName"/><br>
        </c:if>
        <c:if test="${employee.empId != null }">
            <form:hidden path="empId"/>
            <!-- 将提交方法从post转换成PUT方法,此操作需要在web.xml中配置一些东西 -->
            <input type="hidden" name="_method" value="PUT" />
        </c:if>

        email:<form:input path="email"/><br>
        <%
            Map<String, String> genders = new HashMap<String, String>();
            genders.put("M", "男");
            genders.put("F", "女");
            request.setAttribute("genders", genders);
         %>

        gender:<form:radiobuttons path="gender" items="${genders}"/> <br>

        department:<form:select path="dId" items="${departments }" itemLabel="deptName" itemValue="deptId"></form:select>
        <br>

        <input type="submit" value="submit"/>
    </form:form>

  </body>
</html>

将要修改的数据提交请求

    //先行缓冲原来的employee,
    /*
     * @ModelAttribute  可以在springmvc中保存数据,并可以在下面的的请求中提取出来
     */
    @ModelAttribute
    public void getEmployee(@RequestParam(value="empId",required=false) Integer empId,
            Map<String, Object> map){
        if(empId != null){
            map.put("employee", employeeServce.get(empId));
        }

    }
    //在缓存的employee的基本上来修改employee
    @RequestMapping(value="/emp", method=RequestMethod.PUT)
    public String update(Employee employee){
        System.out.println("aaaa");
        employeeServce.save(employee);

        return "redirect:/emps";
    }

完成修改

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值