嗨,我是jsf的新手,有这样的问题。我有这段代码的起始页面:
<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>
我有2个托管bean,其中一个是我的控制器bean(用于删除,创建,更新新闻),第二个用于存储有关数据库中新闻的信息(newsForm)。所有bean都有@SessionScoped
注释。 Controller bean具有post-constructor,它从数据库获取所有新闻并将其设置为newsForm bean。但是当我启动应用程序时,我看到空页面(因为我的Controller bean没有创建)。但是当我在创建控制器bean的app控制台中按下调用控制器bean方法I sea的链接并处理后构造函数时。
也许我错了,但我认为会话创建了由jsf创建的所有SessionScoped bean。
如果我错了,请纠正我,或者在我打开首页时提供如何让我的应用显示所有新闻的建议。
更新
我在Controller bean中的post构造函数:
@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;
}
更新2
这是ControllerBean(这类似于Struts中的Action类)
public final class ControllerBean implements Serializable {
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 REDIRECT = "?faces-redirect=true";
private final static Log log = LogFactory.getLog(ControllerBean.class);
private INewsDao newsDao;
private NewsBean newsForm;
public ControllerBean() {
}
@PostConstruct
public void init() {
list();
}
public String list() {
newsForm.setNews(new News());
newsForm.setCheckboxes(new HashMap<Integer, Boolean>());
try {
newsForm.setNewsList(newsDao.getNewsList());
} catch (NewsManagerException e) {
log.error(e);
}
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() {
int id = newsForm.getNews().getId();
setAttribute(PREVIOUS_PAGE, VIEW);
try {
if (id == 0) {
newsDao.createNews(newsForm.getNews());
return VIEW;
}
newsDao.editNews(newsForm.getNews());
} catch (NewsManagerException e) {
log.error(e);
}
return VIEW;
}
public String edit(int id) {
try {
News news = newsDao.getNewsById(id);
newsForm.setNews(news);
} catch (NewsManagerException e) {
log.error(e);
}
return EDIT;
}
public String view(int id) {
try {
setAttribute(PREVIOUS_PAGE, VIEW);
newsForm.setNews(newsDao.getNewsById(id));
} catch (NewsManagerException e) {
log.error(e);
}
return VIEW;
}
public String cancel() {
String page = getAttribute(PREVIOUS_PAGE);
News news = newsForm.getNews();
try {
if (news.getId() != 0) {
int id = news.getId();
news = newsDao.getNewsById(id);
newsForm.setNews(news);
}
} catch (NewsManagerException e) {
log.error(e);
}
return page;
}
public String delete() {
try {
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());
} catch (NewsManagerException e) {
log.error(e);
}
return list() + REDIRECT;
}
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;
}
}
NewsBean(简单bean在Struts中作为ActionForm元素工作)
public final 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;
}
}
映射
<managed-bean>
<managed-bean-name>controller</managed-bean-name>
<managed-bean-class>com.epam.newsmanagement.beans.ControllerBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>newsDao</property-name>
<value>#{jpaDao}</value>
</managed-property>
<managed-property>
<property-name>newsForm</property-name>
<value>#{newsForm}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>newsForm</managed-bean-name>
<managed-bean-class>com.epam.newsmanagement.beans.NewsBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
答案 0 :(得分:3)
尝试使用PrerenderView而不是post constru:
从ControllerBean中删除@postconstruct并添加到您的页面中:
<f:metadata>
<f:event type="preRenderView" listener="#{controller.init()}"/>
</f:metadata>