【问题标题】:How do I redirect in this scenario?在这种情况下如何重定向?
【发布时间】:2015-05-31 11:43:40
【问题描述】:

这会将数据提交给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。我想让这个listUpdateAuthor.xhtml 的范围内可用,以便list 的值可以显示在视图中。我该怎么做?

【问题讨论】:

  • 问题已通过this解决
  • 该问题涉及不同的问题。您可以在与目标页面关联的 bean 中加载列表。

标签: jsf jsf-2.2


【解决方案1】:

您可以将 List 放在 flash 范围内,以便在重定向期间保持活动状态:

在您的setFindwithid 方法中:

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

那么您就可以在UpdateAuthor.xhtml 视图中引用它:

#{flash.listName}

或以编程方式在 bean 中获取它:

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

编辑:

正如 BalusC 所指出的,您可以确保 List 将保留在 Flash 范围内,即使在页面刷新之后使用目标 facelet 中的以下调用:

#{flash.keep.listName}

另请参阅:

Understand Flash Scope in JSF2

How to keep JSF flash scope parameters on page reload?

【讨论】:

  • 当您按 F5 重定向页面时这仍然有效吗?
  • @BalusC:好点,我已经编辑了我的答案,使其在 F5 重定向页面后工作
  • 当您将浏览器地址栏 URL 复制粘贴到新选项卡/窗口中时?
  • 好吧,对于想要获得有关如何使列表在重定向页面范围内可用的答案的用户,Flash 范围是一个选项。对于 OP 的特定情况(并且在复制粘贴 URL 时列表可用),他最好按照您在评论中所说的:将列表加载到与目标页面关联的 bean 中。
猜你喜欢
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 2016-11-15
相关资源
最近更新 更多