如何为 web-app 添加功能,以便多个用户同时使用它
How to add capability to the web-app so that it can be used by multiple users at the same time
我在“zk-framework”中创建了 web-app,如果只有一个用户正在访问它,它工作正常,但是当其他用户开始访问它时,屏幕显示组件无法访问。
我不能在这里粘贴整个代码,所以我在这里给出主页的片段。
package in.net.usit.dbu.web.main;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Button;
import org.zkoss.zul.Div;
import org.zkoss.zul.Image;
import org.zkoss.zul.Tabpanels;
import org.zkoss.zul.Tabs;
import org.zkoss.zul.Window;
import in.net.usit.dbu.common.to.dbadapter.DBAdapter;
import in.net.usit.dbu.common.util.DbuConstatns;
import in.net.usit.dbu.common.util.ViewConstatns;
import in.net.usit.dbu.web.dbadapter.DbAdapterModel;
import in.net.usit.dbu.web.framework.WindowManager;
public class MainViewModel {
//Variables
private List<DBAdapter> adapterList;
//
private String selectedAdapterName;
//
private DbAdapterModel adapterModel;
public WindowManager windowmanager;
public DBAdapter selectedAdapterObject;
ServletContext context;
//Wired Variables
@Wire("#mainWindow")
Window mainWindow;
@Wire("#mainDiv")
Div mainDiv;
@Wire("#mainTabs")
Tabs mainTabs;
@Wire("#tabPanels")
Tabpanels tabPanels;
/*@Wire("#btnName")
Button btnName;
@Wire("#btnSrc")
Button btnSrc;
@Wire("#btnDest")
Button btnDest;
@Wire("#btnTrans")
Button btnTrans;
@Wire("#btnExec")
Button btnExec;*/
@Wire("#imgNew")
Image imgNew;
@Wire("#imgSrc")
Image imgSrc;
@Wire("#imgDest")
Image imgDest;
@Wire("#imgTrans")
Image imgTrans;
@Wire("#imgExec")
Image imgExec;
@SuppressWarnings("deprecation")
@AfterCompose
public void init(@ContextParam(ContextType.VIEW) Component view){
Selectors.wireComponents(view, this, false);
context = (ServletContext) Sessions.getCurrent().getWebApp().getNativeContext();
initializeData();
}
public void initializeData(){
windowmanager = new WindowManager(mainWindow, mainTabs, tabPanels);
mainWindow.getDesktop().setAttribute(ViewConstatns.WiNDOW_MANAGER, windowmanager);
adapterModel = new DbAdapterModel();
adapterList = new ArrayList<DBAdapter>();
adapterList = adapterModel.getDBAList();
selectedAdapterObject = new DBAdapter();
WindowManager.tabs.clear();
/*context.setAttribute(ViewConstatns.BTN_NEW, btnName);
context.setAttribute(ViewConstatns.BTN_TRANS, btnTrans);
context.setAttribute(ViewConstatns.BTN_DEST, btnDest);
context.setAttribute(ViewConstatns.BTN_SRC, btnSrc);
context.setAttribute(ViewConstatns.BTN_EXEC, btnExec);*/
context.setAttribute(ViewConstatns.IMG_NEW, imgNew);
context.setAttribute(ViewConstatns.IMG_TRANS, imgTrans);
context.setAttribute(ViewConstatns.IMG_DEST, imgDest);
context.setAttribute(ViewConstatns.IMG_SRC, imgSrc);
context.setAttribute(ViewConstatns.IMG_EXEC, imgExec);
((Image) context.getAttribute(ViewConstatns.IMG_NEW)).setSrc("/img/dba1sel.png");
}
@SuppressWarnings("static-access")
@Command
public void onSelectAdapter(){
mainDiv.setVisible(false);
//System.out.println("DBA LIST IS : "+adapterList.toString());
for(DBAdapter adapter : adapterList){
//System.out.println(adapter.getDbaName());
if(adapter.toString().equals(selectedAdapterName)){
//System.out.println("inside if");
selectedAdapterObject = adapter;
}
}
context.setAttribute(DbuConstatns.DBA_OBJECT, selectedAdapterObject);
//System.out.println("context name is " +selectedAdapterObject.getDbaName() );
//btnSrc.setDisabled(false);
windowmanager.createTabComponent(ViewConstatns.SRC_PATH,ViewConstatns.SRC_TAB_NAME);
((Image) context.getAttribute(ViewConstatns.IMG_NEW)).setSrc("/img/dba1.png");
((Image) context.getAttribute(ViewConstatns.IMG_SRC)).setSrc("/img/dba2sel.png");
((Image) context.getAttribute(ViewConstatns.IMG_DEST)).setSrc("/img/dba3.png");
((Image) context.getAttribute(ViewConstatns.IMG_TRANS)).setSrc("/img/dba4.png");
((Image) context.getAttribute(ViewConstatns.IMG_EXEC)).setSrc("/img/dba5.png");
}
@SuppressWarnings("static-access")
@Command
public void onClickNewConnection(){
mainDiv.setVisible(false);
//btnName.setDisabled(false);
windowmanager.createTabComponent(ViewConstatns.DB_NEW_PATH,ViewConstatns.DB_TAB_NAME);
}
//setter and getter methods
public List<DBAdapter> getAdapterList() {
return adapterList;
}
public void setAdapterList(List<DBAdapter> adapterList) {
this.adapterList = adapterList;
}
public String getSelectedAdapterName() {
return selectedAdapterName;
}
public void setSelectedAdapterName(String selectedAdapter) {
this.selectedAdapterName = selectedAdapter;
}
}
和main.zul如下
<?page title="" contentType="text/html;charset=UTF-8"?>
<zk>
<window contentStyle="overflow:auto" id="mainWindow"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('in.net.usit.dbu.web.main.MainViewModel')">
<div class="wrapper">
<div class="AppHeader">
<div class="AppHeaderMenu">
<div class="master_top_inner divider"></div>
</div>
</div>
<space></space>
<hlayout>
<!-- <hbox>
<image id="imgNew" src="/img/dba1.png" width="80px"
height="70px" tooltiptext="DB Adapter Details" />
<image id="imgSrc" src="/img/dba2.png" width="80px"
height="70px" tooltiptext="Source DB Connection" />
<image id="imgDest" src="/img/dba3.png" width="80px"
height="70px" tooltiptext="Destination DB Connection" />
<image id="imgTrans" src="/img/dba4.png"
width="80px" height="70px" tooltiptext="Transformation Rules" />
<image id="imgExec" src="/img/dba5.png" width="80px"
height="70px" tooltiptext="Copying data" />
</hbox> -->
<space></space>
<vbox>
<hbox>
<separator height="25px"></separator>
<image id="imgNew" src="/img/dba1.png" width="80px"
height="70px" tooltiptext="DB Adapter Details" />
<image id="imgSrc" src="/img/dba2.png" width="80px"
height="70px" tooltiptext="Source DB Connection" />
<image id="imgDest" src="/img/dba3.png" width="80px"
height="70px" tooltiptext="Destination DB Connection" />
<image id="imgTrans" src="/img/dba4.png"
width="80px" height="70px" tooltiptext="Transformation Rules" />
<image id="imgExec" src="/img/dba5.png" width="80px"
height="70px" tooltiptext="Copying data" />
<!-- <button id="btnName" label=" New Database Adapter" width="310px" height="80px" disabled="true"></button>
<button id="btnSrc" label="Source Connection Details" width="310px" height="80px" disabled="true"></button>
<button id="btnDest" label="Destination Connection Details" width="310px" height="80px" disabled="true"></button>
<button id="btnTrans" label="Transformation Rules" width="310px" height="80px" disabled="true"></button>
<button id="btnExec" label="Generate and Execute Query" width="310px" height="80px" disabled="true"></button>
-->
</hbox>
<space></space>
<div align="center" id="mainDiv" width="100%">
<hbox>
<label
style="font-size:18px; margin-right:10px">
Database Adapter :
</label>
<combobox width="300px"
model="@bind(vm.adapterList)"
selectedItem="@bind(vm.selectedAdapterName)"
onSelect="@command('onSelectAdapter')">
<template name="model">
<comboitem
label="@bind(each.dbaName)" value="@bind(each.dbaName)">
</comboitem>
</template>
</combobox>
<space width="40px" />
<button sclass="" label="New Connection"
onClick="@command('onClickNewConnection')">
</button>
</hbox>
</div>
<div>
<tabbox>
<tabs id="mainTabs"></tabs>
<tabpanels id="tabPanels"></tabpanels>
</tabbox>
</div>
</vbox>
<space></space>
</hlayout>
</div>
<div class="AppFooter">
<div class="AppFooterInner">
</div>
</div>
</window>
这里我使用的是 zk session class。
谢谢
此错误是由于将 1 个组件用于不同的页面。
最好的例子是向视图添加静态组件。
第一个人可以看到它很好,但是第二个人和其他人都会有一个错误,因为该组件已经有了它所属的桌面。
或者打个比喻:
如果你租车,你可以把车1租给人1。
如果人 2 来了,你不能把车 1 租给那个人,因为它已经用过了,你需要给另一辆车,即使他想要和人 1 一样的车。
我想你需要看看这个方法:
windowmanager.createTabComponent(ViewConstatns.SRC_PATH,ViewConstatns.SRC_TAB_NAME);
如果 2 个输入相同,是否可能 returns 是同一个对象?
如果是 => 那是你的问题。
我在“zk-framework”中创建了 web-app,如果只有一个用户正在访问它,它工作正常,但是当其他用户开始访问它时,屏幕显示组件无法访问。
我不能在这里粘贴整个代码,所以我在这里给出主页的片段。
package in.net.usit.dbu.web.main;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Button;
import org.zkoss.zul.Div;
import org.zkoss.zul.Image;
import org.zkoss.zul.Tabpanels;
import org.zkoss.zul.Tabs;
import org.zkoss.zul.Window;
import in.net.usit.dbu.common.to.dbadapter.DBAdapter;
import in.net.usit.dbu.common.util.DbuConstatns;
import in.net.usit.dbu.common.util.ViewConstatns;
import in.net.usit.dbu.web.dbadapter.DbAdapterModel;
import in.net.usit.dbu.web.framework.WindowManager;
public class MainViewModel {
//Variables
private List<DBAdapter> adapterList;
//
private String selectedAdapterName;
//
private DbAdapterModel adapterModel;
public WindowManager windowmanager;
public DBAdapter selectedAdapterObject;
ServletContext context;
//Wired Variables
@Wire("#mainWindow")
Window mainWindow;
@Wire("#mainDiv")
Div mainDiv;
@Wire("#mainTabs")
Tabs mainTabs;
@Wire("#tabPanels")
Tabpanels tabPanels;
/*@Wire("#btnName")
Button btnName;
@Wire("#btnSrc")
Button btnSrc;
@Wire("#btnDest")
Button btnDest;
@Wire("#btnTrans")
Button btnTrans;
@Wire("#btnExec")
Button btnExec;*/
@Wire("#imgNew")
Image imgNew;
@Wire("#imgSrc")
Image imgSrc;
@Wire("#imgDest")
Image imgDest;
@Wire("#imgTrans")
Image imgTrans;
@Wire("#imgExec")
Image imgExec;
@SuppressWarnings("deprecation")
@AfterCompose
public void init(@ContextParam(ContextType.VIEW) Component view){
Selectors.wireComponents(view, this, false);
context = (ServletContext) Sessions.getCurrent().getWebApp().getNativeContext();
initializeData();
}
public void initializeData(){
windowmanager = new WindowManager(mainWindow, mainTabs, tabPanels);
mainWindow.getDesktop().setAttribute(ViewConstatns.WiNDOW_MANAGER, windowmanager);
adapterModel = new DbAdapterModel();
adapterList = new ArrayList<DBAdapter>();
adapterList = adapterModel.getDBAList();
selectedAdapterObject = new DBAdapter();
WindowManager.tabs.clear();
/*context.setAttribute(ViewConstatns.BTN_NEW, btnName);
context.setAttribute(ViewConstatns.BTN_TRANS, btnTrans);
context.setAttribute(ViewConstatns.BTN_DEST, btnDest);
context.setAttribute(ViewConstatns.BTN_SRC, btnSrc);
context.setAttribute(ViewConstatns.BTN_EXEC, btnExec);*/
context.setAttribute(ViewConstatns.IMG_NEW, imgNew);
context.setAttribute(ViewConstatns.IMG_TRANS, imgTrans);
context.setAttribute(ViewConstatns.IMG_DEST, imgDest);
context.setAttribute(ViewConstatns.IMG_SRC, imgSrc);
context.setAttribute(ViewConstatns.IMG_EXEC, imgExec);
((Image) context.getAttribute(ViewConstatns.IMG_NEW)).setSrc("/img/dba1sel.png");
}
@SuppressWarnings("static-access")
@Command
public void onSelectAdapter(){
mainDiv.setVisible(false);
//System.out.println("DBA LIST IS : "+adapterList.toString());
for(DBAdapter adapter : adapterList){
//System.out.println(adapter.getDbaName());
if(adapter.toString().equals(selectedAdapterName)){
//System.out.println("inside if");
selectedAdapterObject = adapter;
}
}
context.setAttribute(DbuConstatns.DBA_OBJECT, selectedAdapterObject);
//System.out.println("context name is " +selectedAdapterObject.getDbaName() );
//btnSrc.setDisabled(false);
windowmanager.createTabComponent(ViewConstatns.SRC_PATH,ViewConstatns.SRC_TAB_NAME);
((Image) context.getAttribute(ViewConstatns.IMG_NEW)).setSrc("/img/dba1.png");
((Image) context.getAttribute(ViewConstatns.IMG_SRC)).setSrc("/img/dba2sel.png");
((Image) context.getAttribute(ViewConstatns.IMG_DEST)).setSrc("/img/dba3.png");
((Image) context.getAttribute(ViewConstatns.IMG_TRANS)).setSrc("/img/dba4.png");
((Image) context.getAttribute(ViewConstatns.IMG_EXEC)).setSrc("/img/dba5.png");
}
@SuppressWarnings("static-access")
@Command
public void onClickNewConnection(){
mainDiv.setVisible(false);
//btnName.setDisabled(false);
windowmanager.createTabComponent(ViewConstatns.DB_NEW_PATH,ViewConstatns.DB_TAB_NAME);
}
//setter and getter methods
public List<DBAdapter> getAdapterList() {
return adapterList;
}
public void setAdapterList(List<DBAdapter> adapterList) {
this.adapterList = adapterList;
}
public String getSelectedAdapterName() {
return selectedAdapterName;
}
public void setSelectedAdapterName(String selectedAdapter) {
this.selectedAdapterName = selectedAdapter;
}
}
和main.zul如下
<?page title="" contentType="text/html;charset=UTF-8"?>
<zk>
<window contentStyle="overflow:auto" id="mainWindow"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('in.net.usit.dbu.web.main.MainViewModel')">
<div class="wrapper">
<div class="AppHeader">
<div class="AppHeaderMenu">
<div class="master_top_inner divider"></div>
</div>
</div>
<space></space>
<hlayout>
<!-- <hbox>
<image id="imgNew" src="/img/dba1.png" width="80px"
height="70px" tooltiptext="DB Adapter Details" />
<image id="imgSrc" src="/img/dba2.png" width="80px"
height="70px" tooltiptext="Source DB Connection" />
<image id="imgDest" src="/img/dba3.png" width="80px"
height="70px" tooltiptext="Destination DB Connection" />
<image id="imgTrans" src="/img/dba4.png"
width="80px" height="70px" tooltiptext="Transformation Rules" />
<image id="imgExec" src="/img/dba5.png" width="80px"
height="70px" tooltiptext="Copying data" />
</hbox> -->
<space></space>
<vbox>
<hbox>
<separator height="25px"></separator>
<image id="imgNew" src="/img/dba1.png" width="80px"
height="70px" tooltiptext="DB Adapter Details" />
<image id="imgSrc" src="/img/dba2.png" width="80px"
height="70px" tooltiptext="Source DB Connection" />
<image id="imgDest" src="/img/dba3.png" width="80px"
height="70px" tooltiptext="Destination DB Connection" />
<image id="imgTrans" src="/img/dba4.png"
width="80px" height="70px" tooltiptext="Transformation Rules" />
<image id="imgExec" src="/img/dba5.png" width="80px"
height="70px" tooltiptext="Copying data" />
<!-- <button id="btnName" label=" New Database Adapter" width="310px" height="80px" disabled="true"></button>
<button id="btnSrc" label="Source Connection Details" width="310px" height="80px" disabled="true"></button>
<button id="btnDest" label="Destination Connection Details" width="310px" height="80px" disabled="true"></button>
<button id="btnTrans" label="Transformation Rules" width="310px" height="80px" disabled="true"></button>
<button id="btnExec" label="Generate and Execute Query" width="310px" height="80px" disabled="true"></button>
-->
</hbox>
<space></space>
<div align="center" id="mainDiv" width="100%">
<hbox>
<label
style="font-size:18px; margin-right:10px">
Database Adapter :
</label>
<combobox width="300px"
model="@bind(vm.adapterList)"
selectedItem="@bind(vm.selectedAdapterName)"
onSelect="@command('onSelectAdapter')">
<template name="model">
<comboitem
label="@bind(each.dbaName)" value="@bind(each.dbaName)">
</comboitem>
</template>
</combobox>
<space width="40px" />
<button sclass="" label="New Connection"
onClick="@command('onClickNewConnection')">
</button>
</hbox>
</div>
<div>
<tabbox>
<tabs id="mainTabs"></tabs>
<tabpanels id="tabPanels"></tabpanels>
</tabbox>
</div>
</vbox>
<space></space>
</hlayout>
</div>
<div class="AppFooter">
<div class="AppFooterInner">
</div>
</div>
</window>
这里我使用的是 zk session class。
谢谢
此错误是由于将 1 个组件用于不同的页面。
最好的例子是向视图添加静态组件。
第一个人可以看到它很好,但是第二个人和其他人都会有一个错误,因为该组件已经有了它所属的桌面。
或者打个比喻:
如果你租车,你可以把车1租给人1。
如果人 2 来了,你不能把车 1 租给那个人,因为它已经用过了,你需要给另一辆车,即使他想要和人 1 一样的车。
我想你需要看看这个方法:
windowmanager.createTabComponent(ViewConstatns.SRC_PATH,ViewConstatns.SRC_TAB_NAME);
如果 2 个输入相同,是否可能 returns 是同一个对象?
如果是 => 那是你的问题。