如何在此方案中重定向?

时间:2015-05-31 11:43:41

标签: jsf jsf-2.2

这会将数据提交到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>

以下是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";

    }

可以看出我有一个list,我也正在重定向到UpdateAuthor.xhtml。我想在list的范围内提供此UpdateAuthor.xhtml,以便可以在视图中显示list的值。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以将List放入闪存范围,以便在重定向时保持活动状态:

find_theorems方法中:

setFindwithid

然后您就可以在FacesContext.getCurrentInstance().getExternalContext().getFlash().put("listName", list); 视图中引用它:

UpdateAuthor.xhtml

或以bean编程方式获取它:

#{flash.listName}

修改

正如BalusC所指出的,即使在目标facelet中使用以下调用进行页面刷新后,您也可以确保List myList = (List) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("listName"); 保留在Flash范围内:

List

另见:

Understand Flash Scope in JSF2

How to keep JSF flash scope parameters on page reload?