java-打开开始页面后未创建SessionScoped bean

前端之家收集整理的这篇文章主要介绍了java-打开开始页面后未创建SessionScoped bean 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

嗨,我是jsf的新手,有这样的问题.我有以下代码的起始页:

@H_404_6@<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具有后构造函数,该后构造函数数据库获取所有新闻并将其设置为newsForm bean.但是,当我启动应用程序时,我看到一个空页面(因为未创建我的Controller bean).但是,当我按下调用控制器bean方法链接时,我在应用程序控制台中找到了控制器bean创建并处理了后构造函数的过程.

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

如果我错了,请纠正我,或者提供建议,让我在打开起始页时如何让我的应用显示所有新闻.

更新

我在Controller bean中的构造函数

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

列出方法

@H_404_6@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类类似)

@H_404_6@    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元素)

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

制图

@H_404_6@<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>
最佳答案
尝试使用PrerenderView而不是post构造:

从ControllerBean删除@postconstruct并添加您的页面

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

猜你在找的Spring相关文章