當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringMVC-RestfulCRUD
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC-RestfulCRUD
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 回顧helloword基本配置
- 員工列表展示
- 添加
- 修改
- 刪除
回顧helloword基本配置
web.xml
<!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>SpringMVC_crud</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>SpringMVC_crud</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 配置一個(gè)字符編碼的filter --><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><!-- encoding:指定解決POST請(qǐng)求亂碼 --><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><!-- forceEncoding:順手解決響應(yīng)亂碼 --> <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><!-- 支持Rest風(fēng)格轉(zhuǎn)換的filter --><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>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---->直接發(fā)送/emps---->控制器查詢所有員工---->放在請(qǐng)求域中---->轉(zhuǎn)發(fā)到list頁面展示
@Controller public class EmployeeController {@AutowiredEmployeeDao employeeDao;/** 查詢所有員工*/@RequestMapping("/emps")public String getEmps(Model model){Collection<Employee> all = employeeDao.getAll();model.addAttribute("emps", all);return "list";}index.jsp
<!-- 訪問項(xiàng)目就要展示員工列表頁面 --> <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>添加
1、list.jsp點(diǎn)擊“員工添加”---->2、查詢出所有的部門信息要展示在頁面---->3、來到add.jsp---->4、輸入員工數(shù)據(jù)---->5、點(diǎn)擊保存---->6、處理器收到員工保存請(qǐng)求(保存員工)---->7、保存完成后來到list.jsp
/** 去員工添加頁面,去之前查出所有部門信息進(jìn)行展示*/@RequestMapping("/toaddpage")public String toAddPage(Model model){//1.查出所有部門Collection<Department> departments = departmentDao.getDepartments();//2.放在請(qǐng)求域中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> <!-- 表單標(biāo)簽 通過springMVC的表單標(biāo)簽可以實(shí)現(xiàn)將模型數(shù)據(jù)中的屬性和HTML表單元素綁定, 以實(shí)現(xiàn)表單數(shù)據(jù)更便捷編輯和表單值的回顯 1)SpringMVC認(rèn)為,表單數(shù)據(jù)中的每一項(xiàng)最終都是要回顯的path指定的是一個(gè)屬性,這個(gè)屬性是從隱含模型(請(qǐng)求域中取出的某個(gè)對(duì)象的屬性);path指定的每一個(gè)屬性,請(qǐng)求域中必須有一個(gè)對(duì)象,擁有這個(gè)屬性;這個(gè)對(duì)象就是請(qǐng)求域中的command;modelAttribute=""1)、以前我們變淡標(biāo)簽會(huì)從請(qǐng)求域中獲取一個(gè)command對(duì)象;把這個(gè)對(duì)象中的每一個(gè)屬性對(duì)應(yīng)的顯示出來2)、可以告訴springmvc不要去取command的值,我放了一個(gè)modelAttribute指定的值去對(duì)象用的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="",指定要遍歷的集合,自動(dòng)遍歷,遍歷出的每一個(gè)元素都是一個(gè)department對(duì)象itemLabel="屬性",指定遍歷出的這個(gè)對(duì)象的哪個(gè)屬性時(shí)作為option標(biāo)簽體的值itemValue="屬性名",指定剛才遍歷出來的這個(gè)對(duì)象的哪個(gè)屬性時(shí)作為要提交的value值--><form:select path="department.id" items="${depts }" itemLabel="departmentName" itemValue="id"></form:select><br><input type="submit" value="保存"></form:form>聯(lián)系
5、點(diǎn)擊保存
add.jsp
<!-- 絕對(duì)路徑 --><%pageContext.setAttribute("ctp", request.getContextPath());%><form:form action="${ctp }/emp" modelAttribute="employee" method="POST">聯(lián)系:
運(yùn)行結(jié)果
修改
/** 修改*/@RequestMapping(value="/emp/{id}",method=RequestMethod.PUT)public String updateEmp(@ModelAttribute("employee")Employee employee){System.out.println("要修改的員工"+employee);//更新保存二合一employeeDao.save(employee);return "redirect:/emps";}/** 提前查詢員工*/@ModelAttributepublic 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加鏈接
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: 男:<form:radiobutton path="gender" value="1"/> 女:<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>總結(jié)
以上是生活随笔為你收集整理的SpringMVC-RestfulCRUD的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ecplise常用快捷键
- 下一篇: Mybatis-Helloword