SpringMVC----RESTFUL_CRUD_添加操作&表单标签

1.

1.list.jsp

<a href="emp">Add New Employee</a>

2.input.jsp

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!-- why使用form标签
            1.可以更快速的开发出表单页面,而且可以更加方便的进行表单值的回显;
     -->

    <form:form action="emp" method="POST" modelAttribute="employee">
        <!-- path属性对应html表单标签的name属性值 -->
        LastName:<form:input path="lastName" />
        <br>    
        email:<form:input path="email" />
        <br>
        <%
            Map<String, String> genders = new HashMap();
                genders.put("1", "male");
                genders.put("0", "Female");

                request.setAttribute("genders", genders);
        %>
        <!-- delimiter="<br>" 按照br分割 -->
        gender:<form:radiobuttons path="gender" items="${genders}" />
        <br>
        Department:<form:select path="department.id" items="${departments}"
            itemLabel="departmentName" itemValue="id"></form:select>
        <br>
        <input type="submit" value="submit" />
    </form:form>
</body>
</html>

2.java代码

package com.yikuan.springmvc.crud.handlers;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yikuan.springmvc.crud.dao.DepartmentDao;
import com.yikuan.springmvc.crud.dao.EmployeeDao;
import com.yikuan.springmvc.crud.entities.Employee;

@Controller
public class EmployeeHandler {
    
    @Autowired
    private EmployeeDao employeeDao;
    
    @Autowired
    private DepartmentDao departmentDao;
    
    @RequestMapping(value="/emp",method=RequestMethod.POST)
    public String save(Employee employee){
        employeeDao.save(employee);
        return "redirect:/emps";
    }
    
    
    @RequestMapping(value="emp",method=RequestMethod.GET)
    public String input(Map<String,Object> map){
        map.put("departments", departmentDao.getDepartments());
        map.put("employee", new Employee());
        return "input";
    }
    
    @RequestMapping("/emps")
    public String list(Map<String, Object> map){
        map.put("employees", employeeDao.getAll());
        return "list";
    }
}

3.结果

 

转载于:https://www.cnblogs.com/yikuan-919/p/9740758.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值