flex向 struts2(action)传输数据

http://jusesgod.javaeye.com/blog/508967

flex向struts2(action)传输数据

关键字: flex struts2 通信

目标:
  实现从flex页面向后台的服务框架(struts2)传输参数。

实现:
  在flex页面中添加<HTTPService>标签,在事件触发向服务器发送请求时将数据加载在request中向服务器发送,其中的url属性填写服务器的请求路径(.../*.action一定要填相对路径,前面不要加"/"),这样的话struts2的action可以接收到相同名字的属性。

环境:
  1.flex编辑使用 Flex Builder3.0.2.214193
  2.struts2使用 MyEclipse6.5
  3.服务器 Tomcat 5.5

------------------------------flex-------------------------------

flex页面:

其中:_url.action为用户定义的请求,该页面向struts2发送两个参数:userName,pswWord

Xml代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#f6f6f6" backgroundGradientColors="[#f6f6f6, #AA1220]" fontSize="12">  
  3.     <mx:Label text="用户名:" x="23" y="61" id="txt"/>  
  4.     <mx:TextInput id="userName" x="77" y="59"/>  
  5.       
  6.     <mx:Label text="密码:" x="35" y="91"/>  
  7.     <mx:TextInput id="pswWord" x="77" y="89"/>  
  8.       
  9.     <mx:Button label="提交" click='my_HS.send()' x="185" y="121"/>  
  10.       
  11.     <mx:HTTPService id="my_HS" url="_url.action" method="post" >  
  12.         <mx:request>  
  13.             <userName>{userName.text}</userName>  
  14.             <pswWord>{pswWord.text}</pswWord>  
  15.         </mx:request>  
  16.     </mx:HTTPService>  
  17. </mx:Application>  
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#f6f6f6" backgroundGradientColors="[#f6f6f6, #AA1220]" fontSize="12">
    <mx:Label text="用户名:" x="23" y="61" id="txt"/>
    <mx:TextInput id="userName" x="77" y="59"/>
    
    <mx:Label text="密码:" x="35" y="91"/>
    <mx:TextInput id="pswWord" x="77" y="89"/>
    
    <mx:Button label="提交" click='my_HS.send()' x="185" y="121"/>
    
    <mx:HTTPService id="my_HS" url="_url.action" method="post" >
    	<mx:request>
        	<userName>{userName.text}</userName>
        	<pswWord>{pswWord.text}</pswWord>
    	</mx:request>
    </mx:HTTPService>
</mx:Application>


-----------------------------java----------------------------------

web.xml代码
服务器配置文件
Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.    <filter>  
  8.         <filter-name>struts2</filter-name>  
  9.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  10.     </filter>  
  11.   
  12.     <filter-mapping>  
  13.         <filter-name>struts2</filter-name>  
  14.         <url-pattern>/*</url-pattern>  
  15.     </filter-mapping>  
  16.   
  17. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>


struts.xml代码(放在src目录下)
服务器配置文件:其中的action的name属性一定要和服务器请求的属性一致,其后面的class属性要与java中Action的实体类的位置对应
Xml代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7. <constant name="struts.action.extension" value="action,do,webwork" />  
  8. <package name="simple"  extends="struts-default">  
  9.     <action name="_url" class="test.action.GetUserName">  
  10.         <result name="success">index.jsp</result>  
  11.     </action>  
  12. </package>  
  13. </struts>  
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.action.extension" value="action,do,webwork" />
<package name="simple"  extends="struts-default">
    <action name="_url" class="test.action.GetUserName">
        <result name="success">index.jsp</result>
    </action>
</package>
</struts>


action代码
Action实体类:其中的属性名要与flex页面传来的参数名保持一致,并添加g&s方法。然后再在execute()方法里描述这个action具体需要做的事情。
最后的return返回值可以设成null,因为flex不会受struts的驱使跳转页面。
Java代码
  1. package test.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class GetUserName extends ActionSupport{  
  6.     public String userName;  
  7.     public String pswWord;  
  8.       
  9.     public String getPswWord() {  
  10.         return pswWord;  
  11.     }  
  12.   
  13.     public void setPswWord(String pswWord) {  
  14.         this.pswWord = pswWord;  
  15.     }  
  16.   
  17.     public String getUserName() {  
  18.         return userName;  
  19.     }  
  20.   
  21.     public void setUserName(String userName) {  
  22.         this.userName = userName;  
  23.     }  
  24.   
  25.     public String execute(){  
  26.         System.out.println("there it is");  
  27.         String s = getText(userName);  
  28.         System.out.println(s+":"+pswWord);  
  29.           
  30.         return SUCCESS;  
  31.     }  
  32. }  
package test.action;

import com.opensymphony.xwork2.ActionSupport;

public class GetUserName extends ActionSupport{
	public String userName;
	public String pswWord;
	
	public String getPswWord() {
		return pswWord;
	}

	public void setPswWord(String pswWord) {
		this.pswWord = pswWord;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String execute(){
		System.out.println("there it is");
        String s = getText(userName);
        System.out.println(s+":"+pswWord);
        
		return SUCCESS;
	}
}


p.s. 如果不部署到eclipse上运行的话,_url填写完整的http协议路径
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值