Doest struts 2接受“POST”方法从javascript发送数据到Action服务器? [英] Doest struts 2 accept "POST" method to send data from javascript to an Action server?

查看:200
本文介绍了Doest struts 2接受“POST”方法从javascript发送数据到Action服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[我试图简化代码以更好地理解问题]当我试图从Web端客户端发送数据到Struts 2中的ActionClass,如:

  jQuery(#bedata)。click(function(){// button forbedata的函数

var postData =SOME DATA TO SEND

// Sendin data:
$ .ajax({
type:POST,//发送数据的方法,我需要post方法作为更好的选项
url:GuardaFila.action,// Action调用到数据处理
data:{
jgGridData:postData,// PARAMETER jgGrdData变量postDatavalue
customData: someinfo//只有另一个参数customData有更多的数据,
},


/ ** START:处理请求。
dataType:json,
contentType:application / json; charset = utf-8,
success:function(response,textStatus,xhr){
alert成功);
},
错误:function(xhr,textStatus,errorThrown){
alert(error);
}
/ ** END:请求的处理程序。 ** /
});
});






我想自动填充jgGridData和customData 来自在调用CargaTabla.action时调用的ActionClass的属性。但如果类型是POST,它不工作。
只需更改POST类型在ajax类型的发送,打开GET,它工作正常。



我的struts.xml部分的重要代码是:







$ b b

 < action name =GuardaFilamethod =guardarUsuarioclass =org.json.JSONRespuestaTabla> 
< result name =successtype =json/>
< / action>

因此,GuardaFila在其方法guardarUsuario中的操作在debuggin中被正确调用。 ActionClass(org.json.JSONRespuestaTabla)的简化版本是:

  public class JSONRespuestaTabla extends ActionSupport {

String jgGridData =要从客户端重新填充的旧数据;
String customData =要从客户端重新填充的旧数据;

@Override
public String execute(){
//有些东西
return SUCCESS;
}

//属性的Getters和Setter。

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;
}

//在struts.xml中调用和定义的方法(正确调用)
public void guardarUsuario(){
//有些东西。
return;
}

如果Javascript发送参数在GET模式,SETTERS工作不错,我可以得到SOME DATA TO SEND在我的ActionClass JSONRespuestaTabla自动设置,准备使用。但是如果JavaScript在POST模式下发送这个数据,Struts不会调用setter,我不能接收到处理该操作的类中的数据。



如何是什么?我不能理解为什么会发生。



我使用jquery,json和tiles插件struts2。



谢谢

解决方案

完成。只需设置数据类型:

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

insetad:

  contentType:application / json; charset = utf-8

谢谢。 / p>

[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 doesnt 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 doesnt call the setters, and I am not able to recieve the data into the class that handle the action.

How it comes that? I cant understand why it happens.

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

Thank you for your help.

解决方案

Done. Just setting type of data:

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

insetad:

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

Thank you.

这篇关于Doest struts 2接受“POST”方法从javascript发送数据到Action服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆