0

ZK Spring 3, Scope problem

asked 2011-08-26 00:50:03 +0800

Samu gravatar image Samu
39 2

Hi All

I confuse about ZK Spring scope (ZK Spring 3 + ZK 5.0.7). I have main page scope_test.zul with ScopeTestController and popup page call scope_test_popup.zul with ScopeTestPopupController as bellow.

scope_test.zul

<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<?variable-resolver class="org.zkoss.spring.DelegatingVariableResolver"?> 
<zk>
<window id="scopeTestWin" apply="${scopeTestCtrl}"  title="Main Page" border="0" width="100%" height="auto">
	<button id="openPopup" label="Open Popup" />
</window>
</zk>


ScopeTestController.java
@Controller("scopeTestCtrl")
@Scope("prototype")
public class ScopeTestController extends GenericSpringComposer {
	
	@Autowired
	private ScopeTestModel scopeTestModel;
	
	@Autowired
	private Window scopeTestWin;
	@Autowired
	private Button openPopup;
	
	private boolean sameObj = false;
	
	@EventHandler("scopeTestWin.onCreate")
	public void doCreateWindow(Event event){
		System.out.println("scopeTestCtrl");
		if(sameObj){
			System.out.println("old bean");
		}else{
			System.out.println("new bean");
			sameObj = true;
		}
		
		if(scopeTestModel.getModelName() != null){
			System.out.println("old model bean");
		}else{
			System.out.println("new model bean");
			scopeTestModel.setModelName("Main Window");
		}
		
		System.out.println("openPopup-->[" + openPopup.hashCode() + "] label=" + openPopup.getLabel());
	}
	
	@EventHandler("openPopup.onClick")
	public void doClickOpenPopup(Event event) throws SuspendNotAllowedException, InterruptedException{
		System.out.println("scopeTestCtrl openPopup");
		Window popupWin = (Window) Executions.getCurrent().createComponents("/exam23/scope_test_popup.zul",scopeTestWin, null);
		popupWin.doModal();
	}

}


scope_test_popup.zul
<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<?variable-resolver class="org.zkoss.spring.DelegatingVariableResolver"?> 
<zk>
<window id="scopeTestPopupWin" apply="${scopeTestPopupCtrl}" title="This is popup" border="normal" width="200px" height="100px" closable="true">
	<button id="openPopup" label="Open Popup 2" />
</window>
</zk>


ScopeTestPopupController
@Controller("scopeTestPopupCtrl")
@Scope("prototype")
public class ScopeTestPopupController extends GenericSpringComposer {
	@Autowired
	private ScopeTestModel scopeTestModel;
	
	@Autowired
	private Window scopeTestPopupWin;
	@Autowired
	private Button openPopup;
	
	private boolean sameObj = false;
	
	@EventHandler("scopeTestPopupWin.onCreate")
	public void doCreateWindow(Event event){
		System.out.println("scopeTestPopupCtrl");
		if(sameObj){
			System.out.println("old bean");
		}else{
			System.out.println("new bean");
			sameObj = true;
		}
		
		if(scopeTestModel.getModelName() != null){
			System.out.println("old model bean");
		}else{
			System.out.println("new model bean");
			scopeTestModel.setModelName("Main Window");
		}
		
		System.out.println("openPopup-->[" + openPopup.hashCode() + "] label=" + openPopup.getLabel());
	}
	
	@EventHandler("openPopup.onClick")
	public void doClickOpenPopup(Event event) throws SuspendNotAllowedException, InterruptedException{
		System.out.println("scopeTestPopupCtrl openPopup");
	}
}


The scenario of my testing
1 open the scope_test.zul
2 click "Open Popup" button to open scope_test_popup.zul

I test with any spring scope--> protetype, desktop, page, idspace, component, execution. the result of log is

/************ prototype **************/
scopeTestCtrl
new bean
new model bean
openPopup-->Open Popup
scopeTestCtrl openPopup
scopeTestPopupCtrl
new bean
new model bean
openPopup-->Open Popup

/************ desktop **************/
scopeTestCtrl
new bean
new model bean
openPopup-->[-1638341654] label=Open Popup
scopeTestCtrl openPopup
scopeTestPopupCtrl
new bean
old model bean
openPopup-->[-1638341654] label=Open Popup

/************ page **************/
scopeTestCtrl
new bean
new model bean
openPopup-->[722614749] label=Open Popup
scopeTestCtrl openPopup
scopeTestPopupCtrl
new bean
old model bean
openPopup-->[722614749] label=Open Popup

/************ idspace **************/
scopeTestCtrl
new bean
new model bean
openPopup-->[2041104974] label=Open Popup
scopeTestCtrl openPopup
scopeTestPopupCtrl
new bean
old model bean
openPopup-->[2041104974] label=Open Popup

/************ component **************/
java.lang.IllegalStateException: No Scope registered for scope 'component'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)

/************ execution **************/
scopeTestCtrl
new bean
new model bean
openPopup-->[1887762222] label=Open Popup
scopeTestCtrl openPopup
scopeTestPopupCtrl
new bean
new model bean
openPopup-->[1887762222] label=Open Popup

why the openPopup bean in scopeTestPopupCtrl is same as openPopup bean in scopeTestCtrl ?

I try to test without ZK Spring

scopeTestCtrl
new bean
openPopup-->[18509834] label=Open Popup
scopeTestCtrl openPopup
scopeTestPopupCtrl
new bean
openPopup-->[28900573] label=Open Popup 2
scopeTestPopupCtrl openPopup

This is result that I want.

How to set the scope of controller to do like the controller without spring ?

delete flag offensive retag edit

1 Reply

Sort by ยป oldest newest

answered 2012-01-11 04:35:42 +0800

dgofast gravatar image dgofast
9 1

Hi did you find a work around ?

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-08-26 00:50:03 +0800

Seen: 516 times

Last updated: Jan 11 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More