SpringMVC-RestfulCRUD

文章目录

    • 回顾helloword基本配置
    • 员工列表展示
    • 添加
    • 修改
    • 删除

回顾helloword基本配置

web.xml


	<servlet>
		<servlet-name>SpringMVC_crudservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		
		<load-on-startup>1load-on-startup>
	servlet>

	
	<servlet-mapping>
		<servlet-name>SpringMVC_crudservlet-name>
		<url-pattern>/url-pattern>
	servlet-mapping>
  
  	
  	<filter>
  		<filter-name>CharacterEncodingFilterfilter-name>
  		<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
  		
  		<init-param>
  			<param-name>encodingparam-name>
  			<param-value>UTF-8param-value>
  		init-param>
 		 		
  		<init-param>
  			<param-name>forceEncodingparam-name>
  			<param-value>trueparam-value>
  		init-param>
  	filter>
  	<filter-mapping>
  		<filter-name>CharacterEncodingFilterfilter-name>
  		<url-pattern>/*url-pattern>
  	filter-mapping>
  	
  	
  	<filter>
  		<filter-name>HiddenHttpMethodFilterfilter-name>
  		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
  	filter>
  	<filter-mapping>
  		<filter-name>HiddenHttpMethodFilterfilter-name>
  		<url-pattern>/*url-pattern>
  	filter-mapping>

xxxx-sevlet.xml


<context:component-scan base-package="com.jh">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>
@Controller
public class helloController {
	

	@RequestMapping("/hello")
	public String hello(){
		return "success";
	}
}

员工列表展示

访问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";
	}

index.jsp

    <!-- 访问项目就要展示员工列表页面 -->
<jsp:forward page="/emps"></jsp:forward>

list.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<title>员工列表</title>
</head>
<body>
<h1>员工列表页面</h1>
<table border="1" cellpadding="5px" cellspacing="0">
	<tr>
		<td>ID</td>
		<th>lastName</th>
		<th>email</th>
		<th>gender</th>
		<th>departmentName</th>
		<th>edit</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>edit</td>
			<td>delete</td>
		</tr>
	</c:forEach>
</table>
</body>

SpringMVC-RestfulCRUD_第1张图片

添加

1、list.jsp点击“员工添加”---->2、查询出所有的部门信息要展示在页面---->3、来到add.jsp---->4、输入员工数据---->5、点击保存---->6、处理器收到员工保存请求(保存员工)---->7、保存完成后来到list.jsp

/*
	 * 去员工添加页面,去之前查出所有部门信息进行展示
	 */
	@RequestMapping("/toaddpage")
	public String toAddPage(Model model){
		//1.查出所有部门
		Collection<Department> departments = departmentDao.getDepartments();
		//2.放在请求域中
		model.addAttribute("depts", departments);
		model.addAttribute("employee", new Employee());
		//3.去添加页面
		return "add";
	}

list.jsp

<a href="toaddpage">添加员工</a>

add.jsp

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

<h1>员工添加</h1>
<!-- 表单标签
通过springMVC的表单标签可以实现将模型数据中的属性和HTML表单元素绑定,
以实现表单数据更便捷编辑和表单值的回显
1SpringMVC认为,表单数据中的每一项最终都是要回显的
	path指定的是一个属性,这个属性是从隐含模型(请求域中取出的某个对象的属性);
	path指定的每一个属性,请求域中必须有一个对象,拥有这个属性;这个对象就是请求域中的command;
	modelAttribute=""
	1)、以前我们变淡标签会从请求域中获取一个command对象;把这个对象中的每一个属性对应的显示出来
	2)、可以告诉springmvc不要去取command的值,我放了一个modelAttribute指定的值
	去对象用的key就用我modelAttribute指定的
 -->

 <form:form action="" modelAttribute="employee">
 	lastName:<form:input path="lastName"/><br>
 	email:<form:input path="email"/><br>
 	gender:<br>
 		男:<form:radiobutton path="gender" value="1"/><br>
 		女:<form:radiobutton path="gender" value="0"/><br>
 	dept:
 	<!-- 
 	items="",指定要遍历的集合,自动遍历,遍历出的每一个元素都是一个department对象
 	itemLabel="属性",指定遍历出的这个对象的哪个属性时作为option标签体的值
 	itemValue="属性名",指定刚才遍历出来的这个对象的哪个属性时作为要提交的value值
 	 -->
 		<form:select path="department.id" items="${depts }" itemLabel="departmentName" itemValue="id"></form:select><br>
 		<input type="submit" value="保存">
 </form:form>

联系
SpringMVC-RestfulCRUD_第2张图片
5、点击保存

    /*
	 * 保存员工
	 */
@RequestMapping(value="/emp",method=RequestMethod.POST)
	public String addEmp(Employee employee){
		System.out.println("要添加的员工;"+employee);
		employeeDao.save(employee);
		return "redirect:/emps";
	}

add.jsp

  <!-- 绝对路径 -->
 <%
 	pageContext.setAttribute("ctp", request.getContextPath());
 %>
 <form:form action="${ctp }/emp" modelAttribute="employee" method="POST">

联系:
SpringMVC-RestfulCRUD_第3张图片
运行结果
SpringMVC-RestfulCRUD_第4张图片

修改

/*
	 * 修改
	 */
	@RequestMapping(value="/emp/{id}",method=RequestMethod.PUT)
	public String updateEmp(@ModelAttribute("employee")Employee employee){
		System.out.println("要修改的员工"+employee);
		//更新保存二合一
		employeeDao.save(employee);
		return "redirect:/emps";
	}
	
	/*
	 * 提前查询员工
	 */
	@ModelAttribute
	public void myModelAttribute(@RequestParam(value="id",required=false)Integer id,Model model){
		if(id!=null){
			Employee employee = employeeDao.get(id);
			model.addAttribute("employee", employee);
		}
	}
	

list.jsp
为edit加链接

<%
 	pageContext.setAttribute("ctp", request.getContextPath());
 %>
 
<td>
				<a href="${ctp }/emp/${emp.id }">edit</a>
			</td>
			<td>delete</td>
		</tr>
	</c:forEach>
</table>
<a href="${ctp }/toaddpage">添加员工</a>

edit.jsp

<%
 	pageContext.setAttribute("ctp", request.getContextPath());
 %>
<h1>员工修改页面</h1>
<form:form action="${ctp }/emp/${employee.id }" modelAttribute="employee" method="post">
	<input type="hidden" name="_method" value="put">
	<input type="hidden" name="id" value="${employee.id }">
	email:<form:input path="email"/>
	gender:&nbsp;&nbsp;&nbsp;
		男:<form:radiobutton path="gender" value="1"/>&nbsp;&nbsp;&nbsp;
		女:<form:radiobutton path="gender" value="0"/><br>
	dept:
		<form:select path="department.id" items="${depts }"
		itemLabel="departmentName" itemValue="id"></form:select>
		<br>
	<input type="submit" value="修改">
</form:form>

删除

/*
	 * 删除
	 */
@RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
public String deleteEmp(@PathVariable("id")Integer id){
	employeeDao.delete(id);
	return "redirect:/emps"; 
}
	

list.jsp

<td>
	<form action="${ctp }/emp/${emp.id }" method="post">
		<input type="hidden" name="_method" value="DELETE">
		<input type="submit" value="删除">
	</form>
</td>

你可能感兴趣的:(springmvc,java,web,springmvc)