如何在struts2中只获取响应中的特定字段 [英] how to get only particular fields in response in struts2
问题描述
我有一个动作类,其中定义了许多动作方法,并且还定义了适当的getter和setter方法。我有一些动作方法,我通过从jQuery调用它们来获取数据作为json。但是当我得到json数据时,它包含了定义了getter和setter的所有字段,但我想只得到那个由我调用的方法填充的字段。例如 -
I have an action class in which many action methods are defined and appropriate getters and setters methods are also defined. I have some action methods from which I get the data as json by calling them from jQuery. but when I get the json data it includes all the fields for which getters and setters are defined but i want to get only that field which is filled by that method to which i am calling. for example-
public class ApplicantRegistration extends ActionSupport{
private String s1;
private XyzBean bean;
private String s2;
// respective getters and setters....
public String m1(){
// some work
return SUCCESS;
}
public String m2(){
//some work
s2="abc";
return SUCCESS;
}
}
当我打电话方法m2通过jQuery并得到它给出的json响应
when i call method m2 via jQuery and get json response it gives
{
s1: null,
bean: null,
s2:"abc",
m2: "success",
}
但我只想
{
s2:"abc"
}
推荐答案
默认 json
result序列化由 root
参数指定的所有bean属性,该参数默认设置为操作。但是您可以使用结果的 includeProperties
参数来仅过滤匹配正则表达式的 root
中的那些属性。
By default json
result serializes all bean properties specified by the root
parameter which is set by default to the action. But you can use includeProperties
parameter of the result to filter only those properties from the root
that matched regex expressions.
@Result(type="json", params = {"includeProperties", "^s2"})
这篇关于如何在struts2中只获取响应中的特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!