用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>
發佈了41 篇原創文章 · 獲贊 0 · 訪問量 1248
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章