自定义WebContext的attributes——server 9.2 for Java
Web ADF的开发中com.esri.adf.web.data.WebContext是一个核心的类,这个类绑定了许多其他的组件,这些组件分为两类:attributes和resources。WebContext为这些组件提供生命周期管理的服务,这些服务当然不是免费使用的啦,组件必须实现一些特定的接口并注册到WebContext,相关的接口有:
com.esri.adf.web.data.WebContextInitialize
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
+--public void init(WebContext context)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
+--public void destroy()
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
com.esri.adf.web.data.WebLifecycle
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
+--public void activate()
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
+--public void passivate()
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
com.esri.adf.web.data.WebContextObserver
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
+--public void update(WebContext context, java.lang.Object arg)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这里只讨论attributes这类组件,例如WebMap,WebTOC,WebOverview等。为什么我们放大地图的时候,Overview的矩形框也跟着变化呢?就是因为WebMap和WebOverview都在WebContext的领地上,而WebOverview实现了WebContextObserver接口;呵呵,这就相当于它给WebContext交了服务费,每次WebContext状态发生变化的时候,都会给WebOverview传递消息,好让它作出同步的调整。
对于WebContext中注册的attribute,当session的第一个请求开始的时候,执行init()操作,然后是passivate();对于这个session中接下来的请求,总是从activate()开始,passivate()结束;当session中止的时候,执行destroy()操作。这就是attribute的生命周期,当然,上述三个接口并不是强制要求全部都要实现。
好了,现在我们来看看如何自定义一个attribute,文档中有一个相关的例子,但不够详细。这里还是用例子的思路:底图用的是亚洲的各个国家,目的就是根据当前地图显示的国家在地图的左上角显示与该国相关的logo,当地图停留在中国的时候,左上角显示一个吐火龙,停留在日本的时候,显示一个持斧的战士
,等等。
首先,定义一个类实现上述的三个接口,在init方法中()将这个类注册为WebContext的监听器,用来监听WebMap的Extent的变化:
public class PicSelector
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
implements WebContextObserver,WebLifecycle,WebContextInitialize{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void init(WebContext ctx){
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ctx.addObserver(this);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void destroy(){
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.out.println("PicSelector destroyed.");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
对于每个请求,都会执行activate()和passivate()方法,因此,这两个方法适合用来执行一些资源获取和释放的操作,比如这个例子中用IO接口来读取图片文件:
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public void activate(){
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String pic_1 = "E:/images/吐火龙.png";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String pic_2 = "E://images/武士.png";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
input_1 = new BufferedInputStream(new FileInputStream(pic_1));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
input_2 = new BufferedInputStream(new FileInputStream(pic_2));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}catch(IOException e){
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public void passivate(){
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
input_1.close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
input_2.close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}catch(IOException e){
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
接下来,是根据地图显示内容动态地调取logo图片显示在地图的左上角:
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public void update(WebContext ctx, Object arg1){
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
WebExtent currentExtent = ctx.getWebMap().getCurrentExtent();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ctx.getWebGraphics().clearGraphics();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.updatePic(currentExtent,ctx);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private void updatePic(WebExtent ext, WebContext ctx){
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(
){ //从extent判断是否主要区域为中国
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
WebPictureMarkerSymbol pic = new WebPictureMarkerSymbol();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte[] bPic_1 = new byte[input_1.available()];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
input_1.read(bPic_1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
pic.setPicture(bPic_1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}catch(IOException e){
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.out.println(e.getMessage());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
WebGraphics graphics = ctx.getWebGraphics();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
GraphicElement element = new GraphicElement();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
element.setSymbol(pic);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
element.setGeometry(new WebPoint(extent.getMinX(),extent.getMaxY()));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
graphics.addGraphics(element);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
else if(
)//同理,下略
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
最后,是在faces-config.xml中注册这个自定义attribute:
![](https://www.cnblogs.com/Images/dot.gif)
<managed-property>
<property-name>attributes</property-name>
<map-entries>
![](https://www.cnblogs.com/Images/dot.gif)
<map-entry>
<key>myAttr</key>
<value>#{myAttr}</value>
</map-entry>
![](https://www.cnblogs.com/Images/dot.gif)
</map-entries>
</managed-property>
![](https://www.cnblogs.com/Images/dot.gif)
<managed-bean>
<managed-bean-name>myAttr</managed-bean-name>
<managed-bean-class>com.wj.test.data.PicSelector</managed-bean-class>
<managed-bean-scope>none</managed-bean-scope>
</managed-bean>
bContext都会调用update()方法,如果满足要求,“吐火龙”就出来了,看效果图-->
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
对于WebContext中注册的attribute,当session的第一个请求开始的时候,执行init()操作,然后是passivate();对于这个session中接下来的请求,总是从activate()开始,passivate()结束;当session中止的时候,执行destroy()操作。这就是attribute的生命周期,当然,上述三个接口并不是强制要求全部都要实现。
好了,现在我们来看看如何自定义一个attribute,文档中有一个相关的例子,但不够详细。这里还是用例子的思路:底图用的是亚洲的各个国家,目的就是根据当前地图显示的国家在地图的左上角显示与该国相关的logo,当地图停留在中国的时候,左上角显示一个吐火龙,停留在日本的时候,显示一个持斧的战士
![](http://bbs.esrichina-bj.cn/esri/images/smilies/smile.gif)
首先,定义一个类实现上述的三个接口,在init方法中()将这个类注册为WebContext的监听器,用来监听WebMap的Extent的变化:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
对于每个请求,都会执行activate()和passivate()方法,因此,这两个方法适合用来执行一些资源获取和释放的操作,比如这个例子中用IO接口来读取图片文件:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
接下来,是根据地图显示内容动态地调取logo图片显示在地图的左上角:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
最后,是在faces-config.xml中注册这个自定义attribute:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![点击在新窗口查看全图
CTRL+鼠标滚轮放大或缩小](http://bbs.esrichina-bj.cn/esri/attachments/forumid_25/zcK78MH6_P2kIgZATcxy6.jpg)
加油,哥们,现在开始!