2011-01-10 33 views
8

Tôi có đoạn mã sau và tôi sẽ đạt được chức năng/getJson sẽ trả về đối tượng người dùng là json và/getJson2 sẽ trả về user2 làm đối tượng Json.Vấn đề với plugin Json trong Struts 2

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

Hiện nay không có vấn đề phương pháp Tôi đang gọi tôi vẫn nhận được kết quả sau:

{"user":{"firstName":"John","lastName":"Smith"},"user2":{"firstName":"Smith","lastName":"John"}} 

Có thể?

Cập nhật:

tôi sửa đổi mã:

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

Bây giờ tôi nhận được

{"user":{}} 

{"user2":{}} 
+0

Tôi không thể thấy sự cố.Lúc này tôi sẽ loại bỏ các setters (Tôi không biết nếu bạn có bất kỳ phép thuật mùa xuân nào có thể gây ra sự kỳ quặc), di chuyển User tạo thành một phương thức chuẩn bị và xác minh rằng người dùng không rỗng (xem http: // struts. apache.org/2.0.14/docs/prepare-interceptor.html) và đây chỉ là sở thích cá nhân nhưng @JSON (name = "user") khi áp dụng cho getUser là thừa nên tôi sẽ loại bỏ các chú thích @JSON trên getters. – Quaternion

Trả lời

8

Có thể, giải pháp yêu cầu sử dụng các tham số bao gồm/loại trừ.

Sau đây là ví dụ.

Phương thức getJson1 và getJson2 hiển thị includeParameters trong khi getJson3 hiển thị excludeParameters.

Lưu ý: Mặc dù ví dụ này sử dụng chuỗi làm đối số cho các tham số bao gồm/loại trừ nhưng chuỗi được diễn giải dưới dạng cụm từ thông dụng. Vì vậy, tôi có thể thay thế "string1, string2" trên action3 bằng "string *".

Để biết thêm thông tin xem: 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 lợi nhuận { "string1": "Một"}

.../getJson2 lợi nhuận { "string2" : "Hai"}

.../getJson3 trả về {"other": "Other"}

+0

Xin chào, tôi đã sửa đổi mã theo ví dụ của bạn nhưng bây giờ tôi nhận được chuỗi JSON trống, vui lòng xem cập nhật. Cảm ơn bạn trước. –

2

Hành động này cung cấp hai thuộc tính: user và user2.

Nếu cả hai/getJson và/getJson2 ánh xạ vào lớp hành động này, thì cả hai sẽ phản hồi với các thuộc tính có sẵn: người dùng và người dùng2.

4

Bạn phải bao gồm tất cả các thuộc tính mà bạn muốn tuần tự hóa. Điều này bao gồm các thuộc tính của lớp người dùng như thế này, ví dụ:

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

Nhưng, một hình thức để có được công việc này có thể được sử dụng biểu thức thông thường:

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

trọng.

Các vấn đề liên quan