@ViewScoped object is destroyed even if the same JSF page is displayed

Solution Unverified - Updated -

Issue

I have JSF application using javax.faces.view.ViewScoped object like the following. However, the ViewScoped bean is destroyed after the action even if the same JSF page is displayed. How do I keep the ViewScoped bean on the same page?

  • example.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:jsf="http://xmlns.jcp.org/jsf">
<head jsf:id="head">
<meta charset="UTF-8" />
<title>example</title>
    <form jsf:id="form" jsf:prependId="false">
        ...
        <input type="submit" jsf:action="#{exampleBackingBean.execute(download)}" value="click" />
    </form>
</h:body>
</html>
  • ExampleBackingBean.java
@Model
@Named(value="exampleBackingBean")
public class BackingBean implements Serializable {

    @Inject
    ExampleFileList filelist;

    public String execute(String action) throws Exception {
       if (action.equals("download") {
          // application processing against the filelist object like file download...

          // back to the same example.xhtml
          return "example.xhtml";
       } else { 
          // other application processing ...
          return "other.xhtml";
       }
    }
    ...
  • ExampleListBean.java
@Named(value = "list")
@ViewScoped
public class ExampleFileList implements Serializable{
    List<String> file = new ArrayList<String>();

    @PostConstruct
    public void init(){
        // initialize file list
    }

    public List<String> getFile() {
        return file;
    }   

    public void setFile(List<String> file) {
        this.file = file;
    }

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 7.x
  • JSF

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.