Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

java - how to get only particular fields in response in struts2

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


}

when i call method m2 via jQuery and get json response it gives

{
 s1: null,
 bean: null,
 s2:"abc",
 m2: "success",

}

but I want only

{
 s2:"abc"
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...