foreach中的JSF inputText不起作用

时间:2017-03-26 10:34:37

标签: jsf foreach jstl

我研究JSF并且无法在 <h:inputText> 内的 <c:foreach> 中设置我的bean中的值 这是我的代码。

JSF页面

<h:form>
    <c:forEach items="#{commentController.getComments(request.getParameter('item_id'))}" var="comment" varStatus="loop">
        <div class="comment">
            <span class="user">#{comment.user.name}</span>

            <h:outputText value="#{comment.text}" rendered="#{!commentController.editMode}" styleClass="text"/>
            <h:panelGroup rendered="#{commentController.editMode}">
                <h:inputText value="#{comment.text}"/>
                <h:commandButton action="#{commentController.changeComment(comment)}" value="Save">
                    <f:param name="item_id" value="#{request.getParameter('item_id')}"/>
                </h:commandButton>
                <h:commandButton immediate="true" action="#{commentController.switchEditMode}" value="Cancel">
                    <f:param name="item_id" value="#{request.getParameter('item_id')}"/>
                </h:commandButton>
            </h:panelGroup>

            <span class="date">#{comment.date}</span>

            <h:panelGroup rendered="#{commentController.isCurrentUsersComment(comment)}">
                 <h:commandLink rendered="#{!commentController.editMode}" action="#{commentController.switchEditMode}" value="Change comment ">
                     <f:param name="item_id" value="#{request.getParameter('item_id')}"/>
                 </h:commandLink>
                 <h:commandLink rendered="#{!commentController.editMode}" action="#{commentController.deleteComment(comment)}" value="Delete comment"/>
            </h:panelGroup>
        </div>
    </c:forEach>
</h:form>

@ManagedBean
@RequestScoped
public class Comment {

private int id;
private User user;
private String text;
private Date date;
private int itemId;

public Comment() {

}


public String getText() {
    return text;
}

public Date getDate() {
    return date;
}

public void setText(String text) {
    this.text = text;
}

public void setDate(Date date) {
    this.date = date;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public int getItemId() {
    return itemId;
}

public void setItemId(int itemId) {
    this.itemId = itemId;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}
}

控制器

@ManagedBean
@SessionScoped
public class CommentController {

private List<Comment> comments = new ArrayList<>();
private boolean editMode = false;

public CommentController() {

}

public String addComment(Comment comment) {
    java.util.Date date = Calendar.getInstance().getTime();
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    int itemId = Integer.parseInt(request.getParameter("item_id"));
    comment.setUser((User)request.getSession().getAttribute("current_user"));
    comment.setDate(new Date(date.getYear(), date.getMonth(), date.getDay())); //bug
    comment.setItemId(itemId);
    DBUtils.addCommentForItem(comment);
    return "/pages/item.xhtml?item_id=" + itemId +"&faces-redirect=true";
}

public String changeComment(Comment comment) {
    DBUtils.changeCommentForItem(comment);
    return switchEditMode();
}

public String deleteComment(Comment comment) {
    DBUtils.deleteCommentForItem(comment);
    return "/pages/item.xhtml?item_id=" + comment.getItemId() +"&faces-redirect=true";
}

public boolean isCurrentUsersComment(Comment comment) {
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    User currentUser = (User) request.getSession().getAttribute("current_user");
    return currentUser.equals(comment.getUser());
}

public String switchEditMode() {
    editMode = !editMode;
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    int itemId = Integer.parseInt(request.getParameter("item_id"));
    return "/pages/item.xhtml?item_id=" + itemId +"&faces-redirect=true";
}

public List<Comment> getComments(int itemId) {
    comments = DBUtils.getCommentsForItem(itemId);
    return comments;
}

public boolean isEditMode() {
    return editMode;
}
}

在按下按钮&#39; Save&#39; 后,此方法不断获取旧bean。我尝试在 <c:forEach> 中使用 varStatus ,并编写如下内容:

<h:inputText value="#{commentController.getComments(request.getParameter('item_id'))[loop.index].text}">

varStatus="loop" ,但它没有帮助。 也许我以错误的方式使用它,我不知道。请帮忙。如果有必要,我会添加我的代码的其他部分。

1 个答案:

答案 0 :(得分:0)

您已将c:foreach的var参数设置为与请求范围的注册托管bean相同的名称。所以我建议的第一件事是从Comment类中删除托管bean注释;根据您提供的代码,没有理由将其作为一个代码。