一个简单的SpringMVC CRUD restful风格的程序

利用SpringMVC做一个CRUD(增删改查)符合Rest风格的

导包:
在这里插入图片描述

搭建环境
1.配置web.xml 文件
(1).建立一个Spring.xml并在里面开启组件扫描 设置视图参数

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

<!-- 扫描所有组件 -->
<context:component-scan base-package="com.ycx"></context:component-scan>
<!-- 配置一个视图解析器,能帮我们拼接页面地址 -->
<bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
</bean>
</beans>

(2).在web.xml中设置编码格式

<filter>
		<filter-name>encodingFilter</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>
		<!-- forceEncoding  顺手解决相应乱码-->
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
 (3).设置支持rest风格的拦截器
<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>
  

先不使用数据库!

要达到的效果
展示界面
显示所有员工信息
url :emps
请求方式 get
在这里插入图片描述

添加员工界面
urt=emp
请求方式get
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

分析
员工列表展示
访问index.jsp—直接发送/emps请求–控制器查询所有员工—放在请求域中 --转发到list页面展示
在这里插入图片描述

@Controller
public class EmployeeController {
	     @Autowired
        EmployeeDao employeeDao;
     //查询所有员工
	@RequestMapping("/emps")
	public String getEmps(Model model){	
		Collection<Employee> all = employeeDao.getAll();
		model.addAttribute("emps",all);
		return "list";
	}
	
 }

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@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>员工列表页面</title>
</head>
<body>
<h1>员工列表</h1>
<table border="1" cellpadding="5" cellspacing="0">
	<tr>
		<th>ID</th>
		<th>lastName</th>
		<th>email</th>
		<th>gender</th>
		<th>department</th>
		<th>EDIR</th>
		<th>DELETE</th>
	</tr>
	<c:forEach items="${emps}" var="emp">
		<tr>
		<td>${emp.id}</td>
		<td>${emp.lastName}</td>
		<td>${emp.email}</td>
		<td>${emp.gender==0?"女":"男" }</td>
		<td>${emp.department.departmentName}</td>
		<td>EDIR</td>
		<td>DELETE</td>
	</tr>
	</c:forEach>

</table>
</body>
</html>

员工添加分析:
在list页面点击员工添加 — (查询所有的部门信息展示在页面)----来到添加页面(add.jsp)—输入员工数据–点击保存–处理器收到员工保存请求进行员工保存–保存完成后来到列表页面

	@RequestMapping("/toaddpage")
	public String toAddPage(Model model){	

		Collection<Department> departments = departmentDao.getDepartments();	
	
		model.addAttribute("depets",departments);
				
		return "add";
	}
<h1>员工添加</h1>
  <form action="">
 lastname:<input type="text"  name="lastName"><br/>
 email:<input type="text"  name="email"><br/>
 gender:<br/>:<input  type="radio" name="gender" value="1"><br/>:<input  type="radio" name="gender" value="0"><br/>

 dept:   <select name="department.id">
              <c:forEach items="${depets}" var="deptItem"> 
              		<option value="${deptItem.id}">${deptItem.departmentName}</option>
              		
              </c:forEach>
 
 		</select><br/>
      <input type="submit" value="提交">
   
  </form>
</body>
</html>

在这里插入图片描述
使用表单标签完成以上操作
通过SpringMVC的表单标签可以实现将模型中数据中的属性和HTML表单元素相绑定以实现表单数据更便捷编辑和表单值的回显
SpringMVC认为,表达拿数据中的每一项最终都是要回显的
path指定的每一个属性 请求域中必须有一个对象,拥有这个属性 这个对象就是请求域中的command
以前我们的标签会从请求域中获取command对象把这个对象的每一个属性对应的显示
在这里插入图片描述
告诉SpringMVC不要去取command的值 我放了一个modelAttribute的指定的值取对象用的key就用modelAttribute
这个功能回显数据特别好用

1.导入表单标签库

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<form:form action="" modelAttribute="employee">
                     <!-- path 就是原来h tml-input的name项 
                                                            第二作用:自动回显隐含模型中某个对象对应的这个属性值                   
                     -->
       lastName:<form:input path="lastName"/><br/>
        enmail :<form:input path="email"/><br/>
        gender:: <form:radiobutton path="gender" value="1"/><br/>: <form:radiobutton path="gender" value="0"/><br/>
                             <!-- items 指定要遍历的集合 彼此哪里出的每一个元素是一个departments的对象
                                  itemLabel="属性" 指定遍历出的这个对象哪个属性作为option的标签体的值提示信息
                                  itemValue       指定遍历出的这个对象哪个属性作为option要提交的值                                  
                              -->
      dept:<form:select path="department.id" items="${depets}" itemLabel="departmentName" itemValue="id">
      
      </form:select>
      <input type="submit" value="提交">
</form:form>

	@RequestMapping("/toaddpage")
	public String toAddPage(Model model){	

		Collection<Department> departments = departmentDao.getDepartments();	
	
		model.addAttribute("depets",departments);
		//这里就是给我们设置的employee设置个回显值  不加的话会报错
		model.addAttribute("employee",new Employee());
				
		return "add";
	}
 }

添加员工实现

在这里插入图片描述
在这里插入图片描述
员工修改
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值