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

查看:143
本文介绍了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. **/
});
});






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


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>

因此,GuardaFila在其方法guardarUsuario中的操作在调试程序中被正确调用。 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上获得SOME DATA SEND,随时可以使用。但是如果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天全站免登陆