设为首页 收藏本站
查看: 837|回复: 0

[经验分享] Apache Pivot初体验

[复制链接]

尚未签到

发表于 2017-1-2 07:54:59 | 显示全部楼层 |阅读模式
  Apache已经将Pivot升级为顶级项目,虽然从前不知道这个项目,但是看来还是有发展前途的。我看好它,因为我对FLEX语法实在不感冒,还是Java写着舒服。似乎也很少有人用AWT/Swing做商业产品,但Applet的应用还是常见的。就研究研究Pivot吧。
    既然Apache将其定位为RIA in Java,做J2EE的还是关注下吧。这是纯Java血统的东西(相比于FLEX),下了开发包,看例子,语法类似Swing,还是比较容易接受的。但是官方例子中没有完整的目录结构,构建开发环境还是留下了一些障碍。可能是之前没有接触过。我自己摸索了一个适应web开发的目录结构,可能不是最好的,但是是可行的。如下:
DSC0000.jpg
    这里用了ANT进行jar打包,之前用Eclipse的导出打包不好用,就自己写了个ANT构建脚本运行,还真好用了。
    目录结构还是传统的web项目,不同的是webapp下也放置了lib,当然就是运行项目所需的lib库,如果放到WEB-INF/lib下,那页面是不能直接访问到的,而pivot程序是直接在页面显示调用类库的,就需要放到可见的位置。不过我是在WEB-INF/lib下也放置了一份,那里的在编译路径里的,这样不冲突,也为ANT构建省去来回设置类路径的麻烦。jar包也不大,基本的才1.29M,没有办法,谁让人家是RIA呢。
    先从Java Application开始吧,我是参照官方示例学习的。但是如果你刚下载开发包,估计得看上一阵子呢。其实很简单,就是一个类,一个XML配置文件(Pivot中是以wtkx作为扩展名的)。下面用代码来说明,更为直观。

Java代码 DSC0001.gif   DSC0002.png DSC0003.gif




  • package pivot.tutorials.hellowtkx;   
  •   

  • import org.apache.pivot.collections.Map;   

  • import org.apache.pivot.wtk.Application;   

  • import org.apache.pivot.wtk.DesktopApplicationContext;   

  • import org.apache.pivot.wtk.Display;   

  • import org.apache.pivot.wtk.Window;   

  • import org.apache.pivot.wtkx.WTKXSerializer;   
  •   

  • public class HelloWTKX implements Application {   

  •     private Window window = null;   
  •   

  •     public void startup(Display display, Map<String, String> map)   

  •             throws Exception {   

  •         WTKXSerializer wtkxSerializer = new WTKXSerializer();   

  •         window = (Window) wtkxSerializer.readObject(this, "helloWTKX.wtkx.xml");   

  •         window.setTitle("Hello JavaEye");   
  •         window.open(display);   
  •     }   
  •   

  •     public boolean shutdown(boolean optional) throws Exception {   

  •         if (window != null) {   
  •             window.close();   
  •         }   

  •         return false;   
  •     }   
  •   

  •     public void resume() throws Exception {   
  •     }   
  •   

  •     public void suspend() throws Exception {   
  •     }   
  •   

  •     public static void main(String[] args) {   

  •         DesktopApplicationContext.main(HelloWTKX.class, args);   
  •     }   
  •   
  • }  



package pivot.tutorials.hellowtkx;
import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.Window;
import org.apache.pivot.wtkx.WTKXSerializer;
public class HelloWTKX implements Application {
private Window window = null;
public void startup(Display display, Map<String, String> map)
throws Exception {
WTKXSerializer wtkxSerializer = new WTKXSerializer();
window = (Window) wtkxSerializer.readObject(this, "helloWTKX.wtkx.xml");
window.setTitle("Hello JavaEye");
window.open(display);
}
public boolean shutdown(boolean optional) throws Exception {
if (window != null) {
window.close();
}
return false;
}
public void resume() throws Exception {
}
public void suspend() throws Exception {
}
public static void main(String[] args) {
DesktopApplicationContext.main(HelloWTKX.class, args);
}
}
  
官方示例中配置文件都以XXX.wtkx形式存在,其实就是XML文档,我就直接以.xml来编辑,这样在Eclipse中可以高亮显示,否则就是普通文本编辑了。配置文件的编写参考官方示例就行了。我还加上了的XML声明。

Xml代码  




  • <?xml version="1.0" encoding="UTF-8" ?>  

  • <Window title="Hello WTKX" maximized="true"  

  • xmlns:wtkx="ttp://pivot.apache.org/wtkx"    

  • xmlns="org.apache.pivot.wtk">  

  •     <content>  

  •         <Label text="Hello JavaEye! - The Pivot WTKX Application"  

  •             styles="{font:'Arial bold 24',color:'#ff0000',   

  • horizontalAlignment:'center',verticalAlignment:'center'}" />  

  •     </content>  

  • </Window>  



<?xml version="1.0" encoding="UTF-8" ?>
<Window title="Hello WTKX" maximized="true"
xmlns:wtkx="ttp://pivot.apache.org/wtkx"
xmlns="org.apache.pivot.wtk">
<content>
<Label text="Hello JavaEye! - The Pivot WTKX Application"
styles="{font:'Arial bold 24',color:'#ff0000',
horizontalAlignment:'center',verticalAlignment:'center'}" />
</content>
</Window>
  
    基本的Pivot应用程序都是实现了Application接口,自然也要实现其中的抽象方法。从方法名启动,关闭,唤醒和挂起不难看出这是进程的状态转化方法。负责继承状态改变时的处理。主函数就是Pivot应用程序的执行方式,这都是固定的写法了,传入本类的class和主函数的参数。
    类中定义一个私有属性window,就是Pivot应用程序的窗口对象,在startup方法中进行进程加载启动时的事情。WTKXSerializer类在官方文档中解释为“从XML中加载对象层次结构”,可以看出类似于MXML,控件的定义都是写在配置文件中的。只不过这个XML编码时IDE还不能给出提示。我想是Schema还没有最后固定,编码时只能按照已有定义来编写。程序就可以解析XML了。wtkxSerializer对象读取XML配置文件,并从中获取window实例(window是根元素)。中间可以是业务流程的处理。这里是对window的title进行了设置,编码风格很像Swing。最后一步是window的open()方法,传入display参数。在shutdown()方法中别忘记关闭window就可以了。
    此时程序就可以以Application方式运行了。得到效果:
DSC0004.jpg
    第一部分介绍到此结束,第二部分中将介绍Web应用中的Pivot部署和用ANT构建自定义jar包。 
   我们探究了如何构建Pivot开发框架,编写Pivot桌面应用程序。这一篇我们来说说如何在web环境中部署Pivot程序。webapp发布目录 如下所示:
DSC0005.jpg
    css中的样式表和js下的JavaScript脚本都是根据官方示例中拷贝下来的,其中最重要的是deployJava.js,它是展示Java applet的工具包,Pivot在页面中就是以Applet的形式展现的,js文件在http://java.com/js/deployJava.js 下载。Lib下是Pivot应用所依赖的类库,可以看出pivot.jar是我自定义的jar,就是编好代码打的包,而WEB-INF/lib下的库和Pivot没有关系,那里的库正常是访问不到的。
    下面展示HTML页面

Html代码 DSC0006.gif   DSC0007.png DSC0008.gif




  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

  • <html xmlns="http://www.w3.org/1999/xhtml">  

  • <head>  

  • <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />  

  • <title>Push Buttons</title>  

  • <script src="/pivot/js/deployJava.js" type="text/javascript"></script>  

  • </head>  

  • <body>  

  • <script xmlns="" type="text/javascript">  

  •             var attributes = {   
  • code:'org.apache.pivot.wtk.BrowserApplicationContext$HostApplet',   
  •                 width:'480',   
  •                 height:'360'   
  •             };   

  •             var libraries = [];   
  •             libraries.push("/pivot/lib/pivot-core-1.4.jar");   
  •             libraries.push("/pivot/lib/pivot-wtk-1.4.jar");   
  •             libraries.push("/pivot/lib/pivot-wtk-terra-1.4.jar");   
  •             libraries.push("/pivot/lib/pivot.jar");   

  •             attributes.archive = libraries.join(",");   

  •             var parameters = {   
  •                 codebase_lookup:false,   

  •                 java_arguments:'-Dsun.awt.noerasebackground=true -Dsun.awt.erasebackgroundonresize=true',   
  •      application_class_name:'pivot.tutorials.buttons.PushButtons'   
  •             };   
  •             deployJava.runApplet(attributes, parameters, "1.6");   

  • </script>  

  • </body>  

  • </html>  



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title>Push Buttons</title>
<script src="/pivot/js/deployJava.js" type="text/javascript"></script>
</head>
<body>
<script xmlns="" type="text/javascript">
var attributes = {
code:'org.apache.pivot.wtk.BrowserApplicationContext$HostApplet',
width:'480',
height:'360'
};
var libraries = [];
libraries.push("/pivot/lib/pivot-core-1.4.jar");
libraries.push("/pivot/lib/pivot-wtk-1.4.jar");
libraries.push("/pivot/lib/pivot-wtk-terra-1.4.jar");
libraries.push("/pivot/lib/pivot.jar");
attributes.archive = libraries.join(",");
var parameters = {
codebase_lookup:false,
java_arguments:'-Dsun.awt.noerasebackground=true -Dsun.awt.erasebackgroundonresize=true',
application_class_name:'pivot.tutorials.buttons.PushButtons'
};
deployJava.runApplet(attributes, parameters, "1.6");
</script>
</body>
</html>
  
    关键是JS段代码,其实都是固定套路,只需修改自定义的jar即可,其他段根据字母意思即可理解,关键是lib库的位置一定要对。下面说说ANT打包。

Xml代码  




  • <?xml version="1.0" encoding="UTF-8" ?>  

  • <project name="pivot" default="all">  

  •     <description>The Pivot Application</description>  

  •     <!-- 定义文件夹 -->  

  •     <property name="srcDir" location="." />  

  •     <property name="classDir" location="../webapp/WEB-INF/classes" />  

  •     <property name="libDir" location="../webapp/WEB-INF/lib" />  

  •          <property name="webDir" location="../webapp" />  

  •     <!--用于输出打包的文件夹-->  

  •     <property name="tempDir" location="${java.io.tmpdir}/${ant.project.name}" />  

  •     <property name="targetDir" location="../target" />  

  •     <!-- 定义classpath -->  

  •     <path id="master-classpath">  

  •         <fileset file="${libDir}/*.jar" />  

  •         <pathelement path="${classDir}" />  

  •     </path>  

  •     <!-- 执行清理 -->  

  •     <target name="clean">  

  •         <delete dir="${classDir}" />  

  •         <delete dir="${tempDir}" />  

  •         <delete file="${targetDir}/${ant.project.name}.jar" />  

  •         <delete file="${targetDir}/${ant.project.name}.war" />  

  •         <delete dir="${targetDir}" />  

  •     </target>  

  •     <!-- 初始化任务 -->  

  •     <target name="init" depends="clean">  

  •         <mkdir dir="${targetDir}" />  

  •         <mkdir dir="${tempDir}" />  

  •         <mkdir dir="${classDir}" />  

  •     </target>  

  •     <!-- 编译 -->  

  •     <target name="compile" depends="init" description="compile the source files">  

  •         <javac srcdir="${srcDir}" destdir="${classDir}" debug="true" encoding="UTF-8">  

  •             <classpath refid="master-classpath" />  

  •         </javac>  

  •         <copy todir="${classDir}" overwrite="true">  

  •             <fileset dir="${srcDir}">  

  •                 <include name="**/*.xml" />  

  •                 <include name="**/*.properties" />  

  •             </fileset>  

  •         </copy>  

  •     </target>  

  •     <!--打jar包-->  

  •     <target name="jar" depends="compile">  

  •         <jar jarfile="${targetDir}/${ant.project.name}.jar">  

  •             <fileset dir="${classDir}">  

  •                 <include name="**/*" />  

  •             </fileset>  

  •         </jar>  

  •     </target>  

  •     <!—准备war包 -->  
  •        

  •     <target name="preWar" depends="jar">  

  •         <copy todir="${tempDir}/WEB-INF/lib" overwrite="true">  

  •             <fileset dir="${libDir}">  

  •                 <include name="*.jar" />  

  •             </fileset>  

  •         </copy>  

  •         <copy todir="${tempDir}" overwrite="true">  

  •             <fileset dir="${webDir}">  

  •                 <include name="**/*" />  

  •             </fileset>  

  •         </copy>  

  •     </target>  

  •     <!--打war包-->  

  •     <target name="war" depends="preWar">  

  •         <jar jarfile="${targetDir}/${ant.project.name}.war">  

  •             <fileset dir="${tempDir}">  

  •                 <include name="**/*" />  

  •             </fileset>  

  •         </jar>  

  •     </target>  
  •        

  •     <!-- 清理临时目录 -->  

  •     <target name="all" depends="war">  

  •         <delete dir="${tempDir}" />  

  •     </target>  

  • </project>  



<?xml version="1.0" encoding="UTF-8" ?>
<project name="pivot" default="all">
<description>The Pivot Application</description>
<!-- 定义文件夹 -->
<property name="srcDir" location="." />
<property name="classDir" location="../webapp/WEB-INF/classes" />
<property name="libDir" location="../webapp/WEB-INF/lib" />
<property name="webDir" location="../webapp" />
<!--用于输出打包的文件夹-->
<property name="tempDir" location="${java.io.tmpdir}/${ant.project.name}" />
<property name="targetDir" location="../target" />
<!-- 定义classpath -->
<path id="master-classpath">
<fileset file="${libDir}/*.jar" />
<pathelement path="${classDir}" />
</path>
<!-- 执行清理 -->
<target name="clean">
<delete dir="${classDir}" />
<delete dir="${tempDir}" />
<delete file="${targetDir}/${ant.project.name}.jar" />
<delete file="${targetDir}/${ant.project.name}.war" />
<delete dir="${targetDir}" />
</target>
<!-- 初始化任务 -->
<target name="init" depends="clean">
<mkdir dir="${targetDir}" />
<mkdir dir="${tempDir}" />
<mkdir dir="${classDir}" />
</target>
<!-- 编译 -->
<target name="compile" depends="init" description="compile the source files">
<javac srcdir="${srcDir}" destdir="${classDir}" debug="true" encoding="UTF-8">
<classpath refid="master-classpath" />
</javac>
<copy todir="${classDir}" overwrite="true">
<fileset dir="${srcDir}">
<include name="**/*.xml" />
<include name="**/*.properties" />
</fileset>
</copy>
</target>
<!--打jar包-->
<target name="jar" depends="compile">
<jar jarfile="${targetDir}/${ant.project.name}.jar">
<fileset dir="${classDir}">
<include name="**/*" />
</fileset>
</jar>
</target>
<!—准备war包 -->
<target name="preWar" depends="jar">
<copy todir="${tempDir}/WEB-INF/lib" overwrite="true">
<fileset dir="${libDir}">
<include name="*.jar" />
</fileset>
</copy>
<copy todir="${tempDir}" overwrite="true">
<fileset dir="${webDir}">
<include name="**/*" />
</fileset>
</copy>
</target>
<!--打war包-->
<target name="war" depends="preWar">
<jar jarfile="${targetDir}/${ant.project.name}.war">
<fileset dir="${tempDir}">
<include name="**/*" />
</fileset>
</jar>
</target>
<!-- 清理临时目录 -->
<target name="all" depends="war">
<delete dir="${tempDir}" />
</target>
</project>
  
    Eclipse中已经集成了ANT,只要新建build.xml的文件并且编写,右键就能直接运行,非常方便,ANT构建脚本的编写也很简单,关键是目录迭代不能遗漏,还有目录的定位,不过在IDE中都有提示,也很方便。
    将打包好的jar放到webapp/lib下并且修改页面的js段代码即可看到效果了。
DSC0009.jpg
    欢迎大家交流。
    (全篇完)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-322569-1-1.html 上篇帖子: apache tiles的点滴 下篇帖子: Apache Camel框架入门示例
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表