标签:creat over ide 提交 不同的 inf xml配置 怎样 ble
什么是RESTful风格?
简单的说,REST就是将资源的状态以适合客户端或服务端的形式从服务端转移到客户端(或者反过来)。在REST中,资源通过URL进行识别和定位,然后通过行为(即HTTP方法)来定义REST来完成怎样的功能。
实例说明
在平时Web开发中,表单中method属性常用的值是GET和POST,但是实际上,HTTP方法还有PATCH、DELETE、PUT等其他值,这些方法通常会匹配如下的CRUD动作:
CRUD动作 | HTTP方法 |
---|---|
Create(增) | POST |
Select(查) | GET |
Update(改) | PUT或PATCH |
Delete(删) | DELETE |
在使用RESTful风格之前,我们增加一条商品数据是这样的:
/addProduct?name=xxx
但是使用了RESTful风格之后就会变成:
/product
这就变成了使用同一个URL,通过约定不同的HTTP方法来实施不同的业务,这就是RESTful风格所做的事
用户User的实体类
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控制类
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";
}
}
在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界面
<%@ 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>
全局异常处理
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;
}
}
Spring是一个开源框架
加入jar包
加入配置文件
标签:creat over ide 提交 不同的 inf xml配置 怎样 ble
原文地址:https://www.cnblogs.com/wuliqqq/p/11470321.html