访问AJAX调用中的Action类Struts 2中的url参数? [英] Access url parameters in Action classes Struts 2 in AJAX call?

查看:114
本文介绍了访问AJAX调用中的Action类Struts 2中的url参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道S2提供了一种干净的方法来获取操作类中的请求参数,只需遵循这些简单规则即可.

I know that S2 provides a clean way to fetch the request parameters in you action class all you need to follow these simple rules.

  1. 创建与请求参数名称相同的属性.
  2. 为此属性创建getter和setter或将属性公开(对于S2.1 +)

但是,当我在这样的AJAX调用中执行此操作时:

However, when I do this in an AJAX call like this:

 $.ajax({
    url: '/gma/getJSONData.action?tspName='+tspName+'&thresholdType='+thresholdType,

我在动作类中没有得到tspName参数.我为此创建了getter/setter.它显示null值.

I don't get the tspName parameter inside action class. I created the getter/setter for it. It's displaying null value.

我在某个地方错了吗?

我在我的Action类构造函数中检查tspName的值,所以打印null.但是,在我的execute方法中,它正确显示了该值.为什么会这样呢?这意味着在构造函数调用之前,它不会初始化值吗?

I was checking the value of tspName in my Action class constructor, so was printing null. However, in my execute method it displays the value correctly. Why is it so? It means before constructor call it does not initialize values?

推荐答案

我正在检查Action类构造函数中tspName的值,所以 打印为空.但是,在我的execute方法中,它显示值 正确地.为什么会这样呢?这意味着在构造函数调用之前,它不会 初始化值?

I was checking the value of tspName in my Action class constructor, so was printing null. However, in my execute method it displays the value correctly. Why is it so?? It means before constructor call it does not initialize values?

也许您应该学习Struts2的工作原理.当您发出请求时,将调用过滤器,并且调度程序通过创建操作上下文和构建操作实例来处理请求.

Probably you should learn the basics how Struts2 works. When you make a request a filter is invoked and the dispatcher is handling the request via creating the action context and building action instance.

然后在此操作上调用拦截器. defaultStack的拦截器之一是params拦截器.负责使用请求参数填充您的操作,以成为更多Struts2操作上下文参数.

Then interceptors are invoked on this action. One of the interceptors of the defaultStack is params interceptor. It's responsible to populate your action with request parameters, to be more Struts2 action context parameters.

这意味着您始终可以从操作上下文中获取参数.参见我们如何访问传递到Action中的请求参数.

It means you can always get parameters from the action context. See How can we access request parameters passed into an Action.

该动作的构造函数在调用任何拦截器之前被调用,因此该动作尚未填充,并且属性尚未初始化.另一方面,执行该动作时,所有拦截器都已被调用,因此将填充该动作.在构造函数之前或之后都没关系.重要的是动作配置中的params拦截器.

The constructor of the action is called before any interceptor is invoked, so the action is not populated yet and properties aren't initialized. On the other hand when the action is executed all interceptors are already invoked, so the action is populated. Before constructor or after constructor it doesn't matter. What is matter is params interceptor in the action configuration.

您总是可以如上面的链接中所述获得参数,也可以直接从servlet请求中获得参数,例如答案.您可以使用Struts2框架的所有功能.

You can always get parameters like described in the link above, or directly from the servlet request like in this answer. All features of Struts2 framework is available to you.

这篇关于访问AJAX调用中的Action类Struts 2中的url参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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