@ViewScoped在每个回发中创建新实例 [英] @ViewScoped creating new instance on every postback

查看:185
本文介绍了@ViewScoped在每个回发中创建新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的托管bean.但是,每次我将帖子发回到同一个bean时,即在调用updateFileList时.我得到了FileDAO的新实例.

I am having the below managed bean. But every time I do a post back to the same bean ie while calling updateFileList. I get a new instance of FileDAO.

如何防止这种情况? 在托管bean内安装DAO是否安全?如果没有,我可以进行哪些更改以使其更好.

How can I prevent this? Is it safe to have a DAO inside a managed bean, if not what changes can I make to better it.

@ManagedBean(name = "file")
@ViewScoped
public class FileController implements Serializable {

    private static final long serialVersionUID = 1L;

    private List<LoadFileLog> fileList = null;
    private Date selectedDate;
    FileDAO fileDAO;

    public FileController() {

        System.out.println(" In file Controller constructor");
        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        fileDAO = (FileDAO) context.getBean("FileDAO");


    }


    public FileDAO getFileDAO() {
        return fileDAO;
    }





    public void setFileDAO(FileDAO fileDAO) {
        this.fileDAO = fileDAO;
    }





    public List<LoadFileLog> getFileList() {

        return fileList;

    }

    public Date getSelectedDate() {
        return selectedDate;
    }

    public void setSelectedDate(Date selectedDate) {
        this.selectedDate = selectedDate;
    }

    public void updateFileList() {

        SystemController systemControl = (SystemController) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("system");
        List systemList = new ArrayList();

        if (systemControl != null) {
            systemControl.populateSelectedSystems();
            systemList = systemControl.getSelectedSysIdList();
        }

        if (selectedDate != null) {
            fileList = getFileDAO().getFiles(systemList, selectedDate);
        }
    }

}

谢谢!

推荐答案

通常,完全不应该在回发中重新创建视图范围内的JSF托管Bean.

A view scoped JSF managed bean should normally not be recreated on postbacks at all.

但是,这将在特定情况下发生,所有情况都与 Mojarra问题1492 (顺便说一下,该问题已在即将发布的Mojarra 2.2中修复).当您将诸如JSTL <c:forEach>之类的标记处理程序的属性绑定到该视图作用域的bean的属性时,或者当您将JSF组件binding绑定到该视图作用域的bean的属性时,将重新创建一个视图作用域的bean.解决方案是使用JSF组件而不是JSTL标签,并避免在范围比请求范围更广的bean上使用binding.

This will however happen in specific circumstances, all related to the chicken-egg issue as described in Mojarra issue 1492 (which is fixed for the upcoming Mojarra 2.2 by the way). A view scoped bean will be recreated when you're binding attributes of tag handlers like JSTL <c:forEach> to a property of the view scoped bean, or when you're using JSF component binding to a property of the view scoped bean. The solution would be to use JSF components instead of JSTL tags and to avoid using binding on a bean of a broader scope than the request scope.

  • Communication in JSF 2 - @ViewScoped fails in tag handlers
  • @ViewScoped calls @PostConstruct on every postback request
  • JSF 2.0: Why my ViewScope Beans is re-created even though still on same View

这篇关于@ViewScoped在每个回发中创建新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆