打开起始页后未创建SessionScoped bean



你好,我是新来的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注释。控制器bean具有从数据库获取所有新闻并将其设置为newsForm bean的后构造函数。但是当我启动应用程序时,我看到空页面(因为我的控制器bean没有创建)。但是,当我按下链接调用控制器bean方法时,我在应用程序控制台上看到控制器bean被创建并且后期构造函数被处理。

也许我错了,但我认为,当会话创建jsf创建的所有SessionScoped bean。

纠正我,如果我错了或给出建议如何使我的应用程序显示所有的新闻,当我打开开始页。

我在Controller bean中的post构造函数:

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

list method:

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>

尝试使用preenderview代替post construct:

从ControllerBean中删除@postconstruct,并在页面中添加:

<f:metadata>
     <f:event type="preRenderView" listener="#{controller.init()}"/>
</f:metadata>

相关内容

  • 没有找到相关文章

最新更新