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
-----------------------------java----------------------------------
web.xml代码
服务器配置文件
struts.xml代码(放在src目录下)
服务器配置文件:其中的action的name属性一定要和服务器请求的属性一致,其后面的class属性要与java中Action的实体类的位置对应
action代码
Action实体类:其中的属性名要与flex页面传来的参数名保持一致,并添加g&s方法。然后再在execute()方法里描述这个action具体需要做的事情。
最后的return返回值可以设成null,因为flex不会受struts的驱使跳转页面。
p.s. 如果不部署到eclipse上运行的话,_url填写完整的http协议路径
实现从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 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>
<?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 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>
<?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 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>
<?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的驱使跳转页面。
- 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;
- }
- }
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协议路径