首先配置web.xml与pom.xml导入jar包,编写applicationContext.xml文件
编写控制器controller
@Controller
public class RestController {
@RequestMapping("/req")
@ResponseBody
public String req(@PathVariable(value = "userId") int userId) {
System.out.println("---req---" + userId);
return "req" + userId;
}
再使用ajax
再使用restful来传参
四种type ,请求是谁就调用谁,添加实体类
$("#add").click(function () {
$.ajax({
url: 'req',
type: 'post',
data: {
"userName": "zs",
"password": "eee",
"userId": "1"
},
dataType: 'text',
success: function (data) {
$('#d').text("--返回的数据:" + data);
}
});
});
$("#delete").click(function () {
$.ajax({
url: 'req',
type: 'delete',
data:'userName=zsfffff',
dataType: 'text',
success: function (data) {
$('#d').text("--返回的数据:" + data);
}
});
});
$("#update").click(function () {
$.ajax({
url: 'req',
type: 'put',
data: {
"userName": "zs",
"password": "eee",
"userId": "1"
},
dataType: 'text',
success: function (data) {
$('#d').text("--返回的数据:" + data);
}
});
});
$("#find").click(function () {
$.ajax({
url: 'req',
type: 'get',
data:{"userId":"33"},
dataType: 'text',
success: function (data) {
$('#d').text("--返回的数据:" + data);
}
});
});
});
//请求方式:post增,delete删,put修改,get查
@PostMapping("/req")
@ResponseBody
public String reqPost(User user) {
System.out.println(user);
return "reqPost";
}
@DeleteMapping("/req")
@ResponseBody
public String reqDelete(String userName) {
System.out.println("---" + userName);
return "reqDelete";
}
@PutMapping("/req")
@ResponseBody
public String reqPut(User user) {
System.out.println(user);
return "reqPut";
}
@GetMapping("/req")
@ResponseBody
public String reqGet(Integer userId) {
System.out.println("----userId" + userId);
return "reqGet";
}