3

嗨,我是 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 的后构造函数。但是当我启动应用程序时,我看到空白页面(因为我的 Controller bean 没有创建)。但是,当我按下调用控制器 bean 方法的链接时,我会在应用程序控制台中创建控制器 bean 并处理后构造函数。

也许我错了,但我认为当 session 创建由 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>
4

1 回答 1

3

尝试使用 PrerenderView 而不是 post 构造:

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

<f:metadata>
     <f:event type="preRenderView" listener="#{controller.init()}"/>
</f:metadata>
于 2013-03-13T12:37:41.193 回答