2

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.

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);
        }
    }

}

Thanks!

3 Answers 3

5

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

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.

See also

4
  • But, I dont have JSTL or JSF component binding in my xhtml.Where does the creation of new bean happen on subsequent calls? How do I make sure that the bean even though is newly created gets created via spring.( if not things like jdbctemplate are not initialised and it fails with a nullpointer on accessing the database) Jan 4, 2012 at 16:04
  • I don't do Spring, so I won't go in that detail. Your bean is annotated using JSF javax.faces.bean annotations. They look fine so far and I assume that you didn't configure them by Spring (but just used Spring for its JDBC template or something). The JSF managed bean constructor is supposed to be called when the bean does not exist in the current scope yet. The view scope should live as long as you're interacting with the same JSF view in the same browser tab/window (continue in next comment)
    – BalusC
    Jan 4, 2012 at 16:19
  • If that really doesn't work out for you, then perhaps your session management is broken. Verify if the same session cookie is sent in subsequent requests. Try with JSF state saving method set to client instead of server in web.xml.
    – BalusC
    Jan 4, 2012 at 16:21
  • The spring was used only to do a lookup of FileDAO and this is done in the constructor. But I see the on a call to updateFileList from xhtml the fileDAO instance is not the one created via the constructor. it is a new one. but how can it be created without a constructor call? Jan 5, 2012 at 5:56
0

What is a bean scope of fileDAO?

It would be better to get spring inject that dependency without getting application context from servlet one and then retrieving bean from it.

1
  • I can get the fileDAO initialised by Spring in the constructor, but on subsequent call to updateFileList , I get a different fileDAO, not the one I set in the constructor. Jan 4, 2012 at 15:00
0

suggest using Deltaspike to maintain the controller scope is better and stable behavior for handling the instance of your controller

<dependency>
    <groupId>org.apache.deltaspike.modules</groupId>
    <artifactId>deltaspike-jsf-module-api</artifactId>
    <version>${deltaspike.version}</version>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>org.apache.deltaspike.modules</groupId>
    <artifactId>deltaspike-jsf-module-impl</artifactId>
    <version>${deltaspike.version}</version>
    <scope>runtime</scope>
</dependency>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.