0

This submits data to author.setFindwithid():

<h:form>
            <h:outputLabel value="Id of to Be Edited record"></h:outputLabel>
            <h:inputText value="#{author.id}"></h:inputText>
            <h:commandButton value="submit" action="#{author.setFindwithid()}"/>
        </h:form>

Here is definition of author.setFindwithid()

public String setFindwithid() throws SQLException{
        String query = "SELECT * FROM authors WHERE id=?";
        PreparedStatement ps = con.prepareStatement(query);
        ps.setInt(1, this.getId());
        ResultSet rs = ps.executeQuery();

        while (rs.next()) {
            Author tmp = new Author();

            tmp.setId(rs.getInt("id"));
            tmp.setName(rs.getString("name123"));
            list.add(tmp);
        }

        return "UpdateAuthor.xhtml";

    }

As it can be seen I have a list and I am also redirecting to UpdateAuthor.xhtml. I want to make this list available in the scope of UpdateAuthor.xhtml so that values of list can been shown in view. How can I do that?

user4913383
  • 171
  • 1
  • 1
  • 11

1 Answers1

0

You can put the List in the flash scope so that it will be kept alive across redirection:

In your setFindwithid method:

FacesContext.getCurrentInstance().getExternalContext().getFlash().put("listName", list);

then you'll be able to reference it in UpdateAuthor.xhtml view:

#{flash.listName}

or get it programmatically in a bean:

List myList = (List) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("listName");

Edit:

As pointed out by BalusC, you can ensure the List will be kept inside the Flash scope even after a page refresh using the following invocation in the target facelet:

#{flash.keep.listName}

See also:

Understand Flash Scope in JSF2

How to keep JSF flash scope parameters on page reload?

Community
  • 1
  • 1
Zim
  • 1,457
  • 1
  • 10
  • 21
  • Does that still work when you F5 the redirected page? – BalusC May 31 '15 at 16:14
  • @BalusC: good point, I've edited my answer to make it work after F5 the redirected page – Zim May 31 '15 at 17:24
  • And when you copypaste the browser address bar URL into a new tab/window? – BalusC May 31 '15 at 19:54
  • Well, for users who want to get an answer on how to make the list available in the scope of the redirected page, Flash scope is an option. For the OP's specific case (and to have the list available when copypasting the URL), he'd better do what you say in your comment: load the list in a bean associated with the target page. – Zim Jun 01 '15 at 05:34