Does struts 2 accept “POST” method to send data from javascript to an Action server?

青春壹個敷衍的年華 提交于 2019-12-13 05:28:08

问题


[I tried to simplifie the code to better understand the questions]When I tried to send data from a web side client to an ActionClass in Struts 2 like:

jQuery("#bedata").click(function(){ //Function for button "bedata"

 var postData = "SOME DATA TO SEND"

//Sendin data:
$.ajax({
    type: "POST", //Method to send the data. I would need "post" method as better option.
    url: "GuardaFila.action", //Action called to data treatament
    data : {
        jgGridData: postData, //PARAMETER jgGrdData with variable "postData" value
        customData: "someinfo" //Just another parameter called "customData" with more data,
    },


/** START: handlers for request. Not important for this trouble **/
    dataType:"json",
    contentType: "application/json; charset=utf-8",
    success: function(response, textStatus, xhr) {
        alert("success");
     },
    error: function(xhr, textStatus, errorThrown) {
        alert("error");
    }
/** END: handlers for the request. **/
});
});

I wanted to autofill "jgGridData" and "customData" attributes from ActionClass that is called when "CargaTabla.action" is invoked. But if type is POST, it doesn't work. Just changing POST type in ajax type of sending, turning on "GET", it works fine. The call to CargaTabla.action ActionClass setters methods for the jgGridData and customData has done properly.

My struts.xml piece of significant code is:

<action name="GuardaFila" method="guardarUsuario" class="org.json.JSONRespuestaTabla">
    <result name="success" type="json" />
</action>

So, GuardaFila action in its method "guardarUsuario" is properly called in debuggin. A simplified version of ActionClass (org.json.JSONRespuestaTabla) is:

public class JSONRespuestaTabla extends ActionSupport{

String jgGridData = "Old data to be refilled from client";
String customData = "Old data to be refilled from client";

@Override
public String execute() {
    //Some stuff
    return SUCCESS;
}

//Getters and Setter of attributes.

public void setJgGridData(String resultado){
    System.out.append(resultado);
}
public String getJgGridData(){
    return this.jgGridData
}
public String getCustomData() {
    return customData;
}
public void setCustomData(String customData) {
    this.customData = customData;
}

//And the method called and defined in struts.xml (properly called)
public void guardarUsuario(){
   //Some stuff.
   return;
}

Well, so. If Javascript send the parameters in GET mode, SETTERS are working nice, and I can get "SOME DATA TO SEND" on my ActionClass JSONRespuestaTabla setted automatically, ready to work with. But if JavaScript sends this data in POST mode, Struts doesn't call the setters, and I am not able to receive the data into the class that handle the action.

How it comes that? I can't understand why it happens.

I am using jquery, json and tiles plugin for struts2.


回答1:


Done. Just setting type of data:

contentType: "application/x-www-form-urlencoded; charset=utf-8"

instead:

contentType: "application/json; charset=utf-8"


来源:https://stackoverflow.com/questions/8528324/does-struts-2-accept-post-method-to-send-data-from-javascript-to-an-action-ser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!