Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SelectBooleanCheckbox in jsf

Tags:

java

jsf

hi I'm new in JSF and have such problem. On my page I have list of news and each news has checkbox(We can make this checkboxes checked and than delete checked news). This works fine. But after delete I return back to my page and press F5 and then my app think that checkbox that was bellow the deleted one is checked and deletes it. for example I have this : enter image description here

press delete button and have this: enter image description herethan press f5 and I see this: enter image description here

so My delete method look like this :

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;

at my page i use selectBooleanCheckbox:

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

so I don't undestand why when I press f5 my Map<Integer, Boolean> allCheckboxes has some elements with value true. also after each delete I recreate Map<Integer, Boolean> allCheckboxes

UPDATE 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;
    }
}

controller bean:

@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;
    }

}
like image 884
Aleksei Bulgak Avatar asked Oct 21 '22 15:10

Aleksei Bulgak


1 Answers

Your concrete problem is caused because the checkbox values are essentially request/view scoped, but your managed bean is session scoped and you didn't clear the checked checkbox values after deletion.

You need to put view scoped data in the view scope instead of in the session scope.

@ManagedBean
@ViewScoped

See also:

  • How to choose the right bean scope?
like image 192
BalusC Avatar answered Oct 26 '22 22:10

BalusC