
SpringBootRestFul 1.什么是 RESTFul RESTful 是一种软件架构风格、设计风格,而不是标准。 只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风 格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。 当前阶段可以理解为 使用请求方式(POST,GET,PUT,DELETE)来定位方法的一种请求路径写法.




CustomerController.java 添加
/**
* ajax操作:保存客户信息
* @param customer
*/
@PostMapping("/saveCustomer")
public ResponseEntity saveCustomer(Customer customer){
//1、获取表单信息(参数)
//2、调用service保存信息
try {
customerServiceImpl.saveCustomer(customer);
} catch (Exception e) {
//保存失败
// return new Msg(e.getMessage(),false);
// return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);//只能抛出500状态码
return ResponseEntity.ok(new Msg(e.getMessage(),false));
}
//保存成功
//3、获取service结果,写出响应体
return ResponseEntity.ok(new Msg("保存成功",true));
}修改
/**
* ajax修改客户信息
* @return
*/
@PutMapping("/modifyCustomer")
public ResponseEntity modifyCustomer(Customer customer){
//1、调用service进行修改
try {
customerServiceImpl.modifyCustomer(customer);
//2、根据service处理结果,将结果写出响应体
} catch (Exception e) {
//若出现异常,修改失败,将异常信息写出响应体即可
return ResponseEntity.ok(new Msg(e.getMessage(),false));
}
//若未出异常,修改成功
return ResponseEntity.ok(new Msg("修改成功",true));
}删除
/**
* ajax删除客户信息
* @return
*/
@DeleteMapping("/deleteCustomerByCustId/{cust_id}")
public ResponseEntity deleteCustomerByCustId(@PathVariable("cust_id")Long cust_id){
//1、调用service进行删除
//2、根据删除结果,进行响应体写出
try {
customerServiceImpl.deleteCustomerByCustId(cust_id);
} catch (Exception e) {
return ResponseEntity.ok(new Msg(e.getMessage(),false));
}
return ResponseEntity.ok(new Msg("删除成功",true));
}add.jsp
//1、页面加载完成时,为保存按钮绑定点击事件
$("#saveBtn").click(function () {
//2、点击事件中,发送ajax请求到控制器,提交整个表单数据
$.ajax({
type:"post",
url:"${pageContext.request.contextPath}/customer/saveCustomer",
data:$("#f1").serialize(),
contentType:"application/x-www-form-urlencoded",
dataType:"json",
statusCode:{
200:function (data) {
alert(data.msg);
},
500:function () {
alert("服务器异常,请联系管理员!");
}
}
});
});edit.jsp
//2、页面加载完成时,为修改按钮绑定点击事件,点击按钮,ajax方式提交表单
$("#modifyBtn").click(function () {
$.ajax({
type:"put",
url:"${pageContext.request.contextPath}/customer/modifyCustomer",
data:$("#f1").serialize(),
contentType:"application/x-www-form-urlencoded",
dataType:"json",
statusCode:{
200:function (data) {
//1、判断修改结果成功与否
//2、根据修改结果,进行分别处理
alert(data.msg);
if(data.flag){
//修改成功,跳转到 查询所有的功能
kk="${pageContext.request.contextPath}/customer/clist";
}
},
500:function () {
alert("服务器异常,请联系管理员!");
}
}
});
});list.jsp
//删除某个客户信息
function deleteCustomerByCustId(cust_id) {
//加入是否删除判断
if(confirm("是否删除该客户信息?")){
//通过ajax,向控制器发送请求,请求删除该客户信息
$.ajax({
type:"delete",
url:"${pageContext.request.contextPath}/customer/deleteCustomerByCustId/"+cust_id,
dataType:"json",
statusCode:{
200:function (data) {
//1、展示字符串结果
alert(data.msg);
//2、若删除成功,刷新整体页面
if(data.flag){
kk="${pageContext.request.contextPath}/customer/clist";
}
},
500:function () {
alert("服务器异常,请联系管理员!");
}
}
});
}
}