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

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

问题描述

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

[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. **/
});
});

<小时>

我想从调用CargaTabla.action"时调用的 ActionClass 中自动填充jgGridData"和customData"属性.但是如果 type 是 POST,它就不起作用.只需在 ajax 类型的发送中更改 POST 类型,打开GET",它就可以正常工作.对 jgGridData 和 customData 的 CargaTabla.action ActionClass 设置方法的调用已正确完成.


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.

我的 struts.xml 一段重要代码是:

My struts.xml piece of significant code is:

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

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

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

嗯,所以.如果 Javascript 在 GET 模式下发送参数,SETTERS 工作得很好,我可以在我的 ActionClass JSONRespuestaTabla 上自动设置一些要发送的数据",准备工作.但是如果 JavaScript 在 POST 模式下发送这些数据,Struts 不会调用 setter,并且我无法将数据接收到处理操作的类中.

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.

我正在为 struts2 使用 jquery、json 和tiles 插件.

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

推荐答案

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

Done. Just setting type of data:

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

相反:

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

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

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