Struts 2 中 Json 插件的问题

2023-09-25Java开发问题
0

本文介绍了Struts 2 中 Json 插件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有以下代码,我希望实现/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 插件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13