2013-01-21 21 views
6

私はstruts2-json-plugin-2.2.3.jarを使用しています。そして、このようなクラスのfilterListプロパティをシリアル化しようとしている:JsonのincludeProperties struts2プラグインが動作しません

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クラスのこのプロパティを取得することはできませんよ。

+0

注釈味を好むのparam場合は '値を得ることができますincludeProperties'使用されていませんか? – Quincy

+0

paramのincludePropertiesが使用されていない場合、アクションクラスのすべての属性をシリアル化しています –

答えて

6

@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" 
+1

ありがとう、それは私のために働いた。 –

+0

問題はありませんが、すべてのプロパティをリストするのは面倒だと思います。:/ – Quincy

3

Struts2-jsonプラグインは、アクションクラスのすべてのアクション属性をシリアル化します。

私はstruts2-json-pluginを使用していました。 plugin-docincludePropertiesパラメータの動作例を示していますが、それは私のために働いたことはありませんし、多くの試練やグーグルでは決してしませんでした。だから私はexcludePropertiesを使用して、シリアライズしたいものを指定するのではなく、必要でないコンテンツをシリアライズされたものから削除する必要がありました。あなたのFilterType内のフィールドがiddesc

は場合

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

ohh ...そうですか?これは非常に悪いと思いますので、excludePropertiesを使用して不要なコンテンツをシリアライズされたものから削除しなければなりません。okay thanx ...私はそれをチェックし、もしうまくいくならあなたに戻ってきます。もう一度おねがいします... budyd –

+0

私はjson Plugin 2.3.8で試しましたが、includepropertiesはうまく動作します。あなたのプロジェクトでうまくいかない場合は、json Plugin(org.apache.struts2.json.JSONWriter)のソースコードをトレースすることができます。これは簡単です。 – SalutonMondo

0

を試してみて命名されていると仮定すると、

関連する問題