加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

第一个Flex 的程序出炉了

发布时间:2020-12-15 01:17:07 所属栏目:百科 来源:网络整理
导读:开发环境: Eclipse For JavaEE,FlashBuilder4,Tomcat6.0 开发步骤: 1.? 新建web工程xslt,加入以下文件: ConnectionFactory.java package com; import java.sql.*; //调用SQL包 public class ConnectionFactory { ?private static ConnectionFactory ref = n

开发环境:

Eclipse For JavaEE,FlashBuilder4,Tomcat6.0

开发步骤:

1.? 新建web工程xslt,加入以下文件:

ConnectionFactory.java

package com;

import java.sql.*; //调用SQL包

public class ConnectionFactory {

?private static ConnectionFactory ref = new ConnectionFactory();
?// 连接mysql数据库, database : test? user : root? password : 1272107226
?private ConnectionFactory()
?{
? try{
?? Class.forName("com.mysql.jdbc.Driver");
? }catch(ClassNotFoundException e)
? {
?? System.out.println("ERROR: exception loading driver class");
? }
?}
?public static Connection getConnection() throws SQLException{
?
? String url = new String ("jdbc:mysql://localhost:3306/test?user=root&password=root");
? return DriverManager.getConnection(url);
?}
?
?public static void close(ResultSet rs)
?{
? try{
?? rs.close();
? }catch(Exception ignored){}
?}
?public static void close(Statement stmt)
?{
? try{
?? stmt.close();
? }catch(Exception ignored){}
?}
?public static void close(Connection conn)
?{
? try{
?? conn.close();
? }catch(Exception ignored){}
?}
?
}

新建DataServiceImpl.java

package com;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.ConnectionFactory;

public class DataServiceImpl {
?
?String sql;? // 定义类型
?Connection conn = null;
?Statement stmt = null;
?ResultSet rs = null;
?
?public DataServiceImpl(){
?
?}
?
?
?public List<NoticeInfo> getNotices(){
?
? ArrayList<NoticeInfo> noticeList = new ArrayList<NoticeInfo>();
?
? try{
?? conn = ConnectionFactory.getConnection();
?? stmt = conn.createStatement();
? ?
?? String sql = "select userid,username,contents,dates from gbook ";
?? System.out.println("this is =============>" + sql);
?? System.out.println("connect to db is successful~~~");
?? rs = stmt.executeQuery(sql); ?
? ?
? ?
?? while(rs.next()){
?? ?
??? NoticeInfo temp = new NoticeInfo();
??? temp.setUserid(rs.getInt("userid"));
??? temp.setUsername(rs.getString("username"));
??? temp.setContents(rs.getString("contents"));
??? temp.setDates(rs.getString("dates"));
??? noticeList.add(temp);
?? ?
?? }
? System.out.println(noticeList.size() + " 条数据!");
?? return noticeList;
? }catch(SQLException e){
?? e.printStackTrace();
?? return null;
? }
?}
?public String getHello(String name){
?? ? return "hi? " + name;
?}

}

新建NoticeInfo .java

package com;

import java.io.Serializable;

public class NoticeInfo? implements Serializable {
?
?/**
?? ? *
?? ? */
?? ?private static final long serialVersionUID = 1L;
private int userid;
?private String username;
?private String contents;
?private String dates;
?
?
?public String getContents() {
? return contents;
?}
?public void setContents(String contents) {
? this.contents = contents;
?}
?public String getDates() {
? return dates;
?}
?public void setDates(String dates) {
? this.dates = dates;
?}
?
?
?public int getUserid() {
? return userid;
?}
?public void setUserid(int userid) {
? this.userid = userid;
?}
?public String getUsername() {
? return username;
?}
?public void setUsername(String username) {
? this.username = username;
?}
?
?
}



2? .下载blazeds.rar文件,这里有有一些lib和flex所需要的XML文件,解压后将如图文件夹复制到原web项目的WEB-INF下面

新建flex项目HelloWorld,如图将项目位置选择tomcat中的xslt



3.? 在FlashBuilder4中新建flex项目操作如图:



4.? 开发flex代码:

在flex文件夹的remoting-config中加入java接口类配制

<destination id="UserDao">? ?
???? <properties>? ?
????????? <source>com.DataServiceImpl</source>? ?
???? </properties>? ?
</destination>


5.? 在项目的src中新建如下文件:

NoticeInfo.as文件

// ActionScript file
package com.yeeach
{
?? ?[Bindable]
?? ?[RemoteClass(alias="com.NoticeInfo")]
?? ?public class NoticeInfo
?? ?{
?? ??? ?public var userid:int;
?? ??? ?public var username:String;
?? ??? ?public var contents:String;
?? ??? ?public var dates:Date;
?? ?}
?? ?
?? ?
}

HelloWorld.mxml文件

<?xml version="1.0" encoding="utf-8"?>
<!--自动生成头信息-->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
?? ??? ??? ??? xmlns:s="library://ns.adobe.com/flex/spark"
?? ??? ??? ??? xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" width="821" height="674"
?? ??? ??? ??? preloaderChromeColor="#9E4040" backgroundColor="#674F4F">
?? ?<fx:Style source="helloword.css"/>
?? ?
?? ?<fx:Declarations>
?? ??? ?<!-- 将非可视元素(例如服务、值对象)放在此处 定义操作接口名-->
?? ??? ?<s:RemoteObject id="UserDao" destination="UserDao">
?? ??? ??? ?<!--name为UserDao中的方法名,result指定方法执行后处理返回值的访求 ,fault指定出错时执行的方法-->
?? ??? ??? ?<s:method? showBusyCursor="true" name="getNotices" result="changeDate(event)" fault="UserDao_faultHandler(event)"/> ?
?? ??? ?</s:RemoteObject>
?? ?</fx:Declarations>
?? ?<!--按钮的点击事件-->
?? ?<mx:Button x="10" y="139" label="获得list" click="remotingList(event)" fontSize="12"/> ?
?? ?<!--表格控件 ,将list中的数据显示在些处-->
?? ?<mx:DataGrid x="10" y="164" width="714" height="328"? id="myDG">
?? ??? ?<mx:columns>
?? ??? ??? ?<fx:Array>
?? ??? ??? ??? ?<!--dataField为对象的属性-->
?? ??? ??? ?<mx:DataGridColumn headerText="用户ID" dataField="userid"/>
?? ??? ??? ?<mx:DataGridColumn headerText="用户名" dataField="username"/>
?? ??? ??? ?<mx:DataGridColumn headerText="说明" dataField="contents"/>
?? ??? ??? ?<mx:DataGridColumn headerText="日期" dataField="dates"/>
?? ??? ??? ?</fx:Array>
?? ??? ?</mx:columns>
?? ?</mx:DataGrid>
?? ?
?? ?
?? ?<fx:Script>
?? ??? ?<![CDATA[
?? ??? ??? ?//自定义与java相同结构名字为NoticeInfo.as
?? ??? ??? ?import com.yeeach.NoticeInfo;
?? ??? ??? ?
?? ??? ??? ?import mx.collections.ArrayCollection;
?? ??? ??? ?import mx.controls.Alert;
?? ??? ??? ?import mx.rpc.events.AbstractEvent;
?? ??? ??? ?import mx.rpc.events.FaultEvent;
?? ??? ??? ?import mx.rpc.events.ResultEvent;
?? ??? ??? ?import mx.utils.ArrayUtil;
?? ??? ??? ?//定义一个数组
?? ??? ??? ?public function changeDate(event:ResultEvent):void
?? ??? ??? ?{?? ??? ??? ?
?? ??? ??? ??? ?if(event.result!=null){
?? ??? ??? ??? ??? ?//将结果放入表格中
?? ??? ??? ??? ??? ?myDG.dataProvider = event.result;
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?Alert.show("没有找到相关任务信息");
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?protected function UserDao_faultHandler(event:FaultEvent):void
?? ??? ??? ?{
?? ??? ??? ??? ?Alert.show(event.fault.toString());
?? ??? ??? ?}
?? ??? ??? ?public function remotingList(event:Event):void{
?? ??? ??? ??? ?UserDao.getNotices(); ?
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?]]>
?? ?</fx:Script>
</s:Application>
这里就大功告成了,启动tomcat运行就可以了

如果如图:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读