1.创建JavaWeb工程FlexSpring。
2.加入Spring2.5,选择core和web两项,确定。
3.加入BlazeDS,将blazeds.war用解压缩软件解压,得到WEB-INF和META-INF两文件夹,将这两个文件夹复制到WebRoot下替换工程下原来的文件。完成的工程结构如下:
4.将工程发布到服务器,并启动服务器(添加Flex工程时验证服务需要用到);
5.加入Flex工程,右击工程->Flex Project Natrue -> Add Flex Project Natrue ,弹出如下图的窗口,在弹出窗口中选择Application server type为J2EE,然后下一步。
6.填写flex工程属性配置:去掉复选框的选择在相应的输入框中添加相应信:1)第一项为服务器下工程的目录2)访问工程的URl路径3)工程的名字,填写完毕之后点击Validate Configuration 按钮进行验证,正确后点击finish。
7.完成后工程会报个错误,这是因为Flex工程的html模板文件没有,解决办法是打开Myeclipse的problems窗口,在Error中找到
”Cannot create HTML wrapper. Right-click here to recreate folder html “ 然后右键-》recreate Html template,Ok搞定。
8.接下来让我们规划一下工程结构,使Flex源文件与Java源文件分开管理,src.java文件夹用来存放Java源文件,src.flex文件夹用来存放Flex原文件。完成的工程结构如下:
9.将下面的类加入你的src目录下:
import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import flex.messaging.FactoryInstance;
import flex.messaging.FlexContext;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;
public class SessionFactory4Flex implements FlexFactory {
private static final String SOURCE = "source";
private FactoryInstance instance;
public void initialize(String id, ConfigMap configMap) {
instance = new FactoryInstance(this,id,configMap);
}
public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
return instance;
}
public Object lookup(FactoryInstance inst) {
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(FlexContext.getServletContext());
String beanName = instance.getSource();
try {
return context.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;
}
}
}
10.将下面的代码加入到web.xml中。
11.将下面的代码添加到web-inf下的services-config.xml中。
12.接下来我问写一个简单的Flex前端进行测试。
13.编写后台Java代码。
public class Server {
public String sayHello(String userName,String password){
if(userName.equals("litter") && password.equals("123456"))
return "你好"+userName+"先生(女士)!";
return "对不起,你的用户名或密码错误!";
}
}
14.配置remoting-config.xml文件。将下面的代码remoting-config.xml加入文件中。
15.加入<bean id="server" class="com.company.Server" />到applicationContext.xml中。
16.运行。运行截图。如下: