杨庆荣的博客

工作之余接单:公司产品展示网站建设、电商网站建设、小程序、app开发。+86 13714715608

blaze ds 连接到mysql

点击 147449    创建时间 2012-10-22 18:31:56

转载请注意地址:https://wudimei.com/yangqingrong/blog/item/56.html 第一部份:服务器端

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">  
 <web-app>  

 <display-name>Spring and Flex</display-name>  
 <description>Spring and Flex</description>  
 <listener>  
   <listener-class>flex.messaging.HttpFlexSession</listener-class>  
 </listener>  
 <servlet>  
  <servlet-name>dispatcherServlet</servlet-name>  
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  <load-on-startup>1</load-on-startup>  
 </servlet>  


 <servlet>  
  <servlet-name>RDSDispatchServlet</servlet-name>  
  <display-name>RDSDispatchServlet</display-name>  
  <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>  
  <init-param>  
  <param-name>messageBrokerId</param-name>  
  <param-value>_messageBroker</param-value>  
  </init-param>  
  <init-param>  
  <param-name>useAppserverSecurity</param-name>  
  <param-value>false</param-value>  
  </init-param>  
  <load-on-startup>10</load-on-startup>  
 </servlet>  

 <servlet-mapping id="RDS_DISPATCH_MAPPING">  
  <servlet-name>RDSDispatchServlet</servlet-name>  
  <url-pattern>/CFIDE/main/ide.cfm</url-pattern>  
  </servlet-mapping>  

  <servlet-mapping>  
  <servlet-name>dispatcherServlet</servlet-name>  
  <url-pattern>/messagebroker/*</url-pattern>  
 </servlet-mapping>  

 </web-app>

dispatcherServlet-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xmlns:context="http://www.springframework.org/schema/context"  
 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  
 http://www.springframework.org/schema/context  
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

 <flex:message-broker>  
 <flex:remoting-service default-channels="my-amf"/>  
 </flex:message-broker>  

 <context:annotation-config />  
 <context:component-scan base-package="wudimei" />  

 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  <property name="url" value="jdbc:mysql://localhost:3306/test" />  
  <property name="username" value="root" />  
  <property name="password" value="123456" />  
 </bean>  

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  <property name="dataSource" ref="dataSource" />  
  <property name="packagesToScan" value="wudimei"/>  
  <property name="hibernateProperties">  
    <props>  
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
      <prop key="hibernate.hbm2ddl.auto">create</prop>  
    </props>  
  </property>  
 </bean>  

 </beans>

flex/service-config.xml

<?xml version="1.0" encoding="UTF-8"?>  
 <services-config>  

 <services/>  

 <security/>  

 <channels>  
  <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">  
    <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>  
  </channel-definition>  
 </channels>  

 <logging>  
 <target class="flex.messaging.log.ConsoleTarget" level="Info">  
  <properties>  
  <prefix>[BlazeDS] </prefix>  
  <includeDate>false</includeDate>  
  <includeTime>false</includeTime>  
  <includeLevel>false</includeLevel>  
  <includeCategory>false</includeCategory>  
  </properties>  
  <filters>  
    <pattern>Endpoint.*</pattern>  
    <pattern>Service.*</pattern>  
    <pattern>Configuration</pattern>  
  </filters>  
 </target>  
 </logging>  

 <system/>  

 </services-config>

flex-remoting.xml

 <?xml version="1.0" encoding="UTF-8"?>  
 <service id="remoting-service"  
 class="flex.messaging.services.RemotingService">  

  <adapters>  
    <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>  
  </adapters>  
  <!--  
  <default-channels>  
  <channel ref="my-amf"/>  
  </default-channels>  

  <destination id="helloService" channels="my-amf">  
  <properties>  
  <source>wudimei.Hello</source>  
  </properties>  
  </destination>  
  -->  
 </service>

以上是配置,基本是根据adobe教程来的,我改了上面的mysql的连接配置,把mysql-connector-java-5.1.15-bin.jar拷到WEB-INF/lib下。

实体类User.java

 package wudimei;  

 import javax.persistence.Entity;  
 import javax.persistence.GeneratedValue;  
 import javax.persistence.Id;  

 @Entity  
 public class User {  
  @Id  
  @GeneratedValue  
  public long id;  
  private String email;  
  private String password;  
  public long getId() {  
    return id;  
  }  
  public void setId(long id) {  
    this.id = id;  
  }  
  public String getEmail() {  
    return email;  
  }  
  public void setEmail(String email) {  
    this.email = email;  
  }  
  public String getPassword() {  
    return password;  
  }  
  public void setPassword(String password) {  
  this.password = password;  
  }  
 }

服务类Hello.java**

package wudimei;  

 import java.util.List;  

 import javax.servlet.http.HttpServletRequest;  

 import org.apache.commons.logging.Log;  
 import org.apache.commons.logging.LogFactory;  
 import org.hibernate.SessionFactory;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.flex.remoting.RemotingDestination;  
 import org.springframework.flex.remoting.RemotingInclude;  
 import org.springframework.orm.hibernate3.HibernateTemplate;  
 import org.springframework.stereotype.Service;  

 import flex.messaging.FlexContext;  

 @Service  
 @RemotingDestination  
 public class Hello {  
  private Log logger = LogFactory.getLog(this.getClass());  
  private HibernateTemplate template;  
  @Autowired  
  public void setSessionFactory(SessionFactory session)  
  {  
    this.template = new HibernateTemplate(session);  
  }  
  @RemotingInclude  
  public String say(String name)  
  {  
    HttpServletRequest request = FlexContext.getHttpRequest();  

    return "hello," +name + " encoding:" + request.getCharacterEncoding();  
  }  
  @RemotingInclude  
  public boolean login(String username,String password )  
  {  
    boolean isLogined = false;  
    HttpServletRequest request = FlexContext.getHttpRequest();  

    return isLogined;  

  }  
  @RemotingInclude  
  public void save(String email , String password )  
  {  
    User u = new User();  
    u.setEmail(email);  
    u.setPassword(password);  
    this.template.save(u);  
  }  
  @RemotingInclude  
  public List<User> getUsers()  
  {  
    return this.template.findByExample(new User());  
  }  
 }

然后在eclipse中选择File->export ,导出war到tomcat/webapps下

User.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" xmlns:services="services.*">  
  <s:layout>  
    <s:BasicLayout/>  
  </s:layout>  
  <fx:Script>  
  <![CDATA[  
    import mx.controls.Alert;  
    import mx.events.FlexEvent;  

    protected function button_clickHandler(event:MouseEvent):void  
    {  
      saveResult.token = hello.save(arg0TextInput.text,arg1TextInput.text);  
    }  


    protected function users_creationCompleteHandler(event:Event):void  
    {  
      getUsersResult.token = hello.getUsers();  
    }  

  ]]>  
  </fx:Script>  
  <fx:Declarations>  
    <services:Hello id="hello" fault="Alert.show(event.fault.faultString + '\
    ' + event.fault.faultDetail)" showBusyCursor="true">  
      <services:channelSet>  
        <s:ChannelSet>  
          <s:AMFChannel uri="http://localhost:8080/flex-server/messagebroker/amf" />  
        </s:ChannelSet>  
      </services:channelSet>  
    </services:Hello>  
    <s:CallResponder id="saveResult" result="users_creationCompleteHandler(event)"/>  
    <s:CallResponder id="getUsersResult"/>  
    <!-- 将非可视元素(例如服务、值对象)放在此处 -->  
  </fx:Declarations>  
  <mx:Form defaultButton="{button}" x="22" y="284">  
    <mx:FormItem label="Username">  
      <s:TextInput id="arg0TextInput"/>  
    </mx:FormItem>  
    <mx:FormItem label="Password">  
      <s:TextInput id="arg1TextInput"/>  
    </mx:FormItem>  
    <s:Button label="Save" id="button" click="button_clickHandler(event)"/>  
  </mx:Form>  
  <mx:DataGrid x="51" y="47" id="users" creationComplete="users_creationCompleteHandler(event)" dataProvider="{getUsersResult.lastResult}">  
  <mx:columns>  
    <mx:DataGridColumn headerText="id" dataField="id"/>  
    <mx:DataGridColumn headerText="email" dataField="email"/>  
    <mx:DataGridColumn headerText="password" dataField="password"/>  
  </mx:columns>  
  </mx:DataGrid>  
 </s:Application>

上一篇: 命令行下编译flex连接到spring blazeds
下一篇: 好不容易录了一首歌