Restful风格的curd
package com.hous.crud.bean; public class Department { private Integer id; private String departmentName; public Department() { // TODO Auto-generated constructor stub } public Department(int i, String string) { this.id = i; this.departmentName = string; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } @Override public String toString() { return "Department [id=" + id + ", departmentName=" + departmentName + "]"; } }
package com.hous.crud.bean; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.NumberFormat; public class Employee { private Integer id; private String lastName; private String email; //1 male, 0 female private Integer gender; private Department department; @DateTimeFormat(pattern="yyyy-MM-dd") private Date birth; @NumberFormat(pattern="#,###,###.#") private Float salary; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Float getSalary() { return salary; } public void setSalary(Float salary) { this.salary = salary; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", department=" + department + ", birth=" + birth + ", salary=" + salary + "]"; } public Employee(Integer id, String lastName, String email, Integer gender, Department department) { super(); this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; this.department = department; } public Employee() { // TODO Auto-generated constructor stub } }
package com.hous.crud.controller; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.hous.crud.bean.Department; import com.hous.crud.bean.Employee; import com.hous.crud.dao.DepartmentDao; import com.hous.crud.dao.EmployeeDao; @Controller public class EmployeeController { @Autowired private EmployeeDao employeeDao; @Autowired private DepartmentDao departmentDao; // @ModelAttribute // public void getEmployee(@RequestParam(value="id", required=false) Integer id, // Map<String,Object> map) { // if(id != null) { // map.put("employee", employeeDao.get(id)); // } // } /** * 回显 * @param map * @return */ @RequestMapping(value="/emp", method=RequestMethod.GET) private String input(Map<String, Object> map) { map.put("depts", departmentDao.getDepartments()); map.put("employee", new Employee()); return "input"; } @RequestMapping(value="/emp/{id}", method=RequestMethod.GET) private String input(@PathVariable Integer id, Map<String, Object> map) { map.put("depts", departmentDao.getDepartments()); map.put("employee", employeeDao.get(id)); return "input"; } @RequestMapping(value="/emp", method=RequestMethod.POST) private String add(Employee employee){ employeeDao.save(employee); return "redirect:/list"; } @RequestMapping(value="/emp", method=RequestMethod.PUT) private String update(Employee employee){ employeeDao.save(employee); return "redirect:/list"; } @RequestMapping("/list") private String list(Map<String, Object> map) { map.put("employees", employeeDao.getAll()); return "list"; } @RequestMapping(value="/emp/{id}", method=RequestMethod.DELETE) private String delete(@PathVariable Integer id) { employeeDao.delete(id); return "redirect:/list"; } }
package com.hous.crud.dao; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Repository; import com.hous.crud.bean.Department; @Repository public class DepartmentDao { private static Map<Integer, Department> departments = null; static{ departments = new HashMap<Integer, Department>(); departments.put(101, new Department(101, "D-AA")); departments.put(102, new Department(102, "D-BB")); departments.put(103, new Department(103, "D-CC")); departments.put(104, new Department(104, "D-DD")); departments.put(105, new Department(105, "D-EE")); } public Collection<Department> getDepartments(){ return departments.values(); } public Department getDepartment(Integer id){ return departments.get(id); } }
package com.hous.crud.dao; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.hous.crud.bean.Department; import com.hous.crud.bean.Employee; @Repository public class EmployeeDao { private static Map<Integer, Employee> employees = null; @Autowired private DepartmentDao departmentDao; static{ employees = new HashMap<Integer, Employee>(); employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA"))); employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB"))); employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC"))); employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD"))); employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE"))); } private static Integer initId = 1006; public void save(Employee employee){ if(employee.getId() == null){ employee.setId(initId++); } employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId())); employees.put(employee.getId(), employee); } public Collection<Employee> getAll(){ return employees.values(); } public Employee get(Integer id){ return employees.get(id); } public void delete(Integer id){ employees.remove(id); } }
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 自动扫描bean --> <context:component-scan base-package="com.hous.crud"></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> <!-- 解决静态资源路径问题 --> <mvc:default-servlet-handler/> <mvc:annotation-driven/> </beans>
<%@ page import="java.util.*"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加雇员信息</title> </head> <body> <!-- 1.why使用form标签 快速开发页面,方便表单值的回显 2.注意 可以通过modelAttribute指定绑定的模型类型 若没有指定,则读取request的commond的表单bean 若该属性值也不存在则报错 --> <form:form action="${pageContext.request.contextPath}/emp" method="POST" modelAttribute="employee"> <table> <!-- id为整数类型 --> <c:if test="${employee.id == null }"> <tr><th>First name</th><td><form:input path="lastName"/></td></tr> </c:if> <c:if test="${employee.id != null }"> <form:hidden path="id"/> <input type="hidden" value="PUT" name="_method"/> </c:if> <tr><th>Email${employee.id}</th><td><form:input path="email"/></td></tr> <% Map<String, String> genders = new HashMap<String, String>(); genders.put("0","Male"); genders.put("1","Female"); request.setAttribute("genders", genders); %> <tr><th>Gender</th><td><form:radiobuttons path="gender" items="${genders }"/></td></tr> <tr><th>Department</th><td><form:select path="department.id" items="${depts}" itemLabel="departmentName" itemValue="id" /></td></tr> </table> <input type="submit" value="提交"/> </form:form> </body> </html>
<%@ 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> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>列表信息</title> <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery.js"></script> <script type="text/javascript"> $(function(){ $('.delete').click(function(){ var href=$(this).attr("href"); $("form").attr("action", href).submit(); return false; /*********证明springmvc支持DELETE***********/ /* $.ajax({ url:href, type: 'DELETE', success: function(data){ //alert(data); }, }); */ }); }) </script> </head> <body> <!-- 转换post为delete方法 --> <form action="" method="post"> <input type="hidden" name="_method" value="DELETE"/> </form> <c:if test="${empty requestScope.employees}"> 没有员工信息 </c:if> <c:if test="${!empty requestScope.employees}"> <table border="1px" cellspacing="0" cellpadding="10"> <c:forEach items="${requestScope.employees}" var="employee"> <tr> <td>${employee.id}</td> <td>${employee.lastName}</td> <td>${employee.email}</td> <td>${employee.gender == '1' ? 'Female' : 'Male'}</td> <td>${employee.department.id}${employee.department.departmentName}</td> <td><a href="${pageContext.request.contextPath}/emp/${employee.id }" class="delete">删除</a></td> <td><a href="${pageContext.request.contextPath}/emp/${employee.id }" >修改</a></td> </tr> </c:forEach> </table> </c:if> <br/><br/> <a href="${pageContext.request.contextPath}/emp">添加雇员信息</a> </body> </html>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hous.spring</groupId> <artifactId>springmvc-2</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springmvc-2 Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 自定义变量 --> <properties> <org.springframework.version>4.2.1.RELEASE</org.springframework.version> </properties> <dependencies> <!-- 添加Spring-core包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- 添加spring-context包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- 添加spring-context-support包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- 添加spring-beans包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- 面向切面 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- jdbcTemplate开发 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- c3p0数据源 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.1</version> </dependency> <!-- 添加mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> <!-- fastjson解析json --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.9</version> </dependency> <!-- servlet相关 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- jstl标签 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>springmvc-2</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
<?xml version="1.0" encoding="UTF-8" ?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- 处理请求方法,将POST请求转换成PUT,DELETE --> <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> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <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:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
相关推荐
在"RESTful Web Service Demo"项目中,我们将探讨如何使用Java语言来创建这样的服务,并提供CURD(Create、Read、Update、Delete)操作。 首先,理解RESTful架构的关键概念: 1. 资源(Resource):在REST中,一切...
通过以上步骤,你可以构建一个基础的 Flask_Restful API,实现 CURD 操作。随着项目复杂度的增加,你可以进一步优化和扩展,如分页、过滤、排序等功能。在实际开发过程中,确保遵循 RESTful 原则,如使用标准的 ...
RESTful API 是一种软件架构风格,不是技术框架,RESTful API 由 Roy Fielding 在他的论文中提出。RESTful API 有一系列规范,满足这些规范的 API 均可称为 RESTful API。RESTful API 的核心包括: 1. 在 RESTful ...
RESTful API遵循REST架构风格,它要求将所有实体抽象为资源,并通过URI唯一标识。RESTful API强调无状态交互,即服务器不需要保存客户端的状态信息,这有利于实现服务端的高可用和负载均衡。在HTTP协议中,通过POST...
REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于HTTP协议,使得客户端和服务器之间的交互变得简单。在这个项目中,Koa被用来构建符合RESTful原则的API,如GET请求获取数据,...
在 Java 中,我们可以使用 Spring Boot 框架轻松实现 RESTful API,它提供了许多内置功能来简化 REST 服务的开发。以下是使用 Spring Boot 实现 CRUD 操作的关键步骤: 1. **项目初始化**: 使用 Spring ...
项目主要功能模块包括经典信息管理模块、用户信息管理模块、省份信息管理模块等,项目接口遵循restful风格,代码结构分布清晰,严格按照dao层-service层-controller层的设计规范编写代码,定义了统一的接口返回格式...
vue后台管理系统。基于gin+vue搭建的后台管理系统框架,集成jwt鉴权,权限管理,动态路由,分页封装,多点登录拦截,资源权限,上传下载...后端:用 Gin 快速搭建基础restful风格API,Gin 是一个go语言编写的Web框架。
7. **Restful Web Services**: REST(Representational State Transfer)是一种Web服务设计风格,强调资源的获取和操作。RESTful Web Services通过HTTP方法来表示资源的状态变化,比如使用GET获取资源,POST创建资源,...
4.3 RESTful API支持:通过配置,ThinkPHP可以轻松实现RESTful风格的API接口,适应移动互联网和前后端分离的趋势。 4.4 调试工具:如Xdebug的集成,可以帮助开发者追踪代码执行过程,提高调试效率。 总结,...
4. **Restful API 示例**:内置的示例代码可以帮助开发者理解如何使用Gin框架创建RESTful风格的API接口,涵盖CRUD(创建、读取、更新、删除)操作。 三、快速构建Go Web工程 使用"Go-easy-gin",你可以按照以下...
本项目名为"SpringBootCrudProject",它是一个基于Spring Boot框架构建的简单CURD(Create, Read, Update, Delete)应用。CRUD操作是任何数据库驱动的应用程序的基础,用于处理数据的创建、读取、更新和删除。Spring...
4. **RESTful API**:Jeecg小架构鼓励使用RESTful风格的API接口,使得服务接口清晰、统一,易于调试和测试,也便于与其他系统进行集成。 5. **权限管理**:Jeecg内置了完善的权限控制机制,包括角色、菜单、按钮等...
在压缩包`goCURD-main`中,你应该能找到一个完整的Go CURD应用示例,包括源码、配置文件和其他辅助文件。通过阅读和运行这个项目,你可以更好地了解Golang中构建RESTful API的整个流程,以及如何利用Gin这样的框架来...
2. **RESTful API支持**:guns全面支持RESTful风格的API设计,符合现代Web服务标准,方便前后端分离的开发模式。 3. **权限控制**:guns内置了完善的权限控制机制,如RBAC(Role-Based Access Control)角色权限...
Elasticsearch 是 Lucene 的封装,简化了其复杂的使用方式,提供了 RESTful 风格的 API,使得开发者能够轻松地进行索引、搜索和分析操作。Elasticsearch 具备以下特点: 1. **分布式架构**:Elasticsearch 可以构建...
Elasticsearch 是一个强大的分布式搜索和分析引擎,它基于 Lucene 库,并提供了一个 RESTful 风格的接口。Elasticsearch 具有以下几个核心特性: 1. **分布式**: 能够在多台服务器上构建集群,提供高可用性和容错性...
7. **RESTful API**:Guns遵循RESTful架构风格设计API,使得服务接口清晰、易于理解,同时增强了系统的可扩展性和互操作性。 8. **页面模板**:Guns提供了丰富的前端页面模板,包括表格、表单、分页等,可以快速...