2013-01-21 2 views
6

struts2-json-plugin-2.2.3.jar을 사용하고 있습니다.Json struts2 플러그인의 includeProperties가 작동하지 않습니다.

struts.xml 코드

<action name="jsonUserFilterListAction" class="com.my.fitnessb.actions.UserArticlesAction" method="filterList"> 
     <result name="success" type="json"> 
      <param name="includeProperties">filterList</param> 
     </result> 
</action> 

Action 클래스

public class UserArticlesAction extends ActionSupport implements SessionAware, ServletRequestAware { 

    private List<FilterType> filterList; 
    HttpServletRequest request; 

    public String filterList() { 
    filterList = new ArrayList<FilterType>(); 
    filterList.add(new FilterType(10, "Latest Articles")); 
    filterList.add(new FilterType(1, "Trending Articles")); 
    filterList.add(new FilterType(2, "Top Rated Articles")); 
    filterList.add(new FilterType(3, "Most Viewd Atricles")); 
    filterList.add(new FilterType(4, "All Atricles")); 
    return SUCCESS; 
    } 
    //setter & getter of filterList 
} 

을하지만 난 FilterType 클래스의이 속성을 얻을 수 아니에요 :이 같은 클래스의 filterList 속성을 직렬화하려고합니다.

+0

당신이 PARAM 경우 '값을 얻을 수있는 주석 맛을 선호 시도 iddesc

명명 된 includeProperties' 사용하지 않습니까? – Quincy

+0

param includeProperties가 사용되지 않으면 액션 클래스에서 모든 속성을 seralize하고 있습니다. –

답변

6

당신의 FilterType 경우

<param name="includeProperties"> 
filterList\[\d+\]\.id, 
filterList\[\d+\]\.desc 
</param> 
+1

고맙습니다. 저에게 도움이되었습니다. –

+0

문제는 없지만, 모든 속성을 나열하는 것이 번거롭다 고 생각합니다. : / – Quincy

3

Struts2-json 플러그인은 조치 클래스의 모든 조치 속성을 seralize합니다.

struts2-json-plugin을 사용하면서 직면 한 문제입니다. plugin-docincludeProperties 매개 변수에 대한 작동 예제를 보여 주었지만 결코 저에게 효과가 없었으며 많은 시련과 인터넷 검색을 한 적이 없었습니다. 그래서 excludeProperties을 사용하여 serialize하려는 내용을 지정하는 대신 필수 항목이 아닌 serialize 된 내용을 제거해야했습니다. 의 필드를 가정

+0

오 ... 그렇습니까? 이것은 매우 나쁘다고 생각합니다. 그래서 excludeProperties를 사용하여 필수가 아닌 내용을 직렬화에서 제거해야합니다. 좋아요. 고맙습니다. 내가 확인하고 다시 작동하게되면 다시 돌아올 것입니다. 다시 감사드립니다 ... buddy –

+0

나는 json plugin 2.3.8을 사용해 보았는데 includeproperties는 잘 동작한다. 프로젝트에서 작동하지 않는다면 json plugin (org.apache.struts2.json.JSONWriter)의 소스 코드를 추적 할 수 있습니다. – SalutonMondo

0

@ParentPackage("json-default") 
@Namespace("/") 
@ResultPath(value = "/") 
@Results({ @Result(name="firstDir",type="json" 
,params = {"includeProperties","fileList\\[\\d+\\]"} 
) }) 

fileList = new ArrayList<String>(); 
     for (File img : folder.listFiles()) { 
      fileList.add(img.getName()); 
     } 
return "firstDir"