现在流行的ssh怎么和flex整合呢? 上个文章已经可以合并flex和javaweb了。我们现在来合并ssh
Flex和Spring的整合
1.先把spring支持的包拷贝到lib下
2.web.xml注册spring
3.增加SpringFactory.java
4、在services-config.xml中注册SpringFacotry,配置如下:
当进行到这步 就完成了对spring的配置 你再启动tomcat 会看到spring已经加载到了工程中
启动了spring 只要tomcat日志不报错 说明spring添加完成
4.配置数据源 在
applicationContext.xml中增加datasource的配置
5.、增加业务层代码,
bean:
dao
imp
6、在applicationContext.xml中配置bean
7、在remoting-config.xml中配置向Flex客户端公开的bean
、接下来就是Flex客户端的代码了
你主程序中代码 页面显示的
注意一点:
[img]http://dl.iteye.com/upload/picture/pic/53982/08b50398-b49d-3ebb-b8b0-6b2f7ad7a730.bmp[/img]
注意:-services "C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Flex_SSH\WEB-INF\flex\services-config.xml" -locale en_US
这个的配置 是tomcat下的工程的位置 如果这个错误页面就找不到数据源了
这样ssh和flex的整合就完成了
谢谢观赏
Flex和Spring的整合
1.先把spring支持的包拷贝到lib下
2.web.xml注册spring
<!-- Spring configuration file (Not needed if you don't use Spring) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Spring ContextLoaderListener (Not needed if you don't use Spring) -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3.增加SpringFactory.java
package com.samples.factories;
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;
public class SpringFactory implements FlexFactory
{
private static final String SOURCE = "source";
public void initialize(String id, ConfigMap configMap) {}
public FactoryInstance createFactoryInstance(String id, ConfigMap properties)
{
SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);
instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
System.out.println("SpringSpringSpring" + instance.toString());
return 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、在services-config.xml中注册SpringFacotry,配置如下:
<factories>
<factory id="spring" class="com.samples.factories.SpringFactory"/>
</factories>
当进行到这步 就完成了对spring的配置 你再启动tomcat 会看到spring已经加载到了工程中
启动了spring 只要tomcat日志不报错 说明spring添加完成
4.配置数据源 在
applicationContext.xml中增加datasource的配置
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/shop?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="sa" />
</bean>
5.、增加业务层代码,
bean:
package bean;
public class Usersinfor {
private int id;
private String name ;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
dao
package dao;
import java.util.Collection;
import org.springframework.dao.DataAccessException;
public interface UserDao {
public Collection findAll() throws DataAccessException ;
}
imp
package daoImp;
import java.util.Collection;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import dao.UserDao;
public class UserDaoImp extends HibernateDaoSupport implements UserDao{
public Collection findAll() throws DataAccessException {
// TODO Auto-generated method stub
List list = this.getHibernateTemplate().find("from Usersinfor as a");
return list;
}
}
6、在applicationContext.xml中配置bean
<bean id="UserDAOBean" class="daoImp.UserDaoImp">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
7、在remoting-config.xml中配置向Flex客户端公开的bean
<destination id="userService">
<properties>
<factory>spring</factory>
<source>UserDAOBean</source>
</properties>
</destination>
、接下来就是Flex客户端的代码了
你主程序中代码 页面显示的
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns="*" >
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
private function findAllFaultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString, "Error");
}
]]>
</mx:Script>
<mx:RemoteObject id="srv" destination="userService">
<mx:method name="findAll" fault="findAllFaultHandler(event)"/>
</mx:RemoteObject>
<mx:HDividedBox width="100%" height="100%">
<mx:Panel title="Inventory Management" width="100%" height="100%">
<mx:DataGrid id="list" width="100%" height="100%" dataProvider="{srv.findAll.lastResult}">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="Name"/>
<mx:DataGridColumn dataField="pwd" headerText="password"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
<!--<ProductForm product="{Product(list.selectedItem)}"/>-->
</mx:HDividedBox>
</mx:Application>
注意一点:
[img]http://dl.iteye.com/upload/picture/pic/53982/08b50398-b49d-3ebb-b8b0-6b2f7ad7a730.bmp[/img]
注意:-services "C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Flex_SSH\WEB-INF\flex\services-config.xml" -locale en_US
这个的配置 是tomcat下的工程的位置 如果这个错误页面就找不到数据源了
这样ssh和flex的整合就完成了
谢谢观赏