Flex与Servlet交互

Flex 专栏收录该内容
1 篇文章 0 订阅
addTime.mxml:

<?xml version="1.0" encoding="utf-8"?>

<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%">



<!--加载页面时到该URL取值放入<mx:DataGrid>中-->

<mx:Model source="http://localhost:8080/TestServlet/GetTime" id="alltime">  

</mx:Model>



<mx:Script>

	<![CDATA[

		import mx.rpc.events.ResultEvent;

		import mx.controls.Alert;

		

		private function check():void{

			var start:String=startTime.text;

			var end:String=endTime.text;

			if(start==null || end==null || start=="" || end==""){

				Alert.show("请填写完整数据","信息提示");

			}else{

				doSumit();

			}

		}

		

		//点击按钮触发提交事件

		private function doSumit():void{

			submitButton.enabled=false;//按钮置为不可用

			

			//URLVariables:应用程序和服务器之间传输变量

			var params:URLVariables = new URLVariables();

			

			//传向的URL  此处的TestServlet/GetTime为我Tomcat下的工程TestServlet,及servlet映射GetTime

			submitTime.url="http://localhost:8080/TestServlet/GetTime";

			

			//传输方法,经测试默认为get

			submitTime.method="post";

			

			//params.startTime中的startTime是随意起的名称,后台jsp或servlet可用request.getParameter("startTime");来获取值

			//startTime.text中的startTime是下面的<mx:TextInput>组件的id,其text属性为获取其文本内容

			//<mx:TextInput>相当于<input type="text">

			

			params.startTime = startTime.text;

			params.endTime = endTime.text;

			

			submitTime.send(params);//发出发送指令

		}

		//HTTPService处理完提交数据后触发事件,有点类似于aJax的处理数据后对象状态为4时的执行处理

		private function sentEnd(res:ResultEvent):void{

			Alert.show("数据已提交","提示信息");//提示框

			submitButton.enabled=true;//按钮置为可用

			startTime.text="";

			endTime.text="";

		}

	]]>

</mx:Script>



<!--数据通过HTTPService传输-->

	<mx:HTTPService id="submitTime" result="sentEnd(event)"/>

	<mx:Panel width="100%" height="502" layout="absolute" horizontalCenter="5">

		<mx:HBox width="70%" height="8%" borderColor="#84CAFA" themeColor="#3AA7ED" borderStyle="outset" horizontalCenter="20">

			<mx:Label text="设置批次" width="10%" fontSize="14" color="#275C7B"/> 

			<mx:Label text="开始时间:" fontSize="11"/>

			<mx:TextInput id="startTime"/>

			<mx:Label text="结束时间:" fontSize="11"/>

			<mx:TextInput id="endTime"/>

			<mx:Button id="submitButton" label="确定" fontSize="11" click="check()"/>

		</mx:HBox>

		

		<!--绑定DataGrid的数据源dataProvider-->

		<mx:DataGrid dataProvider="{alltime.times}" horizontalCenter="4" verticalCenter="24" width="80%" height="80%" fontSize="11">

			<mx:columns>

				<mx:DataGridColumn headerText="序号" dataField="id"/>

				<mx:DataGridColumn headerText="批次" dataField="time"/>

				<mx:DataGridColumn headerText="操作" dataField="act"/>

				<mx:DataGridColumn headerText="当前批次" dataField="isnow"/>

			</mx:columns>

		</mx:DataGrid>

	</mx:Panel>

	

</mx:Module>
-------------------------------------------------------------------------
//Servlet :(url)GetTime



package servlet;



import java.io.IOException;

import java.io.PrintWriter;



import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



public class GetTime extends HttpServlet {



	/**

	 * Constructor of the object.

	 */

	public GetTime() {

		super();

	}



	/**

	 * Destruction of the servlet. <br>

	 */

	public void destroy() {

		super.destroy(); // Just puts "destroy" string in log

		// Put your code here

	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)

			throws ServletException, IOException {



		doPost(request,response);



	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)

			throws ServletException, IOException {

		response.setContentType("text/html");

		response.setCharacterEncoding("utf-8");

		PrintWriter out = response.getWriter();

		

		

		if(request.getParameter("startTime")!=null){

		//接收flex中参数,经不完全测试,只有该转码方式不为乱码

			String startTime = new String(request.getParameter("startTime").getBytes("ISO-8859-1"),"UTF-8");

			String endTime = new String(request.getParameter("endTime").getBytes("ISO-8859-1"),"UTF-8");

		

			System.out.println(startTime+"  ***  "+endTime);

		}else{

		/**初始加载flex中addTime.mxml时从本servlet中取出所要在<mx:DataGrid>中显示的数据

		 * 目前鄙人只知道能够从xml中获取数据,所以通过servlet做出xml供flex取数据

		 * 正式项目时此处可改为从数据库取数据

		**/

			out.println("<?xml version='1.0' encoding='utf-8' ?>");

			out.println("<alltime>");

				out.println("<times>");

					out.println("<id>1</id>");

					out.println("<time>2008-6-24</time>");

					out.println("<act>删除</act>");

					out.println("<isnow>是</isnow>");

				out.println("</times>");

				out.println("<times>");

					out.println("<id>2</id>");

					out.println("<time>2008-5-19</time>");

					out.println("<act>删除</act>");

					out.println("<isnow>否</isnow>");

				out.println("</times>");

				out.println("<times>");

					out.println("<id>3</id>");

					out.println("<time>2008-4-16</time>");

					out.println("<act>删除</act>");

					out.println("<isnow>否</isnow>");

				out.println("</times>");

			out.println("</alltime>");

			out.flush();

			out.close();

		}



	}



	/**

	 * Initialization of the servlet. <br>

	 *

	 * @throws ServletException if an error occurs

	 */

	public void init() throws ServletException {

		// Put your code here

	}



}
  • 0
    点赞
  • 5
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值