轻松打造J2EE的 SSH 与flash builder整合(由Spring类管理)
在此将我所配置方法,,告知大家..让大家少走弯路
下面将由我来 手把手教大家 如果 配置SSH 以及将flash builder整合到一起
并由Spring类来管理,,使flash builder直接通过AMF访问java后台类方
环境:
My Eclipse 8.5
Struts 1.2
Spring 2
hibernate 3.1
flash builder 4 / 或插件版
1.前期环境
新建项目名为: MyWeb
建包:
com.sshf.study.entity (实体包)
com.sshf.study.dao(数据访问包)
com.sshf.study.biz(业务逻辑)
并在 com.sshf.study.biz 包下
新建接口 IHelloWord.java
并再建包 impl, HellWordImpl.java 类(全速路径 com.sshf.study.biz.impl.HellWordImpl). 并实现IHelloWord接口
代码如下:
package com.sshf.study.biz;
import com.sshf.study.biz.IHelloWord;
public class HelloWordImpl implements IHelloWord{
/**
* 测试方法
* @param name
* @return
*/
public String sayHello(String name){
return "HelloHello! " + name;
}
}
稍后我们会 测试调用这个方法!
2.开始SSH搭建.
a.首先加入Spring框架
自己加入DataSource sessionFactory 实体bean DAOBean 以及事务..养成好习惯
以下是我的初期配置(稍后会添加):
<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 配置事务管理 -->
<bean id="myHibTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--配置事务管理策略-->
<tx:advice id="txAdvice" transaction-manager="myHibTxManager">
<tx:attributes>
<tx:method name="to*" propagation="SUPPORTS" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<!--指定用于那些地方-->
<aop:pointcut id="bizMethods" expression="execution(* com.sshf.study.*.impl.*.*(..))" />
<!-- 织入 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />
</aop:config>
<!-- dataSource -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url" value="jdbc:sqlserver://localhost:1433"></property>
<property name="username" value="sa"></property>
<property name="password" value="dongqiao"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>entity/ConfigQuestionSecondKind.hbm.xml</value>
<value>entity/EngageSubjects.hbm.xml</value>
<value>entity/ConfigQuestionFirstKind.hbm.xml</value></list>
</property>
</bean>
<!-- DAO -->
<!-- biz -->
<bean id="HW" class="com.sshf.study.biz.impl.HellWordImpl">
</bean>
<!-- action -->
</beans>
b.添加Struts支持
如用到Action 一定不要忘了改 代理类 org.springframework.web.struts.DelegatingActionProxy
c.添加hibernate支持.
以上二步省略.. 相信大家都会配..
3.开始将flash builder整合到SSH中
a.下载blazeDS包 将WEB-INF 文件夹下的所有文件夹和文件 拷贝到/WebRoot/WEB-INF/目录下
注意:包括web.xml 全覆盖
b.打开Web.xml 进行配置spring
<!--在web.xml中引入Spring的代码-->
<context-param>
<!-- 参数名为contextConfigLocation -->
<param-name>contextConfigLocation</param-name>
<!-- 配置多个文件之间,以","隔开 -->
<param-value>/WEB-INF/classes/app.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
(此处app.xml为spring配置文件名 默认为applicationContext.xml)
到此为止..SSH配置完毕..部署运行一下..看是否有错误..
如有错误 自己 改正. 在确保SSH集成后无错 方可 继续.!!
以下步骤很重要
b.新建包com.sshf.study.flash
在该包下新建二个类 SpringFactoryInstance.java FlexFactoryImpl.java
SpringFactoryInstance.java代码
package com.sshf.study.flash;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import flex.messaging.FactoryInstance;
import flex.messaging.FlexContext;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;
public class SpringFactoryInstance extends FactoryInstance {
private Log log = LogFactory.getLog(getClass());
SpringFactoryInstance(FlexFactory factory, String id, ConfigMap properties) {
super(factory, id, properties);
}
public Object lookup() {
System.out.println("3---spring工厂类的方法lookup");
ApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(FlexContext.getServletConfig().getServletContext());
String beanName = getSource();
try {
log.info("Lookup bean from Spring ApplicationContext: " + beanName);
return appContext.getBean(beanName);
} catch (NoSuchBeanDefinitionException nex) {
ServiceException e = new ServiceException();
String msg = "Spring service named '" + beanName + "' does not exist.";
e.setMessage(msg);
e.setRootCause(nex);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
} catch (BeansException bex) {
ServiceException e = new ServiceException();
String msg = "Unable to create Spring service named '" + beanName + "'.";
e.setMessage(msg);
e.setRootCause(bex);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
} catch (Exception ex) {
ServiceException e = new ServiceException();
String msg = "Unexpected exception when trying to create Spring service named '" + beanName + "'.";
e.setMessage(msg);
e.setRootCause(ex);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
}
}
}
FlexFactoryImpl.java代码
package com.sshf.study.flash;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
public class FlexFactoryImpl implements FlexFactory {
private Log log = LogFactory.getLog(getClass());
/*override interface method*/
public void initialize(String id, ConfigMap configMap) {
System.out.println("1---flex工厂实现类重写的方法initialize");
}
/*override interface method*/
public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
System.out.println("2---flex工厂实现类重写的方法createFactoryInstance");
log.info("Create FactoryInstance.");
SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);
instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
return instance;
}
/*override interface method*/
public Object lookup(FactoryInstance instanceInfo) {
System.out.println("4---flex工厂实现类重写的方法lookup");
log.info("Lookup service object.");
return instanceInfo.lookup();
}
}
打开/WEB-INF/flex/services-config.xml 配置文件
加入FlexFactory实现类
<factories>
<factory id="flexFactoryImpl" class="com.sshf.study.flash.FlexFactoryImpl"/>
</factories>
打开/WEB-INF/flex/remoting-config.xml 配置文件
然后将 services中的factory的实现类所调用的类配置关联
<destination id="flash">
<properties>
<!-- flexFactory实现类 -->
<factory>flexFactoryImpl</factory>
<!-- 对外的访问类 为spring配置中的bean id -->
<source>HW</source>
<scope>application</scope>
</properties>
</destination>
到此为止 配置完毕
部署运行..有错误改之
4.新建一个flex 项目
并新建一个mxml
不一一做介绍了..添加一个 文本框 和一个 按钮..
点击 按钮 将 文本框中 字符串 传入到 HelloWordImpl 的 sayHello中.并取返回值 输出
代码如下:
<?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">
<fx:Script>
<!--[CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function button1_clickHandler(event:MouseEvent):void
{
var remote:RemoteObject = new RemoteObject();
remote.destination = "flash";//remoting-config中destination ID
//访问地址可改 在web.xml中修改 这里默认
remote.endpoint = "http://localhost:8080/myWeb/messagebroker/amf";
remote.addEventListener(ResultEvent.RESULT, myResult);//添加访问成功后回调函数
remote.addEventListener(FaultEvent.FAULT,fault);//添加访问失败后回调函数
remote.sayHello(u.text);//执行 服务器类的 sayHell方法,
}
private function myResult(evt:ResultEvent):void{
Alert.show(evt.result.toString());
}
private function fault(evt:FaultEvent):void{
Alert.show(evt.fault.message);
}
]]-->
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<mx:RemoteObject id="say" destination="Test">
</mx:RemoteObject>
</fx:Declarations>
<s:Button x="451" y="251" label="按钮" click="button1_clickHandler(event)"/>
<s:TextInput x="290" y="250" id="u"/>
</s:Application>
以上为整合内容.
由于本人最近较忙..故没有多少时间!!
有时间给大家做一些 控件 以及 其它 与java后台高级交互文章..
that's all ! 董侨 QQ5596152
转载请说明出处.!!