【发布时间】:2014-11-02 01:52:20
【问题描述】:
我正在尝试将多个 inputText 字段绑定到 HashMap<Integer, String>。但它没有为HashMap 赋予任何价值。
这里是 JSF 页面。
<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>
这里是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);
}
这段代码可能有什么问题?
【问题讨论】: