java spring MVC, REST 单击后删除方法 link
java spring MVC, REST Delete method after clicked a link
我正在创建一个允许用户添加新地址、删除和更新地址列表的网页。我的 "add new address" 工作正常,但是 "delete address" 不工作。
请查看下面我的界面设计以便更好地理解,将不胜感激。
这是我 "add new address" 的工作代码,也是非工作的 "Delete address" 代码。
在控制器中:
@RequestMapping(value = "/addressBook", method = RequestMethod.GET)
public Object AddressBook(HttpSession session) throws Exception {
try{
String memberId= (String) session.getAttribute("memberId");
String addressId = (String) session.getAttribute("addressId");
List<Address> addresses = memberService.detail(memberId).getAddresses();
return new ModelAndView("account/addressBook").addObject("addressList", addresses);
} catch (Exception e) {
return new ModelAndView("account/addressBook")
.addObject("error", "No address book found");
}
}
@RequestMapping(value = "/addNewAddress", method = RequestMethod.GET)
public Object AddNewAddress() {
return "account/addNewAddress";
}
@RequestMapping(value = "/addNewAddress", method = RequestMethod.POST)
public Object AddNewAddress(@ModelAttribute AddAddress addAddress, HttpSession session, RedirectAttributes redirectAttributes) {
try {
String memberId = (String )session.getAttribute("memberId");
AddressDetail address1 = memberService.add(addAddress, memberId);
session.setAttribute("addressId", address1.getId());
return "redirect:/addressBook";
} catch (Exception e) {
return new ModelAndView("member/addNewAddress")
.addObject("addressList", addAddress)
.addObject("error", e.getMessage());
}
}
@RequestMapping(value="/deleteAddress" , method =RequestMethod.DELETE )
public Object DeleteAddress(HttpSession session) throws Exception {
String addressId = (String) session.getAttribute("addressId");
String memberId = (String) session.getAttribute("memberId");
AddressDetail addressDetail = memberService.deleteAddress(memberId, addressId);
return new ModelAndView("account/addressBook")
.addObject("success", "Address Deleted")
;
// "redirect:/addressBook";
}`
HTML 页数:
<div class="8u 12u(5)">
<div class="table-wrapper">
<h4>Address Book</h4>
{{#addressList}}
<address>
{{street1}}, {{street2}}<br>
{{district}},{{cityTown}}<br>
{{postCode}}, {{provinceState}},<br>
{{countryCode}}<br>
<br>
<a href="/editAddress">Edit</a>   <a class="confirm" href="/deleteAddress">Delete</a>
<hr>
</address>
{{/addressList}}
</div>
<a href="/addNewAddress"><b>Add New Address</b></a>
</div>
会员服务:
public AddressDetail add(AddAddress addAddress, String memberId) throws Exception {
Address address = new Address.Builder()
.setStreet1(addAddress.getStreet1())
.setStreet2(addAddress.getStreet2())
.setCityTown(addAddress.getCityTown())
.setDistrict(addAddress.getDistrict())
.setProvinceState(addAddress.getProvinceState())
.setPostCode(addAddress.getPostCode())
.setCountryCode(addAddress.getCountryCode())
.build();
RestRequest request = RestRequest.newBuilder()
.url("/member/" + memberId + "/address")
.post(address);
Address response = restClient.execute(configuration.serviceMemberName(), request, Address.class).body();
AddressDetail addressDetail = new AddressDetail();
addressDetail.setId(response.getId());
addressDetail.setDistrict(response.getDistrict());
addressDetail.setStreet1(response.getStreet1());
addressDetail.setStreet2(response.getStreet2());
addressDetail.setCityTown(response.getCityTown());
addressDetail.setProvinceState(response.getProvinceState());
addressDetail.setPostCode(response.getPostCode());
return addressDetail;
}
public AddressDetail addressDetail(String memberId, String addressId) throws Exception {
RestRequest request = RestRequest.newBuilder()
.url("/member/" + memberId + "/address/" + addressId)
.get();
Address response = restClient.execute(configuration.serviceMemberName(), request, Address.class).body();
AddressDetail addressDetail = new AddressDetail();
addressDetail.setId(response.getId());
addressDetail.setStreet1(response.getStreet1());
addressDetail.setStreet2(response.getStreet2());
addressDetail.setDistrict(response.getDistrict());
addressDetail.setCityTown(response.getCityTown());
addressDetail.setCountryCode(response.getCountryCode());
addressDetail.setPostCode(response.getPostCode());
addressDetail.setProvinceState(response.getProvinceState());
return addressDetail;
}
public AddressDetail deleteAddress(String memberId, String addressId) throws Exception {
RestRequest request = RestRequest.newBuilder()
.url("/member/" + memberId + "/address/" + addressId)
.delete();
Address response = restClient.execute(configuration.serviceMemberName(), request, Address.class).body();
AddressDetail addressDetail = new AddressDetail();
addressDetail.setId("");
addressDetail.setStreet1("");
addressDetail.setStreet2("");
addressDetail.setDistrict("");
addressDetail.setCityTown("");
addressDetail.setCountryCode("");
addressDetail.setPostCode("");
addressDetail.setProvinceState("");
return addressDetail;
}
据我所知,您没有使用任何 AJAX 调用来删除您的记录。您刚刚创建了一个指向某些“/deleteAddress
”URL 的锚点,期望浏览器将从 URL 语义中推断出要使用哪个 HTTP 动词。你太乐观了。将要发生的是,您将向上面的“/deleteAddress
”URL 发送一个 HTTP GET。但与此同时,您指示您的容器为 HTTP DELETE 动词注册“/deleteAddress
”URL,所以……没有任何反应。您可能会收到 404 错误。
如果您想使用 DELETE,则必须编写一些 javascript 代码以将 "delete button" 单击附加到 AJAX 调用。
类似于以下内容:
$('.btn-delete').click(function () {
var id = //get the id somehow
doAjaxDelete(id);
});
function doAjaxDelete(id){
$.ajax({
type: "DELETE",
url: "your/path/to/record/" + id,
success: function(response){
// we have the response, do something to notify success
error: function(e){
//do something to notify failure
});
}
此代码适用于所有具有“.btn-delete”的元素 class,因此您应该将其添加到您的按钮(或使用不同的选择器,由您决定)。您还应该找到一种方法,在您的 DELETE URL 中包含一个 id 以正确识别您的记录。最后但同样重要的是,我使用了 JQuery,所以你应该将它包含在你的 HTML 页面中。
一种更简单的方法是将方法注释从 @RequestMapping(value="/deleteAddress" , method =RequestMethod.DELETE )
切换到 @RequestMapping(value="/deleteAddress" , method =RequestMethod.GET)
。老实说,我更喜欢使用 GET 动词来检索东西而不是删除它。
我正在创建一个允许用户添加新地址、删除和更新地址列表的网页。我的 "add new address" 工作正常,但是 "delete address" 不工作。
请查看下面我的界面设计以便更好地理解,将不胜感激。
这是我 "add new address" 的工作代码,也是非工作的 "Delete address" 代码。
在控制器中:
@RequestMapping(value = "/addressBook", method = RequestMethod.GET)
public Object AddressBook(HttpSession session) throws Exception {
try{
String memberId= (String) session.getAttribute("memberId");
String addressId = (String) session.getAttribute("addressId");
List<Address> addresses = memberService.detail(memberId).getAddresses();
return new ModelAndView("account/addressBook").addObject("addressList", addresses);
} catch (Exception e) {
return new ModelAndView("account/addressBook")
.addObject("error", "No address book found");
}
}
@RequestMapping(value = "/addNewAddress", method = RequestMethod.GET)
public Object AddNewAddress() {
return "account/addNewAddress";
}
@RequestMapping(value = "/addNewAddress", method = RequestMethod.POST)
public Object AddNewAddress(@ModelAttribute AddAddress addAddress, HttpSession session, RedirectAttributes redirectAttributes) {
try {
String memberId = (String )session.getAttribute("memberId");
AddressDetail address1 = memberService.add(addAddress, memberId);
session.setAttribute("addressId", address1.getId());
return "redirect:/addressBook";
} catch (Exception e) {
return new ModelAndView("member/addNewAddress")
.addObject("addressList", addAddress)
.addObject("error", e.getMessage());
}
}
@RequestMapping(value="/deleteAddress" , method =RequestMethod.DELETE )
public Object DeleteAddress(HttpSession session) throws Exception {
String addressId = (String) session.getAttribute("addressId");
String memberId = (String) session.getAttribute("memberId");
AddressDetail addressDetail = memberService.deleteAddress(memberId, addressId);
return new ModelAndView("account/addressBook")
.addObject("success", "Address Deleted")
;
// "redirect:/addressBook";
}`
HTML 页数:
<div class="8u 12u(5)">
<div class="table-wrapper">
<h4>Address Book</h4>
{{#addressList}}
<address>
{{street1}}, {{street2}}<br>
{{district}},{{cityTown}}<br>
{{postCode}}, {{provinceState}},<br>
{{countryCode}}<br>
<br>
<a href="/editAddress">Edit</a>   <a class="confirm" href="/deleteAddress">Delete</a>
<hr>
</address>
{{/addressList}}
</div>
<a href="/addNewAddress"><b>Add New Address</b></a>
</div>
会员服务:
public AddressDetail add(AddAddress addAddress, String memberId) throws Exception {
Address address = new Address.Builder()
.setStreet1(addAddress.getStreet1())
.setStreet2(addAddress.getStreet2())
.setCityTown(addAddress.getCityTown())
.setDistrict(addAddress.getDistrict())
.setProvinceState(addAddress.getProvinceState())
.setPostCode(addAddress.getPostCode())
.setCountryCode(addAddress.getCountryCode())
.build();
RestRequest request = RestRequest.newBuilder()
.url("/member/" + memberId + "/address")
.post(address);
Address response = restClient.execute(configuration.serviceMemberName(), request, Address.class).body();
AddressDetail addressDetail = new AddressDetail();
addressDetail.setId(response.getId());
addressDetail.setDistrict(response.getDistrict());
addressDetail.setStreet1(response.getStreet1());
addressDetail.setStreet2(response.getStreet2());
addressDetail.setCityTown(response.getCityTown());
addressDetail.setProvinceState(response.getProvinceState());
addressDetail.setPostCode(response.getPostCode());
return addressDetail;
}
public AddressDetail addressDetail(String memberId, String addressId) throws Exception {
RestRequest request = RestRequest.newBuilder()
.url("/member/" + memberId + "/address/" + addressId)
.get();
Address response = restClient.execute(configuration.serviceMemberName(), request, Address.class).body();
AddressDetail addressDetail = new AddressDetail();
addressDetail.setId(response.getId());
addressDetail.setStreet1(response.getStreet1());
addressDetail.setStreet2(response.getStreet2());
addressDetail.setDistrict(response.getDistrict());
addressDetail.setCityTown(response.getCityTown());
addressDetail.setCountryCode(response.getCountryCode());
addressDetail.setPostCode(response.getPostCode());
addressDetail.setProvinceState(response.getProvinceState());
return addressDetail;
}
public AddressDetail deleteAddress(String memberId, String addressId) throws Exception {
RestRequest request = RestRequest.newBuilder()
.url("/member/" + memberId + "/address/" + addressId)
.delete();
Address response = restClient.execute(configuration.serviceMemberName(), request, Address.class).body();
AddressDetail addressDetail = new AddressDetail();
addressDetail.setId("");
addressDetail.setStreet1("");
addressDetail.setStreet2("");
addressDetail.setDistrict("");
addressDetail.setCityTown("");
addressDetail.setCountryCode("");
addressDetail.setPostCode("");
addressDetail.setProvinceState("");
return addressDetail;
}
据我所知,您没有使用任何 AJAX 调用来删除您的记录。您刚刚创建了一个指向某些“/deleteAddress
”URL 的锚点,期望浏览器将从 URL 语义中推断出要使用哪个 HTTP 动词。你太乐观了。将要发生的是,您将向上面的“/deleteAddress
”URL 发送一个 HTTP GET。但与此同时,您指示您的容器为 HTTP DELETE 动词注册“/deleteAddress
”URL,所以……没有任何反应。您可能会收到 404 错误。
如果您想使用 DELETE,则必须编写一些 javascript 代码以将 "delete button" 单击附加到 AJAX 调用。
类似于以下内容:
$('.btn-delete').click(function () {
var id = //get the id somehow
doAjaxDelete(id);
});
function doAjaxDelete(id){
$.ajax({
type: "DELETE",
url: "your/path/to/record/" + id,
success: function(response){
// we have the response, do something to notify success
error: function(e){
//do something to notify failure
});
}
此代码适用于所有具有“.btn-delete”的元素 class,因此您应该将其添加到您的按钮(或使用不同的选择器,由您决定)。您还应该找到一种方法,在您的 DELETE URL 中包含一个 id 以正确识别您的记录。最后但同样重要的是,我使用了 JQuery,所以你应该将它包含在你的 HTML 页面中。
一种更简单的方法是将方法注释从 @RequestMapping(value="/deleteAddress" , method =RequestMethod.DELETE )
切换到 @RequestMapping(value="/deleteAddress" , method =RequestMethod.GET)
。老实说,我更喜欢使用 GET 动词来检索东西而不是删除它。