目标:借助springMVC框架,以 RESTful 风格的形式完成员工的crud操作
源码点我
呈现效果图
1.创建一个web项目,并且向web.xml中配置springMVC相关的信息
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 将post请求转换为 delete 或者是 put请求 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2配置springMVC的核心文件
<context:component-scan base-package="com.zc.cris.springmvc"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
- 创建 javaBean 和对应的dao类
public class Department {
private Integer id;
private String name;
...
}
public class Employee {
private Integer id;
private String name;
private String email;
private Integer age;
private Department department;
private String gender;
...
}
@Repository
public class DeptDao {
private static Map<String, Object> depts = new HashMap<>();
static {
depts.put("1", new Department(1, "管理部"));
depts.put("2", new Department(2, "市场部"));
depts.put("3", new Department(3, "人力部"));
depts.put("4", new Department(4, "前台部"));
depts.put("5", new Department(5, "后勤部"));
}
/**
*
* @MethodName: getDept
* @Description: TODO (根据id返回部门对象)
* @param id
* @return
* @Return Type: Department
* @Author: zc-cris
*/
public static Department getDept(String id) {
return (Department) depts.get(id);
}
/**
*
* @MethodName: getEmps
* @Description: TODO (返回所有部门对象)
* @return
* @Return Type: Collection<Object>
* @Author: zc-cris
*/
public Collection<Object> getEmps() {
return this.depts.values();
}
}
@Repository
public class EmpDao {
private static Map<String, Object> emps = new HashMap<>();
static {
emps.put("1", new Employee(1, "james", "123@qq.com", 25, DeptDao.getDept("1"), "1"));
emps.put("2", new Employee(2, "cris", "444@qq.com", 28, DeptDao.getDept("2"),"0"));
emps.put("3", new Employee(3, "james", "123@qq.com", 25, DeptDao.getDept("3"),"0"));
emps.put("4", new Employee(4, "james", "123@qq.com", 25, DeptDao.getDept("4"),"1"));
emps.put("5", new Employee(5, "james", "123@qq.com", 25, DeptDao.getDept("4"),"1"));
emps.put("6", new Employee(6, "james", "123@qq.com", 25, DeptDao.getDept("5"),"0"));
}
/**
*
* @MethodName: getEmps
* @Description: TODO (返回所有员工对象)
* @return
* @Return Type: Collection<Object>
* @Author: zc-cris
*/
public Collection<Object> getEmps() {
return EmpDao.emps.values();
}
}
- 控制器,并完成基本的list和增加功能
@Controller
public class EmpContoller {
@Autowired
private EmpDao empDao;
@Autowired
private DeptDao deptDao;
/**
*
* @MethodName: save
* @Description: TODO (保存新员工信息)
* @param employee
* @return
* @Return Type: String
* @Author: zc-cris
*/
@RequestMapping(value="emp",method=RequestMethod.POST)
public String save(Employee employee) {
empDao.save(employee);
return "redirect:/list";
}
/**
*
* @MethodName: input
* @Description: TODO (跳转至新增员工页面)
* @param map
* @return
* @Return Type: String
* @Author: zc-cris
*/
@RequestMapping(value="emp",method=RequestMethod.GET)
public String input(Map<String, Object> map) {
//需要从数据库查询部门信息并且放入到requestScope中
map.put("depts", deptDao.getEmps());
//需要为前台的jsp页面的form标签提供 modelAttribute 属性值
map.put("employee", new Employee());
return "input";
}
/**
*
* @MethodName: list
* @Description: TODO (查询所有员工)
* @param map
* @return
* @Return Type: String
* @Author: zc-cris
*/
@RequestMapping("list")
public String list(Map<String, Object> map) {
map.put("emps", this.empDao.getEmps());
return "list";
}
}
- jsp完善
- index.jsp
<a href="list">显示所有员工信息</a>
- list.jsp
<body>
<c:if test="${empty requestScope.emps }">
用户列表里面没有用户了
</c:if>
<c:if test="${!empty requestScope.emps }">
<table align="center" cellpadding="10" cellspacing="0" border="1">
<tr>
<th>员工id</th>
<th>员工名</th>
<th>邮箱</th>
<th>性别</th>
<th>年龄</th>
<th>部门id</th>
<th>修改</th>
<th>删除</th>
</tr>
<c:forEach items="${requestScope.emps }" var="emp">
<tr align="center">
<td>${emp.id }</td>
<td>${emp.name }</td>
<td>${emp.email }</td>
<td>${emp.gender eq '0' ? '女' : '男' }</td>
<td>${emp.age }</td>
<td>${emp.department.id }</td>
<td><a href="">修改</a></td>
<td><a href="">删除</a></td>
</tr>
</c:forEach>
</table>
</c:if>
<a href="emp">新增一个员工</a>
</body>
- input.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
...
<body>
<!--
1. 为什么要使用form标签?
可以更加快捷的开发出表单元素,而且可以很方便的进行页面数据回显
2. 需要注意的是:如果要在jsp页面通过springMVC 表单来显示对应对象的值,得在后台控制器指定绑定的
模型属性,然后在 form标签通过modelAttribu属性来进行映射 若没有指定该属性,那么默认从request 域对象中读取command的表单bean,如果该属性也不存在,那么会抛出异常
-->
<form:form action="emp" method="post" modelAttribute="employee">
<!-- path属性对应html标签的name属性 -->
名字:<form:input path="name"/>
<br>
邮箱:<form:input path="email"/>
<br>
年龄:<form:input path="age"/>
<br>
<%
Map<String, Object> genders = new HashMap<>();
genders.put("0", "女");
genders.put("1", "男");
request.setAttribute("genders", genders);
%>
<!-- 自动根据map的键值对在页面上显示,value是键,显示的是值 -->
性别:<br>
<form:radiobuttons path="gender" items="${requestScope.genders }" delimiter="<br> "/>
<br>
部门:<form:select path="department.id" items="${requestScope.depts }" itemLabel="name" itemValue="id"></form:select>
<br>
<input type="submit" value="添加">
</form:form>
</body>
- 至此,我们就基于 RESTful风格完成了显示和保存功能
总结
- 通过 SpringMVC 的表单标签可以实现将模型数据
中的属性和 HTML 表单元素相绑定,以实现表单
数据更便捷编辑和表单值的回显