<i id='NtTGG'><tr id='NtTGG'><dt id='NtTGG'><q id='NtTGG'><span id='NtTGG'><b id='NtTGG'><form id='NtTGG'><ins id='NtTGG'></ins><ul id='NtTGG'></ul><sub id='NtTGG'></sub></form><legend id='NtTGG'></legend><bdo id='NtTGG'><pre id='NtTGG'><center id='NtTGG'></center></pre></bdo></b><th id='NtTGG'></th></span></q></dt></tr></i><div id='NtTGG'><tfoot id='NtTGG'></tfoot><dl id='NtTGG'><fieldset id='NtTGG'></fieldset></dl></div>

      <tfoot id='NtTGG'></tfoot>

      <small id='NtTGG'></small><noframes id='NtTGG'>

        <bdo id='NtTGG'></bdo><ul id='NtTGG'></ul>
      <legend id='NtTGG'><style id='NtTGG'><dir id='NtTGG'><q id='NtTGG'></q></dir></style></legend>

      Struts 2 中 Json 插件的问题

      Problem with Json plugin in Struts 2(Struts 2 中 Json 插件的问题)
        <tbody id='esLT5'></tbody>

          1. <small id='esLT5'></small><noframes id='esLT5'>

          2. <i id='esLT5'><tr id='esLT5'><dt id='esLT5'><q id='esLT5'><span id='esLT5'><b id='esLT5'><form id='esLT5'><ins id='esLT5'></ins><ul id='esLT5'></ul><sub id='esLT5'></sub></form><legend id='esLT5'></legend><bdo id='esLT5'><pre id='esLT5'><center id='esLT5'></center></pre></bdo></b><th id='esLT5'></th></span></q></dt></tr></i><div id='esLT5'><tfoot id='esLT5'></tfoot><dl id='esLT5'><fieldset id='esLT5'></fieldset></dl></div>

            1. <legend id='esLT5'><style id='esLT5'><dir id='esLT5'><q id='esLT5'></q></dir></style></legend>
                <bdo id='esLT5'></bdo><ul id='esLT5'></ul>
                <tfoot id='esLT5'></tfoot>

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)

                <i id='zmACK'><tr id='zmACK'><dt id='zmACK'><q id='zmACK'><span id='zmACK'><b id='zmACK'><form id='zmACK'><ins id='zmACK'></ins><ul id='zmACK'></ul><sub id='zmACK'></sub></form><legend id='zmACK'></legend><bdo id='zmACK'><pre id='zmACK'><center id='zmACK'></center></pre></bdo></b><th id='zmACK'></th></span></q></dt></tr></i><div id='zmACK'><tfoot id='zmACK'></tfoot><dl id='zmACK'><fieldset id='zmACK'></fieldset></dl></div>
                  <bdo id='zmACK'></bdo><ul id='zmACK'></ul>
                      <tbody id='zmACK'></tbody>
                    <tfoot id='zmACK'></tfoot>
                    • <legend id='zmACK'><style id='zmACK'><dir id='zmACK'><q id='zmACK'></q></dir></style></legend>

                      1. <small id='zmACK'></small><noframes id='zmACK'>