2016-10-06 86 views
0

我有一個jsp頁面,該頁面超鏈接到另一個jsp頁面。如果我們從下面所示的圖像點擊「更新首選項」,然後打開第二圖象:如何在jsp中顯示先前選定的值

image1

2nd image

下面是第二圖像中顯示的JSP代碼:

<body> 
    <center>  
      <div style="font-style:bold;font-size:30;color:black"> Data Platform - Monitor </div> 
    </center> 
    <BR><BR> 
     <%! 
       VoltDAOImpl voltDao = new VoltDAOImpl(); 

     %> 




     <form action="dp_monitor.jsp" method="get" > 



     <table width="350" border="0"> 

      <tr valign="Left"> 
      <td><b>Client Acronym: </b> </td><td>  <b> <input type="text" name="client_acronym" value="PEAKM"></b></td> 
      </tr> 

      <tr valign="Left"> 
      <td><b>Sort By :</b></td> 
      <td> 
      <select name="sort_by"> 
      <option selected="true" value="notional">notional</option> 
     <option value="arr_mid_slp">arr mid slippage</option> 
     <option value="order_qty">order qty</option> 
     <option value="liq_consmption">liq consmption</option> 
     <option value="arr_last_slp">arr last slippage</option> 
     <option value="ivwap_slp">ivwap slppage</option> 
     <option value="exec_qty">exec qty</option> 
     <option value="leaves_qty">leaves qty</option> 
     <option value="limit_px">limit px</option> 
     <option value="avg_px">avg px</option> 
     <option value="last_px">last px</option> 
     <option value="last_qty">last qty</option> 
     <option value="transact_time">transact time</option> 
     <option value="arr_mid_px">arr mid px</option> 
     <option value="ivwap_volume">ivwap volume</option> 
     <option value="ivap_px">ivap px</option> 
     <option value="arr_last_px">arr last px</option> 
     <option value="end_last_px">end last px</option> 
     </select> 

      </td> 
      </tr> 


      <tr valign="Left"> 
      <td><b>Algo: </b> </td> 
      <td> 
      <select name="algo"> 
      <option selected="true" value="">ALL</option> 

       <% 
       try{ 

        List<String> algo_list= voltDao.getDistinctAlgos("PEAKM",voltDao.client); 
       for(String algo : algo_list) 
       { 
       out.println("<option value=\""+algo+"\">"+algo+"</option>"); 
       } 


      } catch(Exception e){System.out.println(" error in getting algos" ); } 
      %> 

     </select> 
      </tr> 

      <tr valign="Left"> 
      <td><b>ListId: </b> </td> 
      <td><select name="listid"> 
      <option selected="true" value="">ALL</option> 

       <% 

       try { 
        List<String> list_id__list= voltDao.getDistinctListID("PEAKM",voltDao.client); 
       for(String list_id : list_id__list) 
       { 
       out.println("<option value=\""+list_id+"\">"+list_id+"</option>"); 
       } 

       } catch(Exception e){System.out.println(" error in getting listids" ); } 
      %> 
     </select> 
     </td> 
      </tr> 

      <tr valign="Left"> 
      <td><b>Refresh : </b> </td> 
      <td> 
      <select name="refresh"> 
      <option value="1">1 sec</option> 
      <option value="3">3 secs</option> 
      <option value="5">5 secs</option> 
      <option value="10">10 secs</option> 
      <option value="15">15 secs</option> 
      <option value="30">30 secs</option> 
      <option value="45">45 secs</option> 
      <option value="60">1 min</option> 
      <option value="120">2 min</option> 
      <option value="180">3 min</option> 
      <option value="240">4 min</option> 
      <option value="300" selected>5 min</option> 
     </select> 
     </td> </tr> 
      <tr><td></td> </tr> 
      <tr><td></td> </tr> 
      <tr><td></td> </tr> 
       <tr> <td><button type="submit">Update</button></td> 
       <td><button type="reset">Clear</button></td> 
      </tr> 
     </table> 




     </form> 






    </body> 

因此,每次我選擇一個首選項,比如"Refresh「從下拉菜單中選擇5 secs,然後點擊Update,所以它會返回到第一個jsp(第一個圖像sh自己在上面)。現在當我再次點擊「Update Preferences」時,它會顯示默認選擇值5 mins而不是5 secs。如何獲得以前選擇的偏好,以便每次點擊Update Preferences時顯示?

更新: 我將此添加到我的第二個JSP:

<% 
    String refreshValue = request.getParameter("refresh"); 

    if(refreshValue==null){ 
     refreshValue = "60"; 
    } 

%> 

      <td><b>Refresh : </b> </td> 
      <td> 
      <select name="refresh"> 
<option value="1" <% if(refreshValue.equals("1")) { %> selected <% } %>>1 sec</option> 
<option value="3" <% if(refreshValue.equals("3")) { %> selected <% } %>>3 secs</option> 
<option value="5" <% if(refreshValue.equals("5")) { %> selected <% } %>>5 secs</option> 
<option value="10" <% if(refreshValue.equals("10")) { %> selected <% } %>>10 secs</option> 
<option value="15" <% if(refreshValue.equals("15")) { %> selected <% } %>>15 secs</option> 
<option value="30" <% if(refreshValue.equals("30")) { %> selected <% } %>>30 secs</option> 
<option value="45" <% if(refreshValue.equals("45")) { %> selected <% } %>>45 secs</option> 
<option value="60" <% if(refreshValue.equals("60")) { %> selected <% } %>>1 min</option> 
<option value="120" <% if(refreshValue.equals("120")) { %> selected <% } %>>2 min</option> 
<option value="180" <% if(refreshValue.equals("180")) { %> selected <% } %>>3 min</option> 
<option value="240" <% if(refreshValue.equals("240")) { %> selected <% } %>>4 min</option> 
<option value="300" <% if(refreshValue.equals("300")) { %> selected <% } %>>5 min</option> 
</select> 

這給我的第一個JSP(dp_monitor):

<input type="hidden" name="refresh" value="<%=request.getParameter("refresh")%>"> 
<%String refresh = request.getParameter("refresh"); 
if(("".equals(refresh)||refresh==null) ) 
     refresh="4000"; 



    // Set refresh, autoload time 
    response.setIntHeader("Refresh", Integer.parseInt(refresh)); 
    // Get current time 
    Calendar calendar = new GregorianCalendar(); 
    String am_pm; 
    int hour = calendar.get(Calendar.HOUR); 
    int minute = calendar.get(Calendar.MINUTE); 
    int second = calendar.get(Calendar.SECOND); 
    if(calendar.get(Calendar.AM_PM) == 0) 
    am_pm = "AM"; 
    else 
    am_pm = "PM"; 

    if(hour == 0) 
    hour = 12; 

    String CT = hour+":"+ minute +":"+ second +" "+ am_pm; 
    out.println("Last Refresh Time: " + CT + "\n");%> 

但是,它時,我選擇「更新某個值偏好設置「,然後點擊」更新「,然後再回來,它只需要refreshValue作爲null,因此只顯示1 min被選中。該怎麼辦?

+0

,你在哪裏存儲此偏好?因爲現在,選擇'<選項值=「300」選項> 5分鐘'選擇'5分鐘'。您需要獲取保存的值(文件,數據庫,緩存)並使用一些JSTL將'selected'添加到相應的 – AxelH

回答

0

我相信dp_monitor.jsp是你的第一個jsp。

您需要在jsps之間讀取,保留和傳輸數據。

要讀你已經使用request.getParameter("refresh")

保留數據,您可以使用一個隱藏的元素dp_monitor.jsp頁面

<input type="hidden" name="refresh" value="<%=request.getParameter("refresh")%>"> 

形式元素中傳輸數據表單中的數據提交會做到這一點。

在你已經爲refresh參數第一次檢查,如果該值不提供的第二個JSP,那麼你已經分配的缺省值

<% 
    String refreshValue = request.getParameter("refresh"); 

    if(refreshValue==null){ 
     refreshValue = "300; 
    } 

%> 

然後使用refreshValue可以選擇所需的選項元素。

另一種方法是將值存儲在session但我認爲第一種方法更適合您的需求

您可以使用下面的代碼片段select值。可以使用多種方式,這是其中之一

<select name="refresh"> 
<option value="1" <% if(refreshValue.equals("1")) { %> selected <% } %>>1 sec</option> 
<option value="3" <% if(refreshValue.equals("3")) { %> selected <% } %>>3 secs</option> 
<option value="5" <% if(refreshValue.equals("5")) { %> selected <% } %>>5 secs</option> 
<option value="10" <% if(refreshValue.equals("10")) { %> selected <% } %>>10 secs</option> 
<option value="15" <% if(refreshValue.equals("15")) { %> selected <% } %>>15 secs</option> 
<option value="30" <% if(refreshValue.equals("30")) { %> selected <% } %>>30 secs</option> 
<option value="45" <% if(refreshValue.equals("45")) { %> selected <% } %>>45 secs</option> 
<option value="60" <% if(refreshValue.equals("60")) { %> selected <% } %>>1 min</option> 
<option value="120" <% if(refreshValue.equals("120")) { %> selected <% } %>>2 min</option> 
<option value="180" <% if(refreshValue.equals("180")) { %> selected <% } %>>3 min</option> 
<option value="240" <% if(refreshValue.equals("240")) { %> selected <% } %>>4 min</option> 
<option value="300" <% if(refreshValue.equals("300")) { %> selected <% } %>>5 min</option> 
</select> 

另一種方法是使用設置JSTL的選項,你可以參考這個link

+0

非常感謝。如何使用refreshValue選擇所需的選項元素? – Angad

+0

我添加了更新到我的問題,告訴你做什麼之後按照你的建議。請看一看。 – Angad

+0

@dp_monitor.jsp中的@Angad在form標籤內添加了」>「該行動指向第二個jsp – vjy

相關問題