I am trying to bind multiple inputText fields to a HashMap<Integer, String>
. But it doesn't put any value into the HashMap
.
Here is the JSF page.
<ui:repeat value="#{questionBean.question.answerCollection}" var="answer">
<h:inputText value="#{questionBean.newComments[answer.answerId]}"></h:inputText>
<br/>
<h:commandButton value="Add Comment">
<f:ajax event="click" listener="#{questionBean.makeComment(answer)}"></f:ajax>
</h:commandButton>
</ui:repeat>
Here is related part of the backing bean.
private Map<Integer, String> newComments = new HashMap<Integer, String>();
...
public void makeComment(Answers answer) throws Exception {
String username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
Users user = userFacade.getUserByUsername(username);
Comments comment = new Comments();
for(Integer key: newComments.keySet()){
throw new Exception("Answer ID IS :"+key);
// It doesn't throw any exception in here.
// The map is empty.
}
String commentContent = newComments.get(answer.getAnswerId());
if(commentContent == null){
throw new Exception("Content Is NULL");
// It throws exception here.
}
comment.setCommentContent(newComments.get(answer.getAnswerId()));
comment.setDateCreated(new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
comment.setAnswerId(answer);
comment.setUserId(user);
commentFacade.create(comment);
}
What may be wrong in this code?