日期:2014-05-17  浏览次数:20632 次

iterator 两层嵌套问题,希望高手们指点下,感激不尽
小弟最近在做一个人问卷调查,一个问卷主题对应N个问题,一个问题对应N个答案,都是从数据库取数据,现在遍历了问题列表,在问题列表下面再遍历答案列表出现问题,下面贴一下主要代码。
public String surveyView(){
logger.debug("进入InvestigateAction surveyView 方法");
String surveyId = this.getRequest().getParameter("id");
if(surveyQuestion == null){
surveyQuestion = new SurveyQuestion();
surveyQuestion.setThemeId(Integer.parseInt(surveyId));
}
questionList = investigateService.getQuestionList(surveyQuestion);
for(int i=0;i<questionList.size();i++){
SurveyQuestion surveyQuestion=questionList.get(i);
logger.debug("---------------------------"+surveyQuestion.getQuestionId());
if(surveyQuestionAnswer == null){
surveyQuestionAnswer = new SurveyQuestionAnswer();
}
surveyQuestionAnswer.setQuestionId(surveyQuestion.getQuestionId());
int num = investigateService.getQuestionCount(surveyQuestionAnswer);
List<SurveyQuestionAnswer> anwerListTemp = new ArrayList<SurveyQuestionAnswer>();
anwerListTemp = investigateService.getAnswerList(surveyQuestionAnswer);
for(int j=0;j<anwerListTemp.size();j++){
SurveyQuestionAnswer surveyQuestionAnswer = anwerListTemp.get(j);
logger.debug("================="+surveyQuestionAnswer.getQuestionId());
int count = surveyQuestionAnswer.getAnswerCount();
if(num != 0){
int ratio = (count*100)/num;
surveyQuestionAnswer.setAnswerCountRadio(ratio);
}
}
answerList.addAll(anwerListTemp);
}
return "surveyView";
}

上边是遍历List的后台,在JSP页面显示如下:
<ul data-role="listview" data-inset="true" data-split-theme="d">
     <s:iterator value="questionList" id="question">
         <li>
<h1><s:property value="#question.questionNum"/>,
<s:property value="#question.questionTitle"/>
<s:if test="#question.chooseType == 0">(单选)</s:if>
<s:else>(多选)</s:else>
</h1>
<fieldset data-role="controlgroup">
<s:iterator value="answerList" id="answer">
<s:if test="#question.questionId == #answer.questionId">
<s:if test="#question.chooseType == 0">
    <input name="<s:property value="#question.questionId"/>" id="<s:property value="#answer.questionAnswerId"/>" type="radio">
     <label for="<s:property value="#answer.questionAnswerId"/>"><s:property value="#answer.answerTitle"/></label>
</s:if>
<s:else>
         <input name="<s:property value="#question.questionId"/>" id="<s:property value="#answer.questionAnswerId"/>" type="checkbox">
     <label for="<s:property value="#answer.questionAnswerId"/>"><s:property value="#answer.answerTitle"/></label>
</s:else>
</s:if>
</s:iterator>
</fieldset>
</li>
     </s:iterator>
     </ul>

现在问题是无法根据问题ID来获取到对应的答