构建Flex4为前端的JavaEE项目.docx
《构建Flex4为前端的JavaEE项目.docx》由会员分享,可在线阅读,更多相关《构建Flex4为前端的JavaEE项目.docx(34页珍藏版)》请在冰点文库上搜索。
![构建Flex4为前端的JavaEE项目.docx](https://file1.bingdoc.com/fileroot1/2023-5/7/d92745b4-5694-4988-aba1-401d091e6ab9/d92745b4-5694-4988-aba1-401d091e6ab91.gif)
构建Flex4为前端的JavaEE项目
构建Flex4为前端的JavaEE项目
第一步:
添加ApacheTomcat运行时
1.从Window菜单选择Preferences
2.在Preferences对话框中展开Server,然后选择RuntimeEnvironments
3.在ServerRuntimeEnvironments页点击Add,打开NewServerRuntimeEnvironment对话框
4.在NewServerRuntimeEnvironment页展开Apache
5.从下面支持的ApacheTomcat服务器中选择一个(我用的是ApacheTomcatv6.0):
*ApacheTomcatv6.0
6.当你添加一个ServerRuntimeEnvironment时,默认会创建一个Server并作为实体添加在Servers视图(View)内。
如果你只想添加serverruntimeenvironment而不想在Servers视图内创建Server,那么清除Createanewlocalserver前的多选框(我们选上这个多选框免得之后手动创建Server)
7.当你点击Next时会打开TomcatServer页
8.在Tomcatinstallationdirectory项选择ApacheTomcat目录(例如:
D:
\apache-tomcat-6.0.26)
9.点击TomcatServer页的Finish
10.点击ServerRuntimeEnvironment页的OK(如6所述,此时Servers视图内会显示一个Server)
第二步:
使用WTP创建Java/Flex组合项目
1.切换到JavaEE视图(perspective)
2.在ProjectExplorer视图(View)内点击右键,选择New项
3.选择子菜单中的Project…项,打开NewProject对话框
4.展开FlashBuilder,选择“Flex项目“(因为我是中文系统所以FlashBuilder的菜单项都显示为中文,尽管我的Eclipse为英文)
5.点击Next
6.在“新建Flex项目”对话框中对应以下几项:
*项目名:
sampleApp
*项目位置:
默认即可
*应用程序类型:
Web
*FlexSDK版本:
默认即可
*应用程序服务器类型:
J2EE
*远程对象访问服务:
选择BlazeDS
*使用WTP创建Java/Flex组合项目:
选上
7.点击Next
8.在“配置J2EE服务器”页对应以下两项,其他项默认即可
*目标运行时:
ApacheTomcatv6.0
*BlazeDSWAR文件:
选择上面准备好的blazeds.war
9.点击Next
*输出文件夹URL:
http:
//localhost:
8080/sampleApp
10.点击Finish
11.按提示切换到Flash视图(perspective),向sampleApp.mxml中拖入DataGrid控件以备后用
项目结构图:
第三步:
运行sampleApp项目
1.重新切换到JavaEE视图(perspective)
2.在Servers视图(View)中的Server内添加sampleApp项目
3.启动此Server
4.以“Web应用程序”的方式运行项目
5.如果看到刚才拖入的表格,恭喜你成功了:
)
第四步:
使Flex以RemoteObject的方式与Java交互
是不是觉得表格太空洞了?
下面我们用它显示雇员信息,借此演示Flex与Java的交互过程。
1.创建com.sample包
2.在包内创建两个类:
雇员类Employee,雇员的Service类EmployeeService
packagecom.sample;
publicclassEmployee{
privateStringname;
privateintage;
privateStringemail;
publicEmployee(Stringname,intage,Stringemail){
this.name=name;
this.age=age;
this.email=email;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetName(){
returnname;
}
publicvoidsetAge(intage){
this.age=age;
}
publicintgetAge(){
returnage;
}
publicvoidsetEmail(Stringemail){
this.email=email;
}
publicStringgetEmail(){
returnemail;
}
}
packagecom.sample;
importjava.util.ArrayList;
publicclassEmployeeService{
publicArrayListgetList(){
ArrayListtempList=newArrayList();
for(inti=1;i<=30;i++){
tempList.add(newEmployee("Smith"+i,20+i,"smith"+i+"@"));
}
returntempList;
}
}
3.在remoting-config.xml文件中定义EmployeeService对应的destination
com.sample.EmployeeService
4.在sampleApp.mxml中通过employeeServiceDest调用EmployeeService的getList()方法
*定义显示雇员信息的表格
DataGridx="32"y="25"width="400"dataProvider="{employeeList}">
columns>
DataGridColumnheaderText="Name"dataField="name"/>
DataGridColumnheaderText="Age"dataField="age"/>
DataGridColumnheaderText="Email"dataField="email"/>
columns>
DataGrid>
*定义RemoteObject组件
Declarations>
RemoteObjectid="employeeServiceRO"destination="employeeServiceDest"
result="resultHandler(event);"
fault="faultHandler(event);"/>
Declarations>
*定义相关函数
Script>
[CDATA[
importmx.controls.Alert;
importmx.rpc.events.ResultEvent;
importmx.rpc.events.FaultEvent;
[Bindable]
privatevaremployeeList:
Object;
privatefunctioninit():
void{
employeeServiceRO.getList();
}
privatefunctionresultHandler(event:
ResultEvent):
void{
employeeList=event.result;
}
privatefunctionfaultHandler(event:
FaultEvent):
void{
//Alert.show(event.fault.faultString,'Error');
Alert.show(event.toString(),'Error');
}
]]>
Script>
*当Application完成构建后立即触发init()方法,以实现对Server端Java的调用
Applicationxmlns:
fx="
xmlns:
s="library:
//
xmlns:
mx="library:
//
minWidth="955"minHeight="600"
creationComplete="init();">
第五步:
重新运行sampleApp项目
很不幸,RPC过程失败了(AdobeFlashBuilder的Bug吗?
)
注意到上图用黑色背景标注的内容了吧?
本应该是sampleApp,但现在却成了WebContent。
我们需要处理一下:
打开项目根文件夹下的.flexProperties文件,更改其中的serverContextRoot=”/WebContent”为serverContextRoot=”/sampleApp”。
OK,再运行试试吧(别忘了刷新项目)。
Flex4系列教程之七–整合Flex,BlazeDS,Spring(>=2.5.6)
SpringBlazeDSIntegration是什么?
SpringBlazeDSIntegration是SpringSource的开源项目,用于整合Spring与BlazeDS。
为什么需要SpringBlazeDSIntegration?
正如“Flex4系列教程之六”介绍的:
不使用SpringBlazeDSIntegration同样可以整合Spring与BlazeDS。
但这种整合方式不自然,需要额外维护一个BlazeDS配置文件,SpringBlazeDSIntegration会改善这种处境。
SpringBlazeDSIntegration需要的软件环境:
*Java5或更高
*Spring2.5.6或更高
*BlazeDS3.2或更高
SpringBlazeDSIntegration特征
*MessageBroker(BlazeDS的核心组件)被配置为Spring管理的Bean
*Flex客户端发出的HTTP消息通过Spring的DispatcherServlet路由给MessageBroker
*Remoteobjects以Spring的方式配置在Spring配置文件内
注意事项:
以下内容基于“Flex4系列教程之五”中创建的sampleApp项目。
在继续本教程之前你需要准备好以下事项:
*下载SpringFramework(截稿时最新版spring-framework3.0.2),解压备用
*下载SpringFrameworkdependencies(截稿时最新版spring-framework3.0.2dependencies),解压备用
*下载SpringBlazeDSIntegration(截稿时最新版spring-flex1.0.3),解压备用
第一步:
准备所需jar包
将以下3部分jar包拷贝到sampleApp项目的lib下
1.SpringFramework
org.springframework.aop-3.0.2.RELEASE.jar
org.springframework.asm-3.0.2.RELEASE.jar
org.springframework.beans-3.0.2.RELEASE.jar
org.springframework.context-3.0.2.RELEASE.jar
org.springframework.core-3.0.2.RELEASE.jar
org.springframework.expression-3.0.2.RELEASE.jar
org.springframework.web.servlet-3.0.2.RELEASE.jar
org.springframework.web-3.0.2.RELEASE.jar
2.SpringFrameworkdependencies
org.aopalliance内的com.springsource.org.aopalliance-1.0.0.jar
edu.emory.mathcs.backport内的com.springsource.edu.emory.mathcs.backport-3.0.0.jar
net.sourceforge.cglib内的.sf.cglib-2.2.0.jar
[注:
]Spring3的依赖包用Ivy或Maven管理会很方便,完成本系列教程后我会单独整理这部分。
暂且手动拷贝吧:
)
3.SpringBlazeDSIntegration
org.springframework.flex-1.0.3.RELEASE.jar
第二步:
修改web.xml文件
将web.xml内所有Flex相关配置删除掉,添加以下内容(改用Springweb应用的前端控制器处理所有应用请求)
SpringMVCDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/web-application-config.xml
1
SpringMVCDispatcherServlet
/messagebroker/*
第三步:
配置web-application-config.xml
1.创建应用上下文配置文件web-application-config.xml
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd">
2.为了使用SpringBlazeDSIntegration的tag,增加命名空间
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
flex="http:
//www.springframework.org/schema/flex"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/flex
http:
//www.springframework.org/schema/flex/spring-flex-1.0.xsd">
3.为了把请求路由给MessageBroker,添加以下tag
message-broker/>
4.定义Bean,并用remoting-destinationtag把它暴露给Flex
remoting-destination/>
第四步:
删除多余的Flex配置文件
删除services-config.xml以外的所有Flex配置文件(你认为它们还有必要保留吗?
)。
但千万别忘记在services-config.xml内重新定义默认channel(原来定义在remoting-config.xml内):
修改services-config.xml,替换
为
第五步:
重新运行sampleApp项目
运行结果与整合之前相同吧:
)
Flex4系列教程之八–整合iBATIS2.3
注意事项:
以下内容基于“Flex4系列教程之七”中最后形成的sampleApp项目。
Spring2.5.6之前版本的整合方式与本篇基本相同,不再重复。
配置数据源
是时候改用DB存储sampleApp中的雇员(Employee)信息了。
我们采用Mysql,并假定你已安装它(截稿时最新版MySQLCommunityServer5.1.47)。
1.准备数据库
*创建数据库sample
createdatabasesampledefaultcharactersetGBK;
*创建表employees
CREATETABLEIFNOTEXISTS`employees`(
`id`int(11)NOTNULLAUTO_INCREMENT,
`name`varchar(20)NOTNULLDEFAULT'',
`age`int
(2)NOTNULLDEFAULT'0',
`email`varchar(100)NOTNULLDEFAULT'',
PRIMARYKEY(`id`)
)ENGINE=InnoDBDEFAULTCHARSET=gbk;
*插入一些模拟数据
insertintoemployees(name,age,email)values('张作强',22,'csdn.eric@');
insertintoemployees(name,age,email)values('陈惠仪',20,'chen.huiyi@');
insertintoemployees(name,age,email)values('张文轩',2,'wenxuan@');
insertintoemployees(name,age,email)values('张文婷',1,'wenting@');
insertintoemployees(name,age,email)values('周进云',22,'jinyun@');
2.准备所需组件
*下载以下组件解压备用
CommonsDBCP(截稿时最新版commons-dbcp1.4)
CommonsPool(截稿时最新版commons-pool1.5.4)
Connector/J(截稿时最新版mysql-connector-java5.1.12)
*拷贝jar包
将解压后的mysql-connector-java-5.1.13-bin.jar,commons-pool-1.5.4.jar和commons-dbcp-1.4.jar拷贝到sampleApp的lib下
3.使用单独文件存储DB驱动等信息
*创建resources包
*在包下创建jdbc.properties文件,输入你的DB信息
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:
mysql:
//localhost:
3306/sample?
userUnicode=true&characterEnc