JavaWeb日记——利用标签进行回显

在网页开发中回显是一个很常见的操作,它可以使添加和修改都用同一个页面,更值得一提的是,不仅是文字,连一些radiobuttons和select也可以显示为对应的值
这篇博客主要用到c标签和spring里的form标签,需要以下知识

  1. jsp基础
  2. 懂得Spring中的ModelAndView
  3. Controller基础
  4. Spring的基础配置
  5. EL语句基础

以一个简单的职员添加和回显为例

Employee的Bean

public class Employee {

    private Integer id;
    @NotEmpty
    private String lastName;

    @Email
    private String email;
    //1 male, 0 female
    private Integer gender;
    
    private Department department;
    
    @Past
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birth;
    
    @NumberFormat(pattern="#,###,###.#")
    private Float salary;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }


}

Department的Bean

public class Department {

    private Integer id;
    private String departmentName;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
    
}

添加操作

map会连同返回的"input"包装成一个ModelAndView传到相应的jsp
跳转到input.jsp

    @RequestMapping(value="/emp", method=RequestMethod.GET)
    public String input(Map map){
        //获取所有的Department,由于不是重点DepartmentDao的源码就不放上来了
        map.put("departments", departmentDao.getDepartments());
        //传入一个空的Employee
        map.put("employee", new Employee());
        return "input";
    }

修改操作

    @RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
    public String input(@PathVariable("id") Integer id, Map map){
        //获取到对应id的Employee
        map.put("employee", employeeDao.get(id));
        //获取所有的Department
        map.put("departments", departmentDao.getDepartments());
        return "input";
    }

jsp页面

头部导入c标签和form标签

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    




Insert title here


    
    


<%--可用${}获取map中的对象--%> LastName:
Email:
<% Map genders = new HashMap(); genders.put("1", "Male"); genders.put("0", "Female"); request.setAttribute("genders", genders); %> Gender:

Department:

有了form标签之后,再也不用分开两个页面造成浪费,或者写很多个c:if导致代码的混乱

有兴趣的朋友还可以尝试着自定义标签来满足不同的需求

你可能感兴趣的:(JavaWeb日记——利用标签进行回显)