Struts 2中的Json插件问题

时间:2011-01-10 15:18:45

标签: java ajax json struts2

我有以下代码,我将实现/ getJson将返回用户对象的功能,因为json和/ getJson2将返回user2作为Json对象。

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

目前无论我打电话给哪种方法,我仍然会得到以下结果:

{"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":{}}

3 个答案:

答案 0 :(得分:8)

是的,该解决方案需要使用包含/排除参数。

以下是一个例子。

方法getJson1和getJson2显示includeParameters,而getJson3显示excludeParameters。

注意:虽然该示例使用字符串作为include / exclude参数的参数,但字符串被解释为正则表达式。所以我可以将action3上的“string1,string2”替换为“string *”。

有关详细信息,请参阅:https://cwiki.apache.org/confluence/display/WW/JSON%20Plugin

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”:“一个”}

... / getJson2 返回{“string2”:“两个”}

... / getJson3 返回{“其他”:“其他”}

答案 1 :(得分:4)

您必须包含要序列化的所有属性。这包括User类的属性,例如:

@Action(value="/getJson", results = {
        @Result(name="success", type="json",params = {
                "includeProperties",
                "user\.firstName, user\.lastName"})})

但是,另一种获得这项工作的形式可能是使用正则表达式:

@Action(value="/getJson", results = {
        @Result(name="success", type="json",params = {
                "includeProperties",
                "user\..*"})})

问候。

答案 2 :(得分:2)

此操作提供两个属性:user和user2。

如果/ getJson和/ getJson2都映射到此操作类,则它们都将使用可用属性进行响应:user和user2。