Problem with Json plugin in Struts 2(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 插件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Struts 2 中 Json 插件的问题
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01