嗨,我是 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']}" />
>>
<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;
}
}