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>


读出需要信息后,将它拼出需要的JSON格式:

<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