问题描述
我有以下代码,我希望实现/getJson 将 user 对象作为 json 返回并且/getJson2 将 user2 作为 Json 对象返回的功能.
I have the following code and I would to achieve functionality that /getJson will return user object as json and /getJson2 will return user2 as Json object.
@ParentPackage("json-default")
public class JsonAction extends ActionSupport{
private User user = new User("John","Smith");
private User user2 = new User("Smith","John");
public String populate(){
return "populate";
}
@Action(value="/getJson", results = {
@Result(name="success", type="json")})
public String test(){
return "success";
}
@Action(value="/getJson2", results = {
@Result(name="success", type="json")})
public String test2(){
return "success";
}
@JSON(name="user")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@JSON(name="user2")
public User getUser2() {
return user2;
}
public void setUser2(User user2) {
this.user2 = user2;
}
}
目前无论我调用哪种方法,我仍然得到以下结果:
Currently no matter which method I'm calling I'm still getting the following result:
{"user":{"firstName":"John","lastName":"Smith"},"user2":{"firstName":"Smith","lastName":"John"}}
有可能吗?
更新:
我修改了代码:
public class JsonAction extends ActionSupport{
private User user = new User("John","Smith");
private User user2 = new User("Smith","John");
public String populate(){
return "populate";
}
@Action(value="/getJson", results = {
@Result(name="success", type="json",params = {
"includeProperties",
"user"})})
public String test(){
return "success";
}
@Action(value="/getJson2", results = {
@Result(name="success", type="json",params = {
"includeProperties",
"user2"})})
public String test2(){
return "success";
}
@JSON(name="user")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@JSON(name="user2")
public User getUser2() {
return user2;
}
public void setUser2(User user2) {
this.user2 = user2;
}
}
现在我明白了
{"user":{}}
和
{"user2":{}}
推荐答案
可以,解决方案需要使用包含/排除参数.
Yes it is possible, the solution requires the use of include/exclude parameters.
以下是一个例子.
getJson1 和 getJson2 方法显示 includeParameters,而 getJson3 显示 excludeParameters.
Methods getJson1 and getJson2 show includeParameters while getJson3 shows excludeParameters.
注意:尽管示例使用字符串作为包含/排除参数的参数,但字符串被解释为正则表达式.所以我可以用string*"替换action3上的string1,string2".
Note: Although the example uses strings as the arguments for include/exclude parameters the string is interpreted as a regular expression. So I could replace "string1, string2" on action3 with "string*".
更多信息参见:https://cwiki.apache.org/confluence/display/WW/JSON%20插件
package struts2;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
@ParentPackage("json-default")
public class Test2 extends ActionSupport {
private String string1 = "One";
private String string2 = "Two";
private String other = "Other";
public String getString1() {
return this.string1;
}
public String getString2() {
return this.string2;
}
public String getOther() {
return this.other;
}
@Action(value="/getJson1", results = {
@Result(type = "json", params = {
"includeProperties",
"string1"
})})
public String action1() {
return ActionSupport.SUCCESS;
}
@Action(value="/getJson2", results = {
@Result(type = "json", params = {
"includeProperties",
"string2"
})})
public String action2() {
return ActionSupport.SUCCESS;
}
@Action(value="/getJson3", results = {
@Result(type = "json", params = {
"excludeProperties",
"string1, string2"
})})
public String action3() {
return ActionSupport.SUCCESS;
}
}
.../getJson1 返回 {"string1":"One"}
.../getJson1 returns {"string1":"One"}
.../getJson2 返回 {"string2":"Two"}
.../getJson2 returns {"string2":"Two"}
.../getJson3 返回 {"other":"Other"}
.../getJson3 returns {"other":"Other"}
这篇关于Struts 2 中 Json 插件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!