注意:这个问题可能与Vaadin有关,也可能与Vaadin无关,这取决于是否有一个“更好”的解决方案来“重置”bean。
背景场景
我正在构建一个向导,用于输入一些值,这些值在完成后被发送到一个表中(使用Vaadin和附加的“Vaadin向导”)。
外接程序没有提供一种方法来重置向导(即回到步骤1),而不强制调用当前步骤(覆盖) onAdvance()和onBack()方法,这些方法在我的一些步骤中将返回false,因为我在这些方法中使用逻辑,以防使用时没有填写所有必需的数据。
我不能简单地创建向导的新实例,因为我使用Spring来管理这个@组件。
因此,这使我需要实际重置bean,以便正确地重置向导。
我的问题是
如何“重置”Spring托管Bean (@Component)?我应该补充一点,这个Bean也有一些依赖项注入到它。
或者..。(对瓦丁人而言):
除了创建新向导之外,还有其他方法重置此向导吗?
一些代码
@Component
@Scope("session")
public class MyWizard extends Wizard {
@Inject
private MyWizardContainer myWizardContainer;
@Inject
private MyService myService;
@Inject
private MyWizardStep1 myWizardStep1;
@Inject
private MyWizardStep2 myWizardStep2;
@Inject
private MyWizardStep3 myWizardStep3;
@Inject
private MyMainTableContainer myMainTableContainer;
final static Logger logger = LoggerFactory.getLogger(MyWizard.class);
private static final long serialVersionUID = 1L;
public MyWizard() {
setupListener();
}
public void addSteps() {
this.addStep(myWizardStep1);
this.addStep(myWizardStep2);
this.addStep(myWizardStep3);
this.mainLayout.setComponentAlignment(this.footer, Alignment.BOTTOM_LEFT);
}
private void setupListener() {
this.addListener(new WizardProgressListener() {
@Override
public void wizardCompleted(WizardCompletedEvent event) {
endWizard("Wizard Finished Successfully!");
}
@Override
public void wizardCancelled(WizardCancelledEvent event) {
endWizard("Wizard Cancelled!");
}
@Override
public void stepSetChanged(WizardStepSetChangedEvent event) {
// TODO Auto-generated method stub
}
@Override
public void activeStepChanged(WizardStepActivationEvent event) {
// TODO Auto-generated method stub
}
});
}
private void resetWizard() {
myWizardContainer.removeAll(); //here I'm simply resetting all data that the user generated thus far in the wizard
this.activateStep(myWizardStep1); //this will not work, as some steps will not always return true on onBack() and/or onAdvance()
}
private void endWizard(String message) {
resetWizard();
this.setVisible(false);
Notification.show(message);
}
}发布于 2014-01-07 21:36:05
您可能使用的SpringVaadinIntegration并不要求所有元素都是@Components,只需要对UI进行注释。
在本例中,向导和它的步骤不应该是组件,如果确实需要向它们注入依赖项,可以使用@Configurable注释,它允许将依赖项注入到Spring未管理的类(需要更多的配置才能使其工作)。只需在需要时将向导和步骤创建为新对象即可。
https://stackoverflow.com/questions/20974420
复制相似问题