用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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值