使用JSON插件在Struts 2的问题 [英] Problem with Json plugin in Struts 2
问题描述
我有以下的code,我会实现功能,/的getJSON将返回用户对象为JSON和/ getJson2将返回用户2作为JSON对象。
@ParentPackage(JSON-默认)
公共类JsonAction扩展了ActionSupport {
私人用户的用户=新用户(约翰福音,史密斯);
私人用户user2 =新用户(史密斯夫妇,约翰);
公共字符串填充(){
返回填充;
}
@Action(值=/的getJSON,结果= {
@Result(NAME =成功,键入=JSON)})
公共字符串测试(){
返回成功;
}
@Action(值=/ getJson2,结果= {
@Result(NAME =成功,键入=JSON)})
公共字符串的test2(){
返回成功;
}
@JSON(名称=用户)
公众用户的getUser(){
返回用户;
}
公共无效SETUSER(用户用户){
this.user =用户;
}
@JSON(名称=用户2)
公众用户getUser2(){
返回用户2;
}
公共无效setUser2(用户user2){
this.user2 =用户2;
}
}
目前无论哪种方法我打电话,我仍然得到以下结果:
<$p$p><$c$c>{"user":{"firstName":"John","lastName":"Smith"},"user2":{"firstName":"Smith","lastName":"John"}}
这可能吗?
更新:
我修改了code:
公共类JsonAction扩展了ActionSupport {
私人用户的用户=新用户(约翰福音,史密斯);
私人用户user2 =新用户(史密斯夫妇,约翰);
公共字符串填充(){
返回填充;
}
@Action(值=/的getJSON,结果= {
@Result(NAME =成功,键入=json的,则params = {
includeProperties
用户})})
公共字符串测试(){
返回成功;
}
@Action(值=/ getJson2,结果= {
@Result(NAME =成功,键入=json的,则params = {
includeProperties
用户2})})
公共字符串的test2(){
返回成功;
}
@JSON(名称=用户)
公众用户的getUser(){
返回用户;
}
公共无效SETUSER(用户用户){
this.user =用户;
}
@JSON(名称=用户2)
公众用户getUser2(){
返回用户2;
}
公共无效setUser2(用户user2){
this.user2 =用户2;
}
}
现在我收到
{用户:{}}
和
{用户2:{}}
是的,它是可能的,解决方案需要使用包含/排除的参数。
下面是一个例子。
方法getJson1和getJson2显示includeParameters而getJson3显示excludeParameters。
注:虽然示例使用字符串作为参数的包含/排除的参数字符串PTED作为一个普通的前pression除$ P $。所以,我可以替换为字符串1,字符串的措施3字符串*。
有关更多信息,请参见:<一href="https://cwiki.apache.org/confluence/display/WW/JSON%20Plugin">https://cwiki.apache.org/confluence/display/WW/JSON%20Plugin
包struts2的;
进口com.opensymphony.xwork2.ActionSupport;
进口org.apache.struts2.convention.annotation.Action;
进口org.apache.struts2.convention.annotation.ParentPackage;
进口org.apache.struts2.convention.annotation.Result;
@ParentPackage(JSON-默认)
公共类的Test2扩展了ActionSupport {
私人字符串字符串1 =一;
私人字符串字符串2 =二;
私人字符串其他=其他;
公共字符串getString1(){
返回this.string1;
}
公共字符串getString2(){
返回this.string2;
}
公共字符串getOther(){
返回this.other;
}
@Action(值=/ getJson1,结果= {
@Result(TYPE =json的,则params = {
includeProperties
字符串1
})})
公共串动作1(){
返回ActionSupport.SUCCESS;
}
@Action(值=/ getJson2,结果= {
@Result(TYPE =json的,则params = {
includeProperties
字符串2
})})
公共字符串动作2(){
返回ActionSupport.SUCCESS;
}
@Action(值=/ getJson3,结果= {
@Result(TYPE =json的,则params = {
excludeProperties
字符串1,字符串
})})
公共字符串措施3(){
返回ActionSupport.SUCCESS;
}
}
... / getJson1 返回{字符串1:一}
... / getJson2 返回{字符串2:两个}
... / getJson3 返回{其他:其他}
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"}}
Is it possible?
Update:
I modified the code:
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;
}
}
Now I'm getting
{"user":{}}
and
{"user2":{}}
Yes it is possible, the solution requires the use of include/exclude parameters.
Following is an example.
Methods getJson1 and getJson2 show includeParameters while getJson3 shows excludeParameters.
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*".
For more information see: 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 returns {"string1":"One"}
.../getJson2 returns {"string2":"Two"}
.../getJson3 returns {"other":"Other"}
这篇关于使用JSON插件在Struts 2的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!