`
可爱の小猪
  • 浏览: 104866 次
  • 性别: Icon_minigender_1
  • 来自: 南充
社区版块
存档分类
最新评论

用Flex+Spring+Hibernate写一个登录

阅读更多
1下载支持文件flex-spring.zip

新建FlexLCDS工程File -> new -> Flex Project 这里不细说这个。请看http://nic.iteye.com/blog/247604

前端是flex.中间层使用spring接着hibernate,spring+hibernate的集成方法和j2ee的项目中方法相同


修改WEB-INF\web.xml ,加入下面代码





Xml代码
<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>



注册sping factory,

修改WEB-INF\flex配置


<factory id="springFactory" class="cn.org.coral.core.flex.factory.FlexSpringFactory" />





Class属性填写第一步中考入项目SpringFactory类的路径

3 注册bean到remoting-config.xml

Xml代码
<destination id="teacherDao">    
      <properties>    
              <factory>springFactory</factory>      
              <source>TeacherDAO</source>    
                
      </properties>    
</destination> 

<destination id="teacherDao"> 
      <properties> 
              <factory>springFactory</factory>   
              <source>TeacherDAO</source> 
             
      </properties> 
</destination>



  编写SpringFactory.java




Java代码
public class FlexSpringFactory implements FlexFactory {  
 
    public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {  
         SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);     
            instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));     
            return instance;  
    }  
    public Object lookup(FactoryInstance inst) {  
         SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;     
            return factoryInstance.lookup();     
    }  
    public void initialize(String arg0, ConfigMap arg1) {  
      
    }  
 
}  
 
public class SpringFactoryInstance extends FactoryInstance     
{     
    public  SpringFactoryInstance(FlexSpringFactory factory, String id, ConfigMap properties)     
    {     
        super(factory, id, properties);     
    }     
    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();     
            throw e;     
        }     
        catch (BeansException bexc)     
        {     
            ServiceException e = new ServiceException();     
            throw e;     
        }      
    }    

public class FlexSpringFactory implements FlexFactory {

public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);  
        instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));  
        return instance;
}
public Object lookup(FactoryInstance inst) {
SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;  
        return factoryInstance.lookup();  
}
public void initialize(String arg0, ConfigMap arg1) {

}

}

public class SpringFactoryInstance extends FactoryInstance  
{  
public  SpringFactoryInstance(FlexSpringFactory factory, String id, ConfigMap properties)  
    {  
        super(factory, id, properties);  
    }  
    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();  
            throw e;  
        }  
        catch (BeansException bexc)  
        {  
            ServiceException e = new ServiceException();  
            throw e;  
        }   
    }   

login.mxml





Xml代码
<?xml version="1.0" encoding="utf-8"?> 
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="326" height="162" title="登录" horizontalAlign="center" verticalAlign="middle"> 
    <mx:RemoteObject id="loginDao" destination="teacherDao"/>   
    <mx:Script> 
        <![CDATA[ 
            import As.bean.Teacher; 
            import mx.rpc.events.FaultEvent; 
            import mx.managers.PopUpManager; 
            import mx.rpc.events.ResultEvent; 
            import mx.controls.Alert;            
             
            public var userId:Label; 
            public var userName:Label; 
            public var sex:Label; 
            public var birth:Label; 
            public var department:Label; 
            public var profession:Label; 
            public var mobile:Label; 
         
            public var teacher:Teacher;          
             
            private function callRO(str:String,psw:String):void{ 
                 
            var t:Teacher=new Teacher(); 
            t.loginname=str; 
            t.loginpass=psw; 
                 
                loginDao.Login(t); 
    loginDao.addEventListener(ResultEvent.RESULT,getROResult); 
    loginDao.addEventListener(FaultEvent.FAULT,getError); 
            } 
            private function getError(e:FaultEvent):void{ 
            Alert.show(e.message.toString()); 
            } 
            private function getROResult(e:ResultEvent) :void { 
                             
                if(e.result.loginname=="error"){ 
                 
                tip.text="No such user!! "; 
                }else 
                { 
                    teacher=e.result as Teacher; 
                     
                    sex.text=e.result.sex; 
                userId.text=e.result.id; 
                userName.text=e.result.name; 
                birth.text=e.result.birth; 
                department.text=e.result.department; 
                profession.text=e.result.profession; 
                mobile.text=e.result.mobile; 
                 
                     
                proccessLogin(); 
                } 
                 
            } 

        private function proccessLogin():void{ 
             
             
             
            PopUpManager.removePopUp(this); 
             
            } 
             
        ]]> 
    </mx:Script> 
    <mx:Label x="34" y="33" text="用户"/> 
    <mx:TextInput x="77" y="31" id="userNameTxt"/> 
    <mx:Label x="34" y="61" text="密码"/> 
    <mx:TextInput x="77" y="59" displayAsPassword="true" id="psw"/> 
    <mx:Button x="77" y="90" label="登录" click="callRO(userNameTxt.text,psw.text);"/> 
    <mx:Label x="163" y="94" id="tip" color="red"/>     
</mx:TitleWindow> 

<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="326" height="162" title="登录" horizontalAlign="center" verticalAlign="middle">
<mx:RemoteObject id="loginDao" destination="teacherDao"/>
<mx:Script>
<![CDATA[
import As.bean.Teacher;
import mx.rpc.events.FaultEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;

public var userId:Label;
public var userName:Label;
public var sex:Label;
public var birth:Label;
public var department:Label;
public var profession:Label;
public var mobile:Label;

public var teacher:Teacher;

private function callRO(str:String,psw:String):void{

var t:Teacher=new Teacher();
t.loginname=str;
t.loginpass=psw;

loginDao.Login(t);
loginDao.addEventListener(ResultEvent.RESULT,getROResult);
loginDao.addEventListener(FaultEvent.FAULT,getError);
}
private function getError(e:FaultEvent):void{
Alert.show(e.message.toString());
}
private function getROResult(e:ResultEvent) :void {

if(e.result.loginname=="error"){

tip.text="No such user!! ";
}else
{
teacher=e.result as Teacher;

sex.text=e.result.sex;
userId.text=e.result.id;
userName.text=e.result.name;
birth.text=e.result.birth;
department.text=e.result.department;
profession.text=e.result.profession;
mobile.text=e.result.mobile;


proccessLogin();
}

}

private function proccessLogin():void{



PopUpManager.removePopUp(this);

}

]]>
</mx:Script>
<mx:Label x="34" y="33" text="用户"/>
<mx:TextInput x="77" y="31" id="userNameTxt"/>
<mx:Label x="34" y="61" text="密码"/>
<mx:TextInput x="77" y="59" displayAsPassword="true" id="psw"/>
<mx:Button x="77" y="90" label="登录" click="callRO(userNameTxt.text,psw.text);"/>
<mx:Label x="163" y="94" id="tip" color="red"/>
</mx:TitleWindow>


applicationcontext.xml



Xml代码
      <bean id="TeacherDAO" class="com.source.bean.TeacherDAO"> 
        <property name="sessionFactory"> 
            <ref bean="sessionFactory" /> 
        </property> 
</bean> 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics