在网页开发中回显是一个很常见的操作,它可以使添加和修改都用同一个页面,更值得一提的是,不仅是文字,连一些radiobuttons和select也可以显示为对应的值
这篇博客主要用到c标签和spring里的form标签,需要以下知识
- jsp基础
- 懂得Spring中的ModelAndView
- Controller基础
- Spring的基础配置
- 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<String, Object> 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<String, Object> 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" %>
<!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>
<!--
注意:
可以通过 modelAttribute 属性指定绑定的模型属性,
若没有指定该属性,则默认从 request 域对象中读取 command 的表单 bean
如果该属性值也不存在,则会发生错误。
action最好是写成绝对路径。
-->
<br><br>
<form:form action="${pageContext.request.contextPath }/emp" method="POST"
modelAttribute="employee">
<br>
<!-- c标签一般用于判断,test为true时显示标签中的内容-->
<%--可用${}获取map中的对象--%>
<c:if test="${employee.id == null }">
<!-- path 属性对应 modelAttribute指定对象中的的 LastName 属性值 -->
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 这个属性 -->
</c:if>
<br>
Email: <form:input path="email"/>
<br>
<!-- radiobuttons的items是一个map-->
<!-- map中的key对应的是select中的value,和path相等时为选中-->
<!-- map中的value对应的是select中显示的内容-->
<%
Map<String, String> genders = new HashMap();
genders.put("1", "Male");
genders.put("0", "Female");
request.setAttribute("genders", genders);
%>
Gender:
<br>
<form:radiobuttons path="gender" items="${genders }" delimiter="<br>"/>
<br>
<!-- select的items是一个List-->
<!-- departments中departmentName的显示在select中-->
<!-- departments中id和path相等时为选中-->
Department: <form:select path="department.id"
items="${departments }" itemLabel="departmentName" itemValue="id"></form:select>
<br>
<input type="submit" value="Submit"/>
</form:form>
</body>
</html>
有了form标签之后,再也不用分开两个页面造成浪费,或者写很多个c:if导致代码的混乱
有兴趣的朋友还可以尝试着自定义标签来满足不同的需求