2

I am creating a web page that allows the user to add new addresses, delete, and update the address list. My "add new address" is working, however "delete address" is not working.

Please check below my interface design for better understanding, help would be appreciated.

This is my working code for "add new address", and also the non working "Delete address" code.

In Controller:

 @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 Page:

    <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> &nbsp <a class="confirm" href="/deleteAddress">Delete</a>
   <hr>
  </address>
  {{/addressList}}
   </div>
  <a href="/addNewAddress"><b>Add New Address</b></a>
   </div>

Member Service:

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;

}
Ah Hiang
  • 592
  • 6
  • 13

1 Answers1

0

From what I can see, you don't use any AJAX call to delete your record. You just created an anchor pointing to some "/deleteAddress" URL expecting that the browser will infer which HTTP verb to use from the URL semantics. You are far too optimistic. What's going to happen is that you'll fire an HTTP GET to the above "/deleteAddress" URL. But at the same time you instruct your Container to register the "/deleteAddress" URL for HTTP DELETE verb and so... nothing happens. Probably you'll get a 404 error. If you want to use DELETE you will have to write a little javascript code to attach the "delete button" click to an AJAX call. Something similar to the following:

$('.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
            });
   }

this code will work for all the elements having a ".btn-delete" class, so you should add it to your button (or use a different selctor, it's up to you). And you should also find a way to include in your DELETE URL an id to properly identify your record. LAst but not least, I used JQuery, so you should include it in your HTML page.

An easier way to proceed is to switch your method annotation from @RequestMapping(value="/deleteAddress" , method =RequestMethod.DELETE ) to @RequestMapping(value="/deleteAddress" , method =RequestMethod.GET). Honestly, I prefer to use the GET verb to retrieve stuff instead of deleting it.

MaVVamaldo
  • 2,505
  • 7
  • 28
  • 50