5

嗨,我是 JSF 的新手,遇到了这样的问题。在我的页面上,我有新闻列表,每个新闻都有复选框(我们可以选中此复选框,然后删除选中的新闻)。这工作正常。但是删除后,我返回我的页面并按 F5,然后我的应用程序认为已选中已删除复选框下方的复选框并将其删除。例如我有这个: 在此处输入图像描述

按删除按钮并拥有这个: 在此处输入图像描述 而不是按 f5,我看到了这个: 在此处输入图像描述

所以我的删除方法如下所示:

Map<Integer, Boolean> allCheckboxes = newsForm.getCheckboxes();
Set<Integer> checkboxes = newsForm.getCheckboxes().keySet();
Set<Integer> checkedCheckboxes = new HashSet<>();
for(Integer id : checkboxes){
    boolean value = allCheckboxes.get(id);
    if(value){
        checkedCheckboxes.add(id);
    }
}
if (checkedCheckboxes.size() != 0) {
    newsDao.deleteNewsById(checkedCheckboxes.toArray());
} else {
    Integer[] delete = { newsForm.getNews().getId() };
    newsDao.deleteNewsById(delete);
}
newsForm.setNewsList(newsDao.getNewsList());
return list() + REDIERCT;

在我的页面上,我使用 selectBooleanCheckbox:

<h:selectBooleanCheckbox id="checkbox" 
                            value="#{newsForm.checkboxes[news.id]}" />

所以我不明白为什么当我按 f5 时,我Map<Integer, Boolean> allCheckboxes的某些元素的值为 true。每次删除后我都会重新创建Map<Integer, Boolean> allCheckboxes

更新 news.xhtml

<h:form id="main-form" onsubmit="return getSelectedCheckBoxes()">
                <h:commandLink styleClass="news-link" action="#{controller.list}"
                    value="#{messages['body.news']}" />
                &gt;&gt;
                <h:outputText value="#{messages['body.news.titles.list']}" />
                <br />
                <ui:repeat id="repeat" var="news" value="#{newsForm.newsList}">
                    <div id="news-table">
                        <div id="news-list-title">
                            <h:outputText value="#{messages['body.news.title']}" />
                            <h:outputText style="margin-left:10px;" value="#{news.newsTitle}" />
                        </div>
                        <div id="news-list-date">
                            <h:outputText value="#{news.newsDate}">
                                <f:convertDateTime pattern="dd/MM/yyyy" />
                            </h:outputText>
                        </div>
                        <div id="news-list-brief">
                            <h:outputText value="#{news.brief}" />
                        </div>
                    </div>
                    <div id="links-style-area">
                        <h:commandLink action="#{controller.view(news.id)}"
                            value="#{messages['body.label.view']}" />
                        <h:commandLink action="#{controller.edit(news.id)}"
                            value="#{messages['body.label.edit']}" />
                        <h:selectBooleanCheckbox id="checkbox" 
                            value="#{newsForm.checkboxes[news.id]}" />
                    </div>
                </ui:repeat>
                <p id="button-style"> 
                    <h:commandButton styleClass="button" action="#{controller.delete}"
                        onclick="clicked = 'deleteList'"
                        value="#{messages['body.button.delete']}"
                        rendered="#{not(empty newsForm.newsList)}" />
                </p>

                <h:outputText id="checkbox-error"
                    value="#{messages['error.error.delete.list']}"
                    styleClass="errorMessage" />
            </h:form>

NewsForm bean:

@ManagedBean(name="newsForm")
@SessionScoped
public class NewsBean implements Serializable{
    private static final long serialVersionUID = 1L;
    private News news;
    private List<News> newsList;
    private Map<Integer, Boolean> checkboxes = new HashMap<>();

    public NewsBean(){}

    public News getNews() {
        return news;
    }
    public void setNews(News news) {
        this.news = news;
    }

    public List<News> getNewsList() {
        return newsList;
    }
    public void setNewsList(List<News> newsList) {
        this.newsList = newsList;
    }

    public Map<Integer, Boolean> getCheckboxes() {
        return checkboxes;
    }

    public void setCheckboxes(Map<Integer, Boolean> checkboxes) {
        this.checkboxes = checkboxes;
    }
}

控制器豆:

@ManagedBean(name="controller")
@SessionScoped
public class ControllerBean implements Serializable{

    private static final long serialVersionUID = 1L;
    private static final String PREVIOUS_PAGE = "previousPage";
    private static final String LIST = "news";
    private static final String EDIT = "edit";
    private static final String VIEW = "view";
    private static final String REDIERCT = "?faces-redirect=true";

    @ManagedProperty(value="#{jpaDao}")
    private INewsDao newsDao;
    @ManagedProperty(value="#{newsForm}")
    private NewsBean newsForm;

    public ControllerBean() {
    }

    @PostConstruct
    public void init() {
        try {
            list();
        } catch (NewsManagerException e) {
        }
    }

    public String list() throws NewsManagerException {
        newsForm.setNews(new News());
        newsForm.setCheckboxes(new HashMap<Integer, Boolean>());
        newsForm.setNewsList(newsDao.getNewsList());
        setAttribute(PREVIOUS_PAGE, LIST);
        return LIST;
    }

    public String add() {
        News news = new News();
        news.setNewsDate(new Date());
        newsForm.setNews(news);
        return EDIT;
    }

    private void setAttribute(String name, String value) {
        Map<String, Object> sessionMap = FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap();
        sessionMap.put(name, value);
    }

    private String getAttribute(String name) {
        Map<String, Object> sessionMap = FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap();
        return (String) sessionMap.get(name);
    }

    public String save() throws NewsManagerException {
        int id = newsForm.getNews().getId();
        setAttribute(PREVIOUS_PAGE, VIEW);
        if (id == 0) {
            newsDao.createNews(newsForm.getNews());
            return VIEW;
        }
        newsDao.editNews(newsForm.getNews());
        return VIEW;
    }

    public String edit(int id) throws NewsManagerException {
        News news = newsDao.getNewsById(id);
        newsForm.setNews(news);
        return EDIT;
    }

    public String view(int id) throws NewsManagerException {
        setAttribute(PREVIOUS_PAGE, VIEW);
        newsForm.setNews(newsDao.getNewsById(id));
        return VIEW;
    }

    public String cancel() throws NewsManagerException {
        String page = getAttribute(PREVIOUS_PAGE);
        News news = newsForm.getNews();
        if (news.getId() != 0) {
            int id = news.getId();
            news = newsDao.getNewsById(id);
            newsForm.setNews(news);
        }
        return page;
    }

    public String delete() throws NewsManagerException, IOException {
        setAttribute(PREVIOUS_PAGE, LIST);
        Map<Integer, Boolean> allCheckboxes = newsForm.getCheckboxes();
        Set<Integer> checkboxes = newsForm.getCheckboxes().keySet();
        Set<Integer> checkedCheckboxes = new HashSet<>();
        for(Integer id : checkboxes){
            boolean value = allCheckboxes.get(id);
            if(value){
                checkedCheckboxes.add(id);
            }
        }
        if (checkedCheckboxes.size() != 0) {
            newsDao.deleteNewsById(checkedCheckboxes.toArray());
        } else {
            Integer[] delete = { newsForm.getNews().getId() };
            newsDao.deleteNewsById(delete);
        }
        newsForm.setNewsList(newsDao.getNewsList());
        return list() + REDIERCT;
    }

    public INewsDao getNewsDao() {
        return newsDao;
    }

    public void setNewsDao(INewsDao newsDao) {
        this.newsDao = newsDao;
    }

    public NewsBean getNewsForm() {
        return newsForm;
    }

    public void setNewsForm(NewsBean newsForm) {
        this.newsForm = newsForm;
    }

}
4

1 回答 1

3

您的具体问题是因为复选框值本质上是请求/视图范围的,但是您的托管 bean 是会话范围的,并且您在删除后没有清除选中的复选框值。

您需要将视图范围的数据放在视图范围而不是会话范围中。

@ManagedBean
@ViewScoped

也可以看看:

于 2013-03-13T14:19:32.713 回答