2016-09-02 18 views
0

データベースからデータを読み込み、ローカルホスト上のtomcatサーバーを使用して表示するJSPページがあります。 jspをJsonオブジェクトに変換するように求められます。それ、どうやったら出来るの?jspページをJsonオブジェクトに変換する方法

私はjspの完全な初心者です。参考のためにjspページがあります。すべてのJSPの

<%@ page import="x.VoltDAOImpl" %> 
<%@ page import="java.util.*" %> 
<%@ page import = "java.text.DecimalFormat" %> 

<html> 
    <head> 
    <title>Data Platform - Real Time Monitor </title> 
    </head> 

    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
    <link href="/common/default.css" rel="stylesheet" type="text/css" /> 

    <%! 
       VoltDAOImpl voltDao = new VoltDAOImpl(); 
       DecimalFormat df1 = new DecimalFormat("##,##,##,##,##,##,###"); 
       DecimalFormat df2 = new DecimalFormat("#,###,##0.00"); 
       DecimalFormat df3 = new DecimalFormat("#,###,##0.0000"); 

    %> 


<script src="sorttable.js"></script> 

    <body> 

    <center> 

      <div style="font-style:bold;font-size:30;color:black"> Data Platform - Monitor </div> 

    <% 

     try{ 


    %> 


    <% 



    String client_acronym = request.getParameter("client_acronym"); 
    String algo = request.getParameter("algo"); 
    String listid = request.getParameter("listid"); 
    String sort_by = request.getParameter("sort_by"); 

    %> 

    <% 

    Map<String , List<HashMap<String,String>>> returnList= null; 
    List<HashMap<String,String>> orderDetailsList1= null; 
    List<HashMap<String,String>> totalSlpSummaryList= null; 
    List<HashMap<String,String>> totalSlpSummaryBySideList= null; 


    if(("".equals(client_acronym)||client_acronym==null) ) 
    { 
    } 
    else if ( (!"".equals(client_acronym) && client_acronym != null) && (!"".equals(algo) && algo != null) && (!"".equals(listid) && listid != null) ) 
    { 
     long start= System.currentTimeMillis(); 
     long start1= System.currentTimeMillis(); 
     returnList= voltDao.getOrderDetails("PEAKM",algo,listid,voltDao.client,sort_by,1000); 
     long end= System.currentTimeMillis();   
     long time_taken=(end-start)/1000; 
     orderDetailsList1 = returnList.get("order_details"); 
     totalSlpSummaryList = returnList.get("total_slp_summary"); 
     totalSlpSummaryBySideList = returnList.get("total_slp_summary_by_side"); 
     System.out.println("Took "+time_taken+"secs and got "+orderDetailsList1.size()+" rows" ); 
     } 
     else if ((!"".equals(client_acronym) && client_acronym != null) && (!"".equals(algo) && algo != null) ) 
     { 

     long start= System.currentTimeMillis();  
      long start1= System.currentTimeMillis();  
      returnList= voltDao.getOrderDetails("PEAKM",algo,"",voltDao.client,sort_by,1000); 
     long end= System.currentTimeMillis();  
      long time_taken=(end-start)/1000; 
      orderDetailsList1 = returnList.get("order_details"); 
      totalSlpSummaryList = returnList.get("total_slp_summary"); 
      totalSlpSummaryBySideList = returnList.get("total_slp_summary_by_side"); 
      System.out.println("Took "+time_taken+"secs and got "+orderDetailsList1.size()+" rows" ); 
     } 

     else if ( (!"".equals(client_acronym) && client_acronym != null) ) 
     { 

     long start= System.currentTimeMillis();  
      long start1= System.currentTimeMillis();  
      returnList= voltDao.getOrderDetails("PEAKM",algo,"",voltDao.client,sort_by,1000); 
     long end= System.currentTimeMillis();  
      long time_taken=(end-start)/1000; 
      orderDetailsList1 = returnList.get("order_details"); 
      totalSlpSummaryList = returnList.get("total_slp_summary"); 
      totalSlpSummaryBySideList = returnList.get("total_slp_summary_by_side"); 
      System.out.println("Took "+time_taken+"secs and got "+orderDetailsList1.size()+" rows" ); 
     } 
     else 
     { 
     } 

JSONObject jsonObject = new JSONObject(orderDetailsList1); 
JSONObject jsonObject1 = new JSONObject(totalSlpSummaryList); 
JSONObject jsonObject2 = new JSONObject(totalSlpSummaryBySideList); 

     HashMap<String,String> totalSlpSummaryMap = totalSlpSummaryList.get(0); 
     HashMap<String,String> totalSlpSummaryBySideMap = totalSlpSummaryBySideList.get(0); 

     if(!("".equals(client_acronym)||client_acronym==null) ) 
     { 
      /* out.println( "<table class=\"sortable\" border=\"1\" > "); 
      out.println(" <tr BGCOLOR = \"#99CCFF\"> <th><p><b>Side </b><p></th> "); 
      out.println("<th><p><b>Order Qty</b><p></th> "); 


     String total_ord_qty_buy  = totalSlpSummaryBySideMap.get("total_ord_qty_buy"); 
     String total_ord_qty_sell  = totalSlpSummaryBySideMap.get("total_ord_qty_sell"); 
     String total_ord_qty  = totalSlpSummaryMap.get("total_ord_qty"); 

     try { total_ord_qty_buy   = df1.format(Double.parseDouble(total_ord_qty_buy)) ;     } catch(Exception e){System.out.println(" could not convert "+total_ord_qty_buy  ); } 
     try { total_ord_qty_sell  = df1.format(Double.parseDouble(total_ord_qty_sell)) ;     } catch(Exception e){System.out.println(" could not convert "+total_ord_qty_sell  ); } 
     try { total_ord_qty   = df1.format(Double.parseDouble(total_ord_qty)) ;  } catch(Exception e){System.out.println(" could not convert "+total_ord_qty  ); } 



      out.println("<tr><td><p>Buy</p></td>"); 
      out.println("<td align=\"right\"><p>"+ total_ord_qty_buy +"</p></td>"); 

      out.println("<tr><td><p>Sell </p></td>"); 
      out.println("<td align=\"right\"><p>"+ total_ord_qty_sell +"</p></td>"); 

      out.println("<tr><td><b><p>Total </p></b></td>"); 
      out.println("<td align=\"right\"><b><p>"+ total_ord_qty +"</p></b></td>"); 

      out.println("</table><BR> "); */ 


     int i=0; 
      out.println( "<table class=\"sortable\" border=\"1\" > "); 
     out.println(" <tr BGCOLOR = \"#99CCFF\"> "); 
     out.println("<th><p><b>CLIENT</b><p></th> "); 
     out.println("<th><p><b>ORD Qty</b><p></th> "); 
     out.println("</tr>"); 

     for(HashMap<String,String> map : orderDetailsList1) 
     { 

     if(i%2==0) 
     out.println("<tr>"); 
     else 
     out.println("<tr class=\"alternateRow\">"); 

     String CLIENT_ACRONYM = (map.get("CLIENT_ACRONYM") != null) ?  map.get("CLIENT_ACRONYM"): "---"; 
     String ORDER_QTY = (!("0E-12".equals(map.get("ORDER_QTY")))) ?  map.get("ORDER_QTY"): "---"; 

     out.println("<td><p>"+ CLIENT_ACRONYM     +"</p></td>"); 
     out.println("<td align=\"right\"><p>"+ df1.format(Double.parseDouble(ORDER_QTY))      +"</p></td>"); 

      out.println("</tr>"); 
      i++; 

     } 

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


     } 

} catch(Exception e){System.out.println(" error in detail" ); } 

    %> 

    </body> 

</html> 
+0

あなたはJSONまたはちょうどenerate JSONにPOJOを変換するために使用することができますlibにいくつかのthridパーティがあります。この[MKYONG](https://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/)のチュートリアルを参照するか、GSON、jacksonなどを使用してください。 – striker

+0

*ページ*をJsonに変換しません。オブジェクトをJsonとして出力します。 – developerwjk

+0

@developerwjkこれを行う方法を教えてもらえますか? – Shek

答えて

1

まずあなたがJSONオブジェクトのコンストラクタの下に使用することができますので、むしろあなたがJSON

に出力を変換する方法を尋ねてください、私はorderDetailsList1は、文字列のキーとStringオブジェクトであるあなたを見ることができますJSONに変換しませんそれをjsonに変換します。

JSONObject jsonObject = new JSONObject(orderDetailsList1);

ノート:JSONのjarファイルをダウンロードしhttp://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm

+0

JSONオブジェクトをどのように返すのですか?私はこのような何かをする必要があります:response.setContentType( "application/json"); ??また、すべてのout.printlnステートメントとhtmlを取り除く必要がありますか? – Shek

+0

これらのステートメントの上にステートメントを使用すると、HashMapは、 ハッシュマップ totalSlpSummaryBySideMap = totalSlpSummaryBySideList.get(0); エラーが発生しました:jspファイルの103行目でエラーが発生しました:/demo.jsp JSONObjectを型に解決できません。私はlibに瓶を入れました。 – Shek

+0

あなたはこのエラーを受け取っているあなたのJSPパッケージにそのlibパッケージをインポートするのを忘れました –

関連する問題