在JSF 2.0中用于向导模式的范围是什么? [英] What scope to use in JSF 2.0 for Wizard pattern?
问题描述
我有一个多页表单,又称向导模式,其中第1页对应于向导表单的第1步,第2页对应于第2步,依此类推.除最后一页外,每个页面上都有一个Next按钮,将您带到表单的下一页.最后一页上有一个提交按钮,用于提交向导中所有页面的所有数据.
I have a multi-page form, aka a Wizard pattern, where Page 1 corresponds to Step 1 of the wizard form, Page 2 corresponds to Step 2, etc. Each page other than the last has a Next button on it that takes you to the next page in the form. The final page has a submit button that submits all the data for all pages in the wizard.
我应该使用什么范围来维护在每个表单上输入的数据的状态?例如我应该使用包含在所有页面上输入的所有数据的View Scoped bean吗?因为我将导航到不同的页面(我认为这被认为是不同的视图";所以如果我认为这是不同的视图",则我相信,当您导航到下一个页面时,视图范围"数据将丢失)向导)
What scope should I use to maintain the state of the data entered on each form? e.g. should I use a View Scoped bean that holds all the data entered on all pages? Will that work since I'll be navigating to different pages (Which I believe are considered to be different "views"; and if they're different views, I believe the View Scoped data will be lost when you navigate to the next page in the wizard)
推荐答案
我相信,当您导航到向导的下一页时,查看范围"数据将会丢失)
是正确的.只要您与同一个视图进行交互,该视图作用域就会存在,并在创建新视图时将其丢弃.您正在寻找对话范围".任何JSF托管Bean范围都无法使用此功能.但是CDI @ConversationScoped
.因此,如果您的环境碰巧支持CDI,则可以使用它:
That's correct. The view scope lives as long as you're interacting with the same view and get trashed whenever a new view get created. You're looking for the "conversation scope". This isn't available by any of the JSF managed bean scopes. This is however available by CDI @ConversationScoped
. So if your environment happen to support CDI, you could make use of it:
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@ConversationScoped
public class Wizard implements Serializable {
@Inject
private Conversation conversation;
@PostConstruct
public void init() {
conversation.begin();
}
public void submitFirstStep() {
// ...
}
// ...
public String submitLastStep() {
// ...
conversation.end();
return "someOtherPage?faces-redirect=true";
}
// ...
}
通过自动插入的cid
请求参数来管理会话.
The conversation is managed by the automatically inserted cid
request parameter.
如果您希望坚持使用JSF视图范围,那么最好的选择是创建一个页面,在其中您可以有条件地呈现多个步骤:
If you'd like to stick to the JSF view scope, then your best bet is to create a single page wherein you render the multiple steps conditionally:
<h:panelGroup rendered="#{wizard.step == 1}">
<ui:include src="/WEB-INF/wizard/step1.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{wizard.step == 2}">
<ui:include src="/WEB-INF/wizard/step2.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{wizard.step == 3}">
<ui:include src="/WEB-INF/wizard/step3.xhtml" />
</h:panelGroup>
或者,您可以使用第三方组件库,例如 PrimeFaces ,该库具有
Or, you could use a 3rd party component library like PrimeFaces which has a <p:wizard>
component for exactly this purpose.
这篇关于在JSF 2.0中用于向导模式的范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!