2014-11-12 5 views
1

다음과 같이 내 액션의 결과 유형을 정의했습니다.struts의 JSON 응답

<package name="nonrest-actions" extends="struts-default,json-default" 
    namespace="/service"> 


    <action name="waterUsageByAggregation" class="action.nonrest.WaterUsageAction" 
     method="usageBreakUp"> 
     <result name="success" type="json" > 
      <param name="root">usageBreakUp</param> 
      <param name="noCache">true</param> 
     </result> 
     <result name="success">NotImplemented.html</result> 
     <result name="input">NotImplemented.html</result> 
    </action> 

</package> 

내 액션 클래스는 다음과 같은 멤버

// input parameters 
private int storageId; 
private Date fromDate; 
private Date toDate; 
private int aggregationId; 

// Output 
private Map<String, String> usageBreakUp = new HashMap<>(); 

하지만 얻을 JSON 응답은 usageBreakUp 루트되지 않습니다 있습니다.

{ 
    "actionErrors":[ 

    ], 
    "actionMessages":[ 

    ], 
    "aggregationId":2, 
    "errorMessages":[ 

    ], 
    "errors":{ 

    }, 
    "fieldErrors":{ 

    }, 
    "fromDate":null, 
    "locale":{ 
     "ISO3Country":"USA", 
     "ISO3Language":"eng", 
     "country":"US", 
     "displayCountry":"United States", 
     "displayLanguage":"English", 
     "displayName":"English (United States)", 
     "displayScript":"", 
     "displayVariant":"", 
     "extensionKeys":[ 

     ], 
     "language":"en", 
     "script":"", 
     "unicodeLocaleAttributes":[ 

     ], 
     "unicodeLocaleKeys":[ 

     ], 
     "variant":"" 
    }, 
    "storageId":1, 
    "texts":null, 
    "toDate":null, 
    "usageBreakUp":{ 
     "3rd Floor":"20", 
     "Ist Floor":"20", 
     "4th Floor":"40", 
     "2nd Floor":"20" 
    } 
} 

무엇이 여기에 있습니까?

편집 :은 여기 내 전체 액션 클래스

public class WaterUsageAction extends ActionSupport 
{ 
    private final static int NO_DATA = -1; 

    /** 
    * Action method which gives the break up of water usage amongst child 
    * aggregations 
    */ 

    // Input parameters 
    private int storageId; 
    private Date fromDate; 
    private Date toDate; 
    private int aggregationId; 

    // Output 
    private Map<String, String> usageBreakUp = new HashMap<>(); 

    // http://localhost:8080/SWNBackend/service/waterUsageByAggregation.json?aggregationId=2&storageId=1 
    public String usageBreakUp() 
    { 
     AggregationDAO aggregationDAO = new AggregationDAO(); 
     Aggregation aggregation = aggregationDAO.findByIdLazy(aggregationId); 

     for (int childAggregationId : aggregation.getAggregationIds()) 
     { 
      Aggregation childAggregation = aggregationDAO 
        .findByIdLazy(childAggregationId); 
      String usageString = String.valueOf(getUsage(
        childAggregation.getId(), fromDate, toDate)); 
      if (usageString.equals(String.valueOf(NO_DATA))) 
       usageString = "No Data"; 
      usageBreakUp.put(childAggregation.getName(), usageString); 
     } 
     return SUCCESS; 
    } 

    /*************************************************************************/ 

    /** 
    * Calculates water usage for the specified aggregation in the specified 
    * time frame 
    * 
    * @param aggregationId 
    * @param from 
    * @param to 
    * @return 
    */ 
    private int getUsage(int aggregationId, Date from, Date to) 
    { 
     int usage = 0; 

     // The nodes through which water flows into this aggregation 
     List<SWNNode> entryNodes = WaterNetwork.getInstance().getEntryNodes(
       aggregationId); 
     if (entryNodes.isEmpty()) 
      throw new RuntimeException(
        "SWN configuration error : No entry points for aggregation " 
          + aggregationId); 

     for (SWNNode entryNode : entryNodes) 
     { 
      int tmpUsage = getUsage(entryNode, from, to); 
      if (tmpUsage == NO_DATA) 
       return NO_DATA; 
      usage += tmpUsage; 
     } 
     return usage; 
    } 

    /** 
    * Calculates water usage w.r.t to the flow through the specified node 
    * 
    * @param entryNode 
    * @param from 
    * @param to 
    * @return 
    */ 
    private int getUsage(SWNNode entryNode, Date from, Date to) 
    { 
     // Base case 
     if (entryNode.getAsset().hasFlowSensor()) 
     { 
      return 20; 
      // TODO : ImplementSensorsDataDAO 
      /* 
      * return (new SensorsDataDAO()).getFlowData(entryNode.getAsset() 
      * .getFlowSensorId(), fromDate, toDate); 
      */ 
     } 
     else if (entryNode.getChildren().isEmpty()) 
      return NO_DATA; 

     // Recursively find the usage 
     int usage = 0; 
     for (SWNNode node : entryNode.getChildren()) 
     { 
      int tmpUsage = getUsage(node, from, to); 
      if (tmpUsage == NO_DATA) 
       return NO_DATA; 
      usage += tmpUsage; 
     } 
     return usage; 
    } 

    public Map<String, String> getUsageBreakUp() 
    { 
     return usageBreakUp; 
    } 

    public void setUsageBreakUp(Map<String, String> usageBreakUp) 
    { 
     this.usageBreakUp = usageBreakUp; 
    } 

    public int getStorageId() 
    { 
     return storageId; 
    } 

    public void setStorageId(int storageId) 
    { 
     this.storageId = storageId; 
    } 

    public Date getFromDate() 
    { 
     return fromDate; 
    } 

    public void setFromDate(Date fromDate) 
    { 
     this.fromDate = fromDate; 
    } 

    public Date getToDate() 
    { 
     return toDate; 
    } 

    public void setToDate(Date toDate) 
    { 
     this.toDate = toDate; 
    } 

    public int getAggregationId() 
    { 
     return aggregationId; 
    } 

    public void setAggregationId(int aggregationId) 
    { 
     this.aggregationId = aggregationId; 
    } 
} 

편집이다 : 소스 here을 찾습니다.

+0

param 속성에 대한 getter가 있습니까? –

+0

@RomanC, 예. getUsageBreakUp() 있습니다. –

+0

조치 클래스 코드를 게시 할 수 있습니까? –

답변

1

당신은 당신의 행동을 두 번 성공 결과를 정의

편집 할 수 있습니다.

+0

당신의 설명이 너무 의미가있는 것 같았습니다. 불행하게도 액션 메소드의 이름을'execute()'로 변경 한 후에도 같은 결과를 얻었습니다. 그건 그렇고, 내 서버 콘솔에 진단 메시지가있다.'2014 년 11 월 13 일 10:08:49 AM net.sf.json.JSONObject _fromBean INFO : action.nonrest.WaterUsageAction 클래스의 'container'속성에 read 메소드가 없다. . SKIPPED '. 도움이 될까요? –

+0

@AndreaLigious, 여기에서 전체 소스를 찾을 수 있습니다. https://github.com/smartwatermanagement/self-healing-water-networks/tree/master/SWNBackend/src –

+1

SUCCESS 결과를 두 번 정의했습니다. –