简体   繁体   English

如何配置Struts 2.3接受GET和POST参数?

[英]How to configure Struts 2.3 to accept both GET and POST parameters?

We have migrated from 2.0 to 2.3. 我们已经从2.0迁移到2.3。 In Struts 2.0, we were able to send an AJAX request that contained both post and get parameters. 在Struts 2.0中,我们能够发送包含post和get参数的AJAX请求。 But after migrating to Struts 2.3, were are not able to do such requests. 但是在迁移到Struts 2.3之后,无法执行此类请求。

Here is what a sample AJAX request made using Prototype.js looks like : 这是使用Prototype.js发出的AJAX请求样例:

var url = '/security/userdetails.action?mode=edit&userid=5';
var params = Form.serialize(form);
new Ajax.Request(url,
        {
            parameters: params,
            onSuccess: function(trans) {
                console.debug('success', trans);
            },
            onFailure: function(trans) {
                console.debug('failure', trans);
            },
            onException:function(trans) {
                console.debug('exception', trans);
            }
        });

As you can see, the above AJAX request contains both GET and POST requests. 如您所见,以上AJAX请求包含GET和POST请求。 This worked fine with 2.0 but in 2.3, it seems the request is validated and the request is forwarded to result type "input", but for that we have no JSP configured, and in the end all we get is a 404 Not Found Error. 在2.0上可以正常工作,但在2.3中,似乎可以验证请求并将请求转发到结果类型“输入”,但是为此我们没有配置JSP,最后得到的只是一个404 Not Found Error。

But if we change the code to the following, it works in Struts 2.3 as well 但是,如果将代码更改为以下代码,它也可以在Struts 2.3中使用

var url = '/security/userdetails.action?mode=edit';    // ------ The Changes are here
var params = Form.serialize(form);
params.userid = 5;  // ----- And here
new Ajax.Request(url,
        {
            parameters: params,
            onSuccess: function(trans) {
                console.debug('success', trans);
            },
            onFailure: function(trans) {
                console.debug('failure', trans);
            },
            onException:function(trans) {
                console.debug('exception', trans);
            }
        });

What is wrong with the first method ? 第一种方法有什么问题? Is there a configuration in Struts 2.3 to allow that request to be made ? Struts 2.3中是否有配置允许发出该请求?

It depends on how HttpServlet works and which method Struts2 uses to retrieve parameters from the request, and method GET/POST (not both) used during that. 这取决于HttpServlet工作方式以及Struts2使用哪种方法从请求中检索参数,以及在此期间使用的GET / POST方法(不是两者)。 Struts2 uses request.getParameterMap() to get parameters Struts2使用request.getParameterMap()获取参数

Request parameters are extra information sent with the request. 请求参数是与请求一起发送的额外信息。 For HTTP servlets, parameters are contained in the query string or posted form data. 对于HTTP Servlet,参数包含在查询字符串或发布的表单数据中。

but you are using http post method and parameters should be post data. 但是您正在使用http post方法,并且参数应该是post数据。

The thing is this : 事情是这样的:

The userid was defined as an integer in the action class, so obviously the setter and getter for that was defined in terms of integer as well userid在操作类中被定义为整数,因此很明显,该setter和getter也是根据整数定义的

But this userid, was present in both the url (as the query string) and in the post body - so basically an array of two strings. 但是此用户ID出现在url(作为查询字符串)和帖子正文中-基本上是两个字符串组成的数组。 when the struts tries to parse the integer from the array, it gets the numberformatexception -> this results in invalid form submission and thus redirects to the "input" result type. 当struts尝试从数组中解析整数时,它会获得numberformatexception->这将导致无效的表单提交,从而重定向到“输入”结果类型。

I did not see the struts log in that matter and got swayed away from the actual problem. 我没有看到Struts登录该问题,并摆脱了实际问题。

Thanks for your inputs though. 感谢您的投入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM