EXT+JSON请求服务
来源:互联网 发布:自定义全局透明软件 编辑:程序博客网 时间:2024/04/27 23:00
需求:从文本中读取IP地址,子网掩码,网关
<span style="font-size:18px;"><strong>BOOTPROTO="static"DEVICE="eth0"ONBOOT="yes"IPADDR="192.168.1.51"NETMASK="255.255.255.0"GATEWAY="192.168.1.1"DNS1="192.168.1.254"DNS2="192.168.2.254"</strong></span>
然后将它显示在WEB页面:
1)通过I/O流读出所需要的信息。
<span style="font-size:18px;"><strong>/** * 得到服务IP地址 */ public void getServerIp(){FileInputStream is=null;InputStreamReader isr=null;String ipaddr=null;String netmask=null;String gateway=null;File file = new File(FILEURL); try { is=new FileInputStream(file); isr= new InputStreamReader(is); BufferedReader in= new BufferedReader(isr); String line=null; while((line=in.readLine())!=null) //System.out.println(line); if(line.contains("IPADDR")) { String[] array = line.split("[=]"); ipaddr = array[1].replace("\"",""); //System.out.println(array[1]+" "); } else if(line.contains("NETMASK")) { String[] array = line.split("[=]"); netmask = array[1].replace("\"",""); } else if(line.contains("GATEWAY")) { String[] array = line.split("[=]"); gateway = array[1].replace("\"",""); } // this.actionWrite("{{},{},{}GATEWAY:111]}") } catch (IOException e) { e.printStackTrace(); }finally{ try { if(isr!=null){isr.close(); } if(is!=null){ is.close(); }} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} } String str = "{\"ipaddr\":"+ipaddr+",\"netmask\":"+netmask+",\"gateway\":"+gateway+"}"; try {JSONObject obj = new JSONObject(str);this.actionWrite(obj.toString());} catch (JSONException e) {logger.error(e);} }</strong></span>
上面这个方法我们怎么理解呢?
其实我们在Web页面上请求发送到一个Action,处理的方法就是上面这个方法
<span style="font-size:18px;"><strong>String[] array = line.split("[=]");ipaddr = array[1].replace("\"","");String[] array = line.split("[=]");netmask = array[1].replace("\"","");String[] array = line.split("[=]");gateway = array[1].replace("\"","");</strong></span>
<span style="font-size:18px;"><strong>String str = "{\"ipaddr\":"+ipaddr+",\"netmask\":"+netmask+",\"gateway\":"+gateway+"}";JSONObject obj = new JSONObject(str);this.actionWrite(obj.toString());</strong></span>
<span style="font-size:18px;"><strong>/** * 服务端响应请求结果集 * * @param result */public void actionWrite(String result) {if(result == null){result = "";}HttpServletResponse resp = ServletActionContext.getResponse();resp.setContentType("text/json;charset=UTF-8");resp.setHeader("Cache-Control", "no-cache");PrintWriter pw = null;try {pw = resp.getWriter();pw.write(result);} catch (IOException e) {throw new EVoucherException("获取http写入流异常" + e.getMessage());} finally {if (pw != null) {pw.close();}}}</strong></span>
2)接下来我们看看前台页面时怎么请求的?(JSP+EXT+JSON)
在页面处,让页面已加载就开始触发方法:onload();
<span style="font-size:18px;"><strong><script type="text/javascript">Ext.onReady(function() {Ext.QuickTips.init();Ext.create('Ext.container.Viewport',{id : 'ipSetFrame',layout : 'border',items : [tbarPanel,centerPanel]});});</script> </head> <body onload="init();"> </body></html></strong></span>
然后我们看看init()方法怎么实现的。
(这个方法是自己非常不清楚的地方,涉及到Ext的ajax请求,需要理解体会)
<span style="font-size:18px;"><strong>function init(){Ext.Ajax.request({ url : '/Portal/user/serviceManage_getServerIp.action', method : 'POST', type:'json',callback : function (options,success,response){ if(success){ var text = Ext.JSON.decode(response.responseText); //获取action里的rs json Ext.getCmp("ipaddr").setValue(text.ipaddr); Ext.getCmp("netmask").setValue(text.netmask); Ext.getCmp("gateway").setValue(text.gateway); } }});</strong></span>
这个Ext.Ajax.request的请求可以从官网上看看API,一定要理解。
下面这行代码是最重要的,取得你从action拼的JSON字符串
var text = Ext.JSON.decode(response.responseText); //获取action里的rs json
然后将值设置到TextField中OK。
0 0
- EXT+JSON请求服务
- ext json
- PHP请求服务curl以及json的解析
- Jersery Restful接口服务接收JSON对象的参数请求
- Ext同步请求实现
- Ext.Ajax请求
- EXT请求总结
- ext 提交请求方式
- ext 请求等待框
- ext JSON网格(JSON Grid)
- struts2 ext json数据
- struts2 ext json数据
- Ext json序列化
- json-ajax-ext
- JSON解析请求服务器端的服务时,需要提供一个请求的URL,注意点。
- ext异步请求一个aspx/ashx/web service取得数据,服务端返回一个json.然后客户端接收并显示.
- Ext Ext.Decode = Ext.util.JSON.Decode 方法介绍
- Ext.Ajax.request异步请求
- [2778]小明的花费预算 (二分查找)SDUT
- jQuery选择器总结
- 屏住呼吸在2010年即将推出的手机
- 子类获取父类iframe中的值
- Orcfile文件格式解析(1)
- EXT+JSON请求服务
- ROW_NUMBER() OVER函数的基本用法
- ZooKeeper 节点
- calloc
- DNS负载均衡与负载均衡器两种方案的选择
- Hacker Tools
- 如何使用ZStack——获取RSSI和LQI
- 用JDBC编程的运行时错误及其解决大全
- solr-4.4与php通信,结果高亮显示