1

I'd like to send a data from my javascript to my struts action. It's just an id, therefore i wonder if there is a better way than to do it through a form.submit();

here is the variable i'd like to send.

var id = $('[data-detailsid="' + $(this).data('headid') + '"]');

What would be the proper way to do it ?

My current js function :

$.ajax({
    url: "deleteProduct",
    type: 'POST',
    data: 'productId=' + id
});

And my struts action :

@Action("/deleteProduct")
@ResultPath("/")
@Result(name = "success", location = "jsp/board.jsp")
public class DeleteAction {

    int productId;

    @Autowired
    private UserDao userDao;


    public String execute() {
        Product currentProduct = new Product();
        currentProduct.setId(productId);
        userDao.deleteProduct(currentProduct);
        setProducts(userDao.getProducts());
        return "success";
    }

    public void setProductId(int productId) {
        this.productId = productId;
        }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Aod Ren
  • 681
  • 1
  • 8
  • 17
  • use `$.ajax` to send data to server – charlietfl Feb 22 '16 at 21:25
  • Thanks @charlietfl i'm trying to get the ajax to work but i'm not very used to it. Mind looking at what i did ? I edited my post. I'd like to have my productId inside my action to be set to the id variable in my javascript – Aod Ren Feb 22 '16 at 21:42
  • 1
    basics are correct in `$.ajax`. Use success and error handlers for confirmation. I don't know anything about struts... just treat the ajax data as if it was a form submit of an `` with `name="productId` – charlietfl Feb 22 '16 at 21:51
  • I'll give it a try and let you know. But ajax seems pretty darn powerful. My page don't have to reload to fetch data holy moly :') – Aod Ren Feb 22 '16 at 21:57
  • As integration to RomanC's answer, any action result will always end inside the success callback (`.done()`), even if you return ERROR, because jQuery knows nothing about action results, it uses HTTP codes to detect the state of a response, and with dispatcher result they'll always be 200. To enter the error callback (`.fail()`), you need to return a `json` result with the `errorcode` param, or an `httpheader` result, as described [here](http://stackoverflow.com/a/32840288/1654265) and [here](http://stackoverflow.com/a/35039017/1654265) – Andrea Ligios Feb 23 '16 at 10:17

1 Answers1

4

If you are calling action via Ajax, there isn't necessary to return dispatcher result. You can return result type json if you have json plugin on the classpash and @ParentPackage("json-default") annotation to the action. You can use root parameter to the json result to define the object being serialized when result is executed. By default the root is set to the action instance, so all properties are serialized. If you want to restrict/allow some properties from json serialization you can use parameters to the result excludeProperties and includeProperties. Both parameters are mutually exclusive.

@Result(type="json", params = {"includeProperties", "productId" })

It will return productId back to the page if it was populated to the action bean and the action class has a getter method.

You can get the result on success callback function using

$.ajax({
    url: "deleteProduct",
    type: 'POST',
    data: 'productId=' + id,
    dataType: "json"
}).done(function(data){
    console.log("The product id:" + data.productId);
}).fail(function(jqXHR, textStatus) {
   alert( "Request failed: " + textStatus );
});
Roman C
  • 49,761
  • 33
  • 66
  • 176