html------3.javaWeb網頁下拉列表框調用數據庫數據

效果java

 

struts+action+jspjson

1建立實體:實現 get set方法api

 二、action數組

調用api 接口 處理json 數據app

 

 

api調用處理jsp

package com.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
/**
 * 接收url 並處理 json 數據
 * @author Administrator
 *
 */
public class UrlUtil {
    
    private static String LINKURL="http://localhost:8080/";
    
    public static String getUrl(String url)
    {
        String resData = null;
        StringBuffer s = new StringBuffer();
        BufferedReader bReader = null;
        try {
            
            URL urlWeb = new URL(LINKURL+url);
            URLConnection connection = urlWeb.openConnection();
            bReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            while (null != (resData = bReader.readLine())) {
                s.append(resData);
            }
            bReader.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(s);
        return s.toString();
    }
    
    public static void main(String[] args) {
        //測試請求的 json 數據
        //System.out.println(getUrl("rubOrder.action?wor_id=5&order_id=100"));
        String s=getUrl("rubOrderRoom.action?wor_id=3&is_system=0");
        try {
            //net.sf.json.jsonobject 沒有 new JSONObject(String)的構造方法
            JSONObject json = JSONObject.fromObject(s.toString());
            
            //success
            //Boolean data=(Boolean) json.get("success");
            
            String data=json.getString("data");
            System.out.println(data);
            JSONObject jsonData=JSONObject.fromObject(data);
            
            
            System.out.println(jsonData.get("data").toString());
            //String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" ;  // 一個未轉化的字符串
            JSONArray bjson = JSONArray.fromObject(jsonData.get("data").toString() ); // 首先把字符串轉成 JSONArray  對象
            if(bjson.size()>0){
                for(int i=0;i<bjson.size();i++){
                    JSONObject job = bjson.getJSONObject(i);  // 遍歷 jsonarray 數組,把每個對象轉成 json 對象
                    System.out.println(job.get("problem_description")+"****"+job.get("order_id")) ;  // 獲得 每一個對象中的屬性值
                }
            }
        } catch (JsonIOException e) {
            e.printStackTrace();
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
    }
}

 

struts.xml配置測試

 

 

jsp頁面數據列表顯示google

 

點擊按鈕提交數據url

 

 struts.xml配置spa

 

action處理數據

相關文章
相關標籤/搜索