2012-11-17 125 views
1

我只有一个想法,即实现获取用户的答案。我决定使用List<String> questionsWithAnswers,并将它放在这个列表中,如'1_1','1_2','1_3','2_1'。像这样的东西。但即使有这个想法,我有一个问题。同样重要的是我将使用web服务(这是使用map作为结构来保存问题和答案的限制)。调查问卷获得结果JSF

********我的豆********

public class TestBean implements Serializable{ 

     private static final long serialVersionUID = 1L; 

     private List<Test> testList; 
     private List<Answer> answerList; 
     private List<Subject> subjectList; 
     private Map<Question, List<QuestionAnswer>> mapQestionsWithAnswers; 
     private List<String> questionAnswerList; 

     private Long subjectId = 0L; 
     private Test test; 
//... 
} 

getTest.xhtml

<c:forEach items="#{test.testBean.mapQestionsWithAnswers}" 
        var="entry"> 
        <h:inputHidden value="#{entry.key.questionId}" /> 
        <h:outputText value="#{entry.key.question}" rendered="#{not empty entry.value}"/>     
        <h:selectManyCheckbox value="#{test.testBean.questionAnswerList}" layout="pageDirection"> 
         <f:selectItems value="#{entry.value}" var="ans" 
          itemValue="#{fnc:concat(ans.answer.answerId, entry.key.questionId)}" 
          itemLabel="${ans.answer.answer}" />       
        </h:selectManyCheckbox> 
       </c:forEach> 
       <h:commandButton value="#{msgs['page.content.passtest']}" action="#{test.passTest}" /> 

对于concatination questionId与answerId我使用CONCAT。我觉得这是在Concatenate strings in JSF/JSP EL and Javascript

但我不能得到价值entry.key.questionIditemValue="#{fnc:concat(ans.answer.answerId, entry.key.questionId)}"

我不明白为什么。

我该在哪里犯错?我想问一些更多的逻辑来源和简单的决策类似的问题。谢谢

+1

为什么areyou在JSF项目2使用JSTL?更好地使用jsf''标签 –

+0

@SteveAtkinson,因为我阅读了有关jsf – Ray

+1

中的输出Map的问题,使用ui地图或列表从未遇到过问题:重复。 JSTL标签的问题在于它们在生命周期的错误部分执行并导致意外的结果。见http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense –

回答

2

你并不需要连接:

itemValue="#{fnc:concat(ans.answer.answerId, entry.key.questionId)}" 

可能是简单的:

itemValue="#{ans.answer.answerId}, #{entry.key.questionId}" 
+0

你如何看待'List '和'JSTL'是'ui:repeat'的主意? – Ray

+0

我同意ui:repear和c:forEach不会在相同的生命周期中执行,因此可能会导致问题。 –

+0

你更喜欢案例图中的jstl或jsf标签吗? – Ray

1

为什么不使用自己的方法来连接?您可以在getTest.xhtml使用具有方法命名豆concatenatorBean

String customConcat(String str1, String str2) {
   return str1+"_"+str2;
}

然后你可以使用自己的连接符来解决这个问题。

+0

我的问题是我得到空questionAnswerList – Ray

+0

如何初始化questionAnswerList?你可以在创建TestBean时初始化它(在TestBean的构造函数中)。 –

+0

我在构造函数TestBean中初始化列表。问题是这个列表是空的,而不是空的。 – Ray