配置flex+bleazeds+spring+hibernate
准备:
我个人开发时的配置:
MYSQL:
MySQL 5.0
JDK:
j2sdk-1.5.0.0
TOMCAT:
tomcat-5.0.28
ECLIPSE:
eclipse-3.2
MYECLIPSE插件:
MyEclipse-5.0
Flex:
FB3_WWEJ_Plugin.exe
BLEAZEDS:
BlazeDS
http://opensource.adobe.com/wiki/display/blazeds/Release+Builds,它有三个版本,其中Turnkey版内置了Tomcat和 Flex 3 SDK,还包含blazeds.war、ds-console.war和samples.war,建议初学者使用;Binary Distribution版只包括blazeds.war,此为最简配置;而Source则包含blazeds的Java源代码,想做二次开发就下载这个。
安装:
一:
安装mysql,jdk,tomcat,eclipse,myeclipse并正确配置环境变量。
二:安装flex3
1. 首先下载安装Eclipse 3.3 解压到 D:\Program Files\eclipse 到算安装成功了.
2. 下载安装Flex Builder 3.0
http://trials.adobe.com/Applications/Flex/FlexBuilder/3/FB3_WWEJ_Plugin.exe
1. 指定 Flex Builder 3.0 安装路径为D:\Program Files\eclipse\Flex Builder,进下一步. (你也可设为其它目录,但不要这样:D:\Program Files\eclipse)
2. 指定 Eclipse 安装路径为 D:\Program Files\eclipse 进下一步.
3. 选择是否安装浏览器 Flash Plyer 9 插件(可选安装)
安装要花一点时间.大家慢慢等待...出现一些提示你不用管它.
等安装完后运行 Eclipse.exe 你是看不到 Flex的..往下看我会帮你解决这个问题^_^
3. 绿化Flex Builder 这是重点.
1. 把D:\Program Files\eclipse\Flex Builder\com.adobe.flexbuilder.update.site\下的 plugins、features、site.xml
复制到 D:\Program Files\eclipse\ (如果你的eclipse目录已经有一个site.xml 你只要把两个文件的feature节内容合并就好了.)
2. 把D:\Program Files\eclipse\Flex Builder\eclipse\下的 plugins、features(如果有的话,这是Eclipse的一些更新)
复制到 D:\Program Files\eclipse\
3. 把D:\Program Files\eclipse\Flex Builder\sdks 复制到D:\Program Files\eclipse\
OK,Flex Builder安装完成,接下来就是启动Elipse
我们现在已经可以找到Flex了.但还没完,请往下看.
4. 进行Flex的配置.
在Eclipse中.点击->Window->Preferences->Flex->Installed Flex SDKs
修改Flex SDKs 2.0 和 Flex SDKs 3.0的路径点Apply让设置成效.
重启Eclipse..你就可以开发Flex了.
5. 现在你的Flex已经开始工作了.但之前安装的Flex Builder怎么办呢.哈哈.
运行这个D:\Program Files\eclipse\Flex Builder\Uninstall Adobe Flex Builder 3 Plug-in\Uninstall Adobe Flex Builder 3 Plug-in.exe就可以了.
三:bleazeds
解压到任何一个地方就可以,主要用到里面的flex文件夹和lib文件夹
HELLO WORLD示例:
1. 建立数据表
CREATE TABLE `user` (
`UserID` int(11) NOT NULL auto_increment,
`UserName` varchar(20) default NULL,
`Password` varchar(20) default NULL,
`Email` varchar(20) default NULL,
PRIMARY KEY (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
2. 插入测试数据
3. 搭配flex+bleazeds的web项目
1.新建web项目MyFirstFSH
按NEXT进入下一步
点击FINISH
2.添加bleazeds支持
1.找到已解压的 bleazeds 文件夹,进入到:blazeds\WEB-INF
复制flex和lib两个文件夹到MyFirstFSH下的webRoot/WEB-INF目录下
2.在web.xml文件中添加代码:
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<!-- MessageBroker Servlet -->
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
将web工程添加到tomcat中
按
3.添加flex工程
1.右键MyFirtstFSH,选择:Flex Project Nature-Add Flex Project Nature
注:选择J2EE,作用LCDS远程对象
Root folder:选择刚才Deploy的工程路径
Root URL:TOMCAT服务器的启动路径
Context root :编译后的FLEX文件保存路径
Output folder:工程 重构后FLEX文件的输出路径
[点击Finish完成]
2配置Flex 项目属性
在Flex视图下,右键 properties打开项目属性设置对话框.选择 “Flex Build Path”,出现以下对话框:
Output folder:工程FLEX文件的输出路径
Output folder URL:工程在TOMCAT服务器的启动路径
――――――――――――――――――――――――――
选择 “Flex Compiler”,出现以下对话框:
选择你当前用的FLEX SDK(如何你前面步骤配置好的话就不会出错了!)
[点击Apply应用]
―――――――――――――――――――――――――――――――――――
选择 “Flex Server”,出现以下对话框:
Context root:是你的工程的根目录
[点击OK完成设置]
这时会出现错误提示:
右键“错误”信息,Recreate …
3.到了这个时候基本上已经搭建好flex+bleazeds的web环境,现在来测试一下。
新建一个包:com.wingo.myfirstfsh.web.ro
在包中新一个类:HelloWorld,作用flex的一个远程对象
package com.wingo.myfirst.web.ro;
public class HelloWorld {
public String sayHelloTo(String name){
return "Hello "+name;
}
}
在WEB-INF/flex/remotiong-config.xml中注册这个远程对象
<destination id="Hello">
<properties>
<source>com.wingo.myfirstfsh.web.ro.HelloWorld</source>
</properties>
</destination>
编辑FLEX应用程序
在src/ MyFirstFSH.mxml 中添加以下代码:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:RemoteObject id="say" destination="Hello">
</mx:RemoteObject> <mx:Button x="335" y="80" label=" Click" click="remotingSayHello(event);"/> <mx:TextInput x="159" y="80" id="tiName"/> <mx:Label x="109" y="82" text="name:"/> <mx:Label text="{say.sayHelloTo.lastResult}" x="44" y="162" width="448" height="71" id="lblView" color="#FCEE09" fontSize="20" fontWeight="bold" textDecoration="underline" fontStyle="normal"/> <mx:Script> <![CDATA[ function remotingSayHello(event:Event):void{ var iname:String=tiName.text; say.sayHelloTo(iname); } ]]> </mx:Script> </mx:Application> |
启动tomcat 打开http://localhost:8080/MyFirstFSH/pages/MyFirstFSH.html
到了这一步,flex+bleazeds已经完成
4.加上spring
1.右键Myeclipse-Add spring
2.添加包: com.wingo.myfirstfsh.common
3.添加SpringFactory.java(网上的)
package com.wingo.myfirstfsh.common;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;
/**
* This interface is implemented by factory components which provide
* instances to the flex messaging framework. To configure flex data services
* to use this factory, add the following lines to your services-config.xml
* file (located in the WEB-INF/flex directory of your web application).
*
* <factories>
* <factory id="spring" class="flex.samples.factories.SpringFactory" />
* </factories>
*
* You also must configure the web application to use spring and must copy the spring.jar
* file into your WEB-INF/lib directory. To configure your app server to use spring,
* you add the following lines to your WEB-INF/web.xml file:
*
* <context-param>
* <param-name>contextConfigLocation</param-name>
* <param-value>/WEB-INF/applicationContext.xml</param-value>
* </context-param>
*
* <listener>
* <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
* </listener>
*
* Then you put your spring bean configuration in WEB-INF/applicationContext.xml (as per the
* line above). For example:
*
* <?xml version="1.0" encoding="UTF-8"?>
* <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
*
* <beans>
* <bean name="weatherBean" class="dev.weather.WeatherService" singleton="true"/>
* </beans>
*
* Now you are ready to define a destination in flex that maps to this existing service.
* To do this you'd add this to your WEB-INF/flex/remoting-config.xml:
*
* <destination id="WeatherService">
* <properties>
* <factory>spring</factory>
* <source>weatherBean</source>
* </properties>
* </destination>
*
* @author Jeff Vroom
*/
public class SpringFactory implements FlexFactory
{
private static final String SOURCE = "source";
/**
* This method can be used to initialize the factory itself. It is called with configuration
* parameters from the factory tag which defines the id of the factory.
*/
public void initialize(String id, ConfigMap configMap) {}
/**
* This method is called when we initialize the definition of an instance
* which will be looked up by this factory. It should validate that
* the properties supplied are valid to define an instance.
* Any valid properties used for this configuration must be accessed to
* avoid warnings about unused configuration elements. If your factory
* is only used for application scoped components, this method can simply
* return a factory instance which delegates the creation of the component
* to the FactoryInstance's lookup method.
*/
public FactoryInstance createFactoryInstance(String id, ConfigMap properties)
{
SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);
instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
return instance;
} // end method createFactoryInstance()
/**
* Returns the instance specified by the source
* and properties arguments. For the factory, this may mean
* constructing a new instance, optionally registering it in some other
* name space such as the session or JNDI, and then returning it
* or it may mean creating a new instance and returning it.
* This method is called for each request to operate on the
* given item by the system so it should be relatively efficient.
* <p>
* If your factory does not support the scope property, it
* report an error if scope is supplied in the properties
* for this instance.
*/
public Object lookup(FactoryInstance inst)
{
SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;
return factoryInstance.lookup();
}
static class SpringFactoryInstance extends FactoryInstance
{
SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties)
{
super(factory, id, properties);
}
public String toString()
{
return "SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope();
}
public Object lookup()
{
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());
String beanName = getSource();
try
{
return appContext.getBean(beanName);
}
catch (NoSuchBeanDefinitionException nexc)
{
ServiceException e = new ServiceException();
String msg = "Spring service named '" + beanName + "' does not exist.";
e.setMessage(msg);
e.setRootCause(nexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
}
catch (BeansException bexc)
{
ServiceException e = new ServiceException();
String msg = "Unable to create Spring service named '" + beanName + "' ";
e.setMessage(msg);
e.setRootCause(bexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
}
}
}
}
4.在WEB-INF/flex中的services-config.xml中注册SpringFactory工厂
<factories>
<factory id="spring" class="com.wingo.myfirstfsh.common.SpringFactory" />
</factories>
5.在WEB-INF/web.xml中加入对spring的支持
<!-- Backend configuration -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>SpringLog4jConfigServlet</servlet-name>
<servlet-class>
org.springframework.web.util.Log4jConfigServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>web</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
5.添加hibernate
1.添加数据源
2.加进hibernate
右键MyFirstFSH-MyEclipes-Add Hibernate
点击:Finish
3.添加包:
结构如下
4.删掉jar:
5.利用myeclipse自动生成User.java,User.hbm.xml,UserDAO
打开:
6.把UserDAO放到com.wingo.myfirstfsh.dao.impl下
7.修改applicationContext.xml中对
<bean id="userDAO" class="com.wingo.myfirstfsh.dao.impl.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
8.添加类
代码如下:
package com.wingo.myfirstfsh.service.impl;
import com.wingo.myfirstfsh.dao.impl.UserDAO;
import com.wingo.myfirstfsh.vo.User;
public class IUserServiceImpl {
private UserDAO userDAO;
/*通过ID搜索返回一个User对象
* @param id 需要查找的用户ID
* @return User对象
*
*/
public User getUser(Integer id){
return userDAO.findById(id);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
9.修改HellWorld.java文件
代码如下:
package com.wingo.myfirstfsh.web.ro;
import com.wingo.myfirstfsh.service.impl.IUserServiceImpl;
import com.wingo.myfirstfsh.vo.User;
public class HelloWorld {
private IUserServiceImpl userService;
/*
* 该方法通过用户ID返回用户名
* @param id 用记ID
* @return String “你好 ”+用户名
*/
public String sayHelloTo(String id){
User user = new User();
user = userService.getUser(Integer.valueOf(id));
return "Hello "+user.getUserName();
}
public IUserServiceImpl getUserService() {
return userService;
}
public void setUserService(IUserServiceImpl userService) {
this.userService = userService;
}
}
10.在WEB-INF/applicationContext.xml注册bean
applicationContext.xml增加的代码:
<bean id="userService"
class="com.wingo.myfirstfsh.service.impl.IUserServiceImpl"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<property name="userDAO">
<ref local="userDAO" />
</property>
</bean>
<bean name="helloWorld"
class="com.wingo.myfirstfsh.web.ro.HelloWorld" abstract="false"
lazy-init="default" autowire="default" dependency-check="default">
<property name="userService">
<ref local="userService" />
</property>
</bean>
11.修改:WEB-INF/flex/remoting-config.xml
<destination id="Hello">
<properties>
<factory>spring</factory>
<source>helloWorld</source>
</properties>
</destination>
到此已完成所以配置,测试一下
启动Tomcat http://localhost:8080/MyFirstFSH/pages/MyFirstFSH.html