Hibernate+SpringMVC整合:实战三,…

来源:互联网 发布:mmd制作软件手机 编辑:程序博客网 时间:2024/06/05 16:28
package com.lrq.sh.web.controller;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.itheima.sh.domain.Department;
import com.itheima.sh.service.IDepartmentService;

@Controller
@RequestMapping(value = "/department")
public class DepartmentController {
@Autowired
IDepartmentService iDepartmentService;

@RequestMapping(value = "addDepartment")
public String addDepartment(Department department) {
iDepartmentService.saveEntity(department);
return "redirect:/department/showAllDepartments";
}
@RequestMapping(value = "addUI")
public String addUI(){
return "/department/addDepartment";
}

@RequestMapping(value = "showAllDepartments")
public String showAllDepartments(HttpServletRequest request) {
Collection<Department> departments = iDepartmentService
.getAllEntities();
request.setAttribute("departments", departments);
return "/department/showAllDepartments";
}
@RequestMapping(value="updateUI")
public String updateUI(HttpServletRequest request,Long did){
Department department = iDepartmentService.getEntity(Department.class, did);
request.setAttribute("department", department);
return "/department/updateDepartment";
}
@RequestMapping(value="updateDepartment")
public String updateDepartment(Department department) {
iDepartmentService.updateEntity(department);
return "redirect:/department/showAllDepartments";
}

public String deleteDepartment(Long did){
iDepartmentService.deleteEntity(did);
return "redirect:/department/showAllDepartments";
}
}

对应的jsp页面showAllDepartments.jsp

<c:forEach items="${departments }" var="department">
<tr>
<td>${department.dname }</td>
<td>${department.description }</td>
<td>
<a href="http://rongqiang1992.blog.163.com/blog/${pageContext.request.contextPath }/department/updateUI?did=${department.did }">
修改
</a>
<a href="http://rongqiang1992.blog.163.com/blog/${pageContext.request.contextPath }/department/deleteDepartment?did=${department.did }">

</a>
</td>
</tr>
</c:forEach>
<tr>
<td>
<a href="http://rongqiang1992.blog.163.com/blog/${pageContext.request.contextPath }/department/addUI">添 部门</a>
</td>
</tr>
注意:我的jsp页面的路径:是WEB-INF/jsp/department/xxx;
因此在控制器返回的路由串中需要加上对应的路径,department;(重定向除外);
addDepartment.jsp
<form action="${pageContext.request.contextPath }/department/addDepartment" method="post">
部门名称:<input type="text" name="dname"><br/>
部门描述:<textarea rows="5" cols="20" name="description"></textarea><br/>
<input type="reset" value="重置">
<input type="submit" value="提交">
</form>
updateDepartment.jsp:
<form action="${pageContext.request.contextPath }/department/updateDepartment" method="post">
<input type="hidden" name="did" value="${department.did}">
部门名称:<input type="text" name="dname" value="${department.dname }"><br/>
部门描述:<textarea rows="5" cols="20" name="description">${department.description }</textarea><br/>
<input type="reset" value="重置">
<input type="submit" value="提交">
</form>
 
0 0
原创粉丝点击