gpt4 book ai didi

java - 将变量传递给另一个 View : best practice

转载 作者:行者123 更新时间:2023-12-01 23:48:43 24 4
gpt4 key购买 nike

我在 View 中有一个表单,例如:

form.xhtml

<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="Name" />
<h:inputText value="#{bean.name}" />

<h:outputLabel value="Age" />
<h:inputText value="#{bean.age}"
converter="#{ageConverter}" />

<h:outputLabel value="" />
<h:commandButton action="#{bean.submit}"
value="Submit" />
</h:panelGrid>
</h:form>

由以下 bean 支持:

Bean.java

@Named
// Scope
public class Bean implements Serializable {
@Inject private Service service;

private String name;
private int age;
private List<Person> people;

public void submit() {
people= service.getPeople(name, age);
}

// getters & setters for name & age
// getter for people
}

导致的 View :

结果.xhtml

<h:form>
<h:dataTable value="#{bean.people}"
var="person">
<h:column>
<f:facet name="header">Name</f:facet>
#{person.name}
</h:column>

<h:column>
<f:facet name="header">Day of Birth</f:facet>
#{person.dayOfBirth}
</h:column>
</h:dataTable>
</h:form>

现在显然用例类似于: - 使用form.xhtml提交表单 - 使用Bean.java从服务中获取人员 - 向人们展示使用 result.xhtml

在此示例中,拼图仍有一小部分不完整。例如,范围对于结果中是否有 people 至关重要,此外,结果页面没有转发(或类似的内容)。

现在我不确定实现此目标的最佳(或至少是良好)方法是什么。以下是我能想到的一些方法:

  • 使用 @ViewScoped (JSF2.2) 和隐式导航(从 submit() 返回 String)导航到第二页。然而,这打破了视野(无论如何要完成这个)?
  • 使用@ViewScoped并包含基于rendered=''和一些EL的正确文件(form.xhtml或result.xhtml)。这可以通过提交时的 Ajax 调用来完成。
  • 将值 nameage 作为请求中的 GET 参数传递给 result.xhtml,并在 @PostConstruct 上执行逻辑(但是,如果表格“巨大”怎么办)?在这种情况下,@RequestScoped 就足够了。

我的问题是,完成这个用例的有效且良好(最佳)的方法是什么?

感谢您的投入。

最佳答案

我认为您正在寻找的是 flash scope 。它在重定向后仍然存在,但在后续请求中不再可用。

解决方案是通过 Flash 范围传递您的 nameage 变量,并使用它们来渲染结果。

关于java - 将变量传递给另一个 View : best practice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16622251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com