1.RESTful style
What is RESTful style?
- REpressentational State Transfer REST is an acronym, the Chinese translation for the Representational State Transfer, REST is an architecture, and HTTP is a REST framework agreement contains the property, in order to facilitate understanding, we have split it into different initials of several parts:
- Representational (REpressentational): REST resources can actually be expressed in a variety of forms, including XML, JSON, or even HTML-- most suitable for any form of resource users;
- State (State): When was using REST, we are more concerned about the state of the resource rather than the behavior of resources taken;
- Escape (Transfer): REST resource data related to escape, it is transferred in some form of expression from one application to another
Simply put, REST is the state of the resource to suit the client or server in the form of transfer from the server to the client (or vice versa). In REST, to identify and locate resources by URL, and then REST is defined by behavior (ie HTTP methods) to complete what function.
- REpressentational State Transfer REST is an acronym, the Chinese translation for the Representational State Transfer, REST is an architecture, and HTTP is a REST framework agreement contains the property, in order to facilitate understanding, we have split it into different initials of several parts:
Example shows
In the usual Web development, forms method attribute values are commonly used in the POST and GET, but in fact, HTTP methods other values PATCH, DELETE, PUT, etc. These methods generally matching CRUD actions of:
CRUD action HTTP method Create (increase) POST Select (check) GET Update(改) PUT or PATCH Delete (删) DELETE Before using RESTful style, we have added a product data is this:
/addProduct?name=xxx
it will become after using a RESTful style:/product
This became to use the same URL, to implement different business agreement through different HTTP methods, this is the style of doing things RESTful
User User entity class
package com.alibaba.wlq.bean;
public class User {
@Override
public String toString() {
return "User [account=" + account + ", password=" + password + ", phone=" + phone + "]";
}
private String account;
private String password;
private String phone;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public User(String account, String password, String phone) {
super();
this.account = account;
this.password = password;
this.phone = phone;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String account, String password) {
super();
this.account = account;
this.password = password;
}
public User() {
super();
}
}
Controller control class
package com.alibaba.wlq.controller;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import com.alibaba.wlq.bean.User;
@Controller
@RequestMapping("user")
public class UserController {
//RESTful--->user/1
//method:表示该方法处理get请求
//1赋值给了{uid}了
@RequestMapping(value="{uid}",method=RequestMethod.GET)//查询操作
public String selectById(@PathVariable("uid")int id) {//@PathVariable:把uid的值赋值给形参id
System.out.println("id====="+id);
return "index";
}
@RequestMapping(value="{uid}",method=RequestMethod.POST)//添加操作
public String addUser(@PathVariable("uid")int id,User user) {
System.out.println(user);
System.out.println("id====="+id);
return "index";
}
//SpringMVC提供了一个过滤器,该过滤器可以把post请求转化为put和delete请求
@RequestMapping(method=RequestMethod.PUT)//修改操作
@ResponseBody
public String updateUser(User user) {
System.out.println(user+"======update");
return "index";
}
@RequestMapping(value="{id}",method=RequestMethod.DELETE)//删除操作
@ResponseBody
public String deleteById(@PathVariable int id) {
System.out.println(id+"=====delete");
return "index";
}
}
Arranged filter configuration file web.xml
<!-- 把post请求转化为put和delete请求
使用_method表示真正的提交方式 -->
<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>
index interface
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/9.5springmvcdemo/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$.ajax({
url:"../user",
type:"POST",
data:{
_method:"PUT",
"account":"zhangsan",
"password":"123456",
"phone":"18360917652"
},
success:function(result){
alert(result);
}
});
</script>
</head>
<body>
index界面
</body>
</html>
2. Exception Handling
Global exception handler
package com.alibaba.wlq.controller; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.servlet.ModelAndView; @ControllerAdvice public class ExceptionController { @ExceptionHandler public ModelAndView error(Exception exception) { ModelAndView mv = new ModelAndView(); mv.addObject("error",exception.getMessage()); mv.setViewName("error"); return mv; } }
3.Spring framework
Spring is an open source framework
Join jar package
Join Profiles