2011-11-16 51 views
0

這是以下2個鏈接的延續....由於我無法張貼這些任何評論...發送參數從JSP Struts2的action類的方法

1. Populating a table based on values chosen from a drop down in struts2 application

2. Struts2 parameter to javascript

我也有相同的場景,我需要從下拉列表中打印基於所選值的表格,在我的谷歌搜索中我得到了這個頁面,並且我在這裏使用了建議。但是,當我在我的下拉列表中選擇一個值,我得到的表格打印和頁面仍然留在同一頁面沒有任何更新和下面是我的代碼...幫助我在這...

javascript

<script type="text/javascript"> 

function showAllocationStatusJavaScript(){ 

    var batchURL1="<s:property value="#batchURL"/>"; 
    $.ajax({ 
     url:batchURL1, 
     type: 'get', 
     beforeSend: function(){ 
      $("#loading").show(); 
      alert("parsed"); 
     }, 
     success: function(result){ 
      if(result!=''){ 
     $('myTableWrapper').html(result); 
      } else { 
       alert(result); 
      } 
     }, 
     }); 
     } 

</script> 

jsp的身體

<s:select label="Select Batch" headerKey="-1" headerValue="Select a Batch..."list="%{#session.Batchs}" Value="batch" name="batch" onchange="showAllocationStatusJavaScript()" id="batch"/> 

URL標記

<s:url action="doShowAllocationStatus" var="batchURL"><param value="%{batch}"/></s:url> 

此表以打印我的列表

<div id="myTableWrapper"> 
<table align="center" border="2"> 
    <tr> 
    <th>TAN</th> 
    <th>Curator</th> 
    <th>Curator Status</th> 
    <th>QC</th> 
    <th>QC Status</th> 
    </tr> 
    <s:iterator value="allocationList" > 
    <tr> 
    <td><s:property value="tan"/></td> 
    <td><s:property value="curator"/></td> 
    <td><s:property value="curator_status"/></td> 
    <td><s:property value="qc"/></td> 
    <td><s:property value="qc_status"/></td> 
    </tr> 
    </s:iterator> 
</table> 
</div> 

struts.xml的

<action name="doShowAllocationStatus" class="controller.AllocateTAN" method="showAllocationStatus" > 
    <result name="success" type="dispatcher" >Allocation.jsp</result> 

AllocateTAN動作類

//Fields that hold data... 
    private List<BatchInfo> allocationList =new ArrayList<BatchInfo>(); 
    private String batch; 
    private List<String> batchs = new ArrayList<String>(); 
    private String TAN; 
    private List<String> Tans = new ArrayList<String>(); 
    private String user; 
    private List<String> users = new ArrayList<String>(); 

//and all getters and setters.... 

..... 

//variable used to access DataBase... 
    CationDAO dao1 = new CationDAO() ; 



//flow 1.: making all details available for the allocate TAN page...when page page is loaded 1st time 


    public String AllocatingTANpageDetails() throws SQLException{ 
     Map<String, Object>session=ActionContext.getContext().getSession(); 
     this.batchs=dao1.Batch_List(); 
     session.put("Batchs", batchs); 
     //Tans=dao1.Tan_list(getBatch()); 
     this.users=dao1.Users_List(); 
     session.put("users", users); 
     return SUCCESS; 
    } 

    private void showTANlist(String Batch1) throws SQLException{ 
     Map<String, Object>session=ActionContext.getContext().getSession(); 
     Tans=dao1.Tan_list(Batch1); 
     session.put("Tans", Tans); 

    } 
//flow 2.: showing Allocation Status in Table form...in same page 


    public String showAllocationStatus() throws SQLException { 
     Map<String, Object>session=ActionContext.getContext().getSession(); 
     //setBatch(batch_value); 
     session.put("Batch",batch); 

     showTANlist(batch); 
     System.out.println("Processing Allocation List... "); 
     this.allocationList=(List<BatchInfo>)dao1.status(batch); 
     System.out.println("Finished..."); 
     return SUCCESS; 
     } 

//execute method form allocating a TAN for a user...  
    public String execute(){ 

     return SUCCESS; 
} 
+0

你的Action類更新allocationList? –

+0

另外顯示我們的行動類代碼 –

+0

是的,我正在更新! –

回答

1

你jQuery選擇不正確:

$('myTableWrapper').html(result); 

DOM元素ID由一個主導#字符指定;這應該是:

$('#myTableWrapper').html(result); 

你也有外來的尾隨逗號在$.ajax參數,這將打破某些瀏覽器,應始終被淘汰。

function showAllocationStatusJavaScript() { 
    $.ajax({ 
     url: "<s:property value='#batchURL'/>", 
     type: 'get', 
     beforeSend: function() { 
      $("#loading").show(); 
     }, 
     success: function(result) { 
      if (result != '') { 
       $('#myTableWrapper').html(result); 
      } 
      $("#loading").hide(); 
     } 
    }); 
} 
+0

好,讓我明天試試吧。我需要說我是新來的stuts2和ajax。我現在在這裏添加了我的動作班,我希望我的動作班裏的每一件事都是正確的,如果需要更正,請告訴我。 –

+0

我仍然沒有打印表格,我認爲我在使用url標記時出錯... 這是將參數發送到具有我的方法」showAllocationStatus「的操作類的url標記。我應該在哪裏寫這個URL標記在我的jsp 另外我有一個懷疑,你曾說過, $('myTableWrapper')。html(result); //錯誤 和 $('#myTableWrapper')。HTML(結果); //是正確的 並給了我更正的代碼爲我的ajax代碼。 在這段代碼中你沒有使用「#」? ÿ? –

+0

@SathishKumarkk Typo;固定。您可以查看呈現的HTML以確保所有設置都正確。 –

0

最後我找到了答案...... 有一些修正我在jQuery的Ajax代碼和struts.xml的做......他們在這裏...

jQuery的Ajax代碼

<head> 
<script type="javascript" src="jquery-1.7.js"></script> 
<head> 

<script type="text/javascript"> 
$(document).ready(function() 
     { 
      $('#batchID').change(function(event) { 
       var batch=$('#batchID').val(); 
       alert("inside change fn."+batch); 
       $.ajax({ 
        url  : "doShowAllocationStatus.action", 
        data : "batch="+batch, 
        success : function(html) { 
         alert("success"); 
           $("#table").html(html); 
           }, 
        error : function(html) { 
           alert("error"); 
           } 
         }); 
      }); 
     });  
</script> 

現在這裏我寫了上面的問題顯示我的表的代碼,在一個新的jsp頁面(AllocationStatusTable.jsp)和包括它...

<div id=table> 
<s:include value="AllocationStatusTable.jsp" /> 
</div> 

在struts.xml中後來我重新寫了行動的結構,

<action name="doShowAllocationStatus" class="controller.AllocateTAN" method="showAllocationStatus" > 
    <result name="input" >Allocation.jsp</result> 
    <result name="error" >Allocation.jsp</result> 
    <result>AllocationStatusTable.jsp</result> 
    </action> 

因此該操作的結果指向新的jsp頁面(AllocationStatusTable.jsp)。

最後,當我在brop下拉列表中選擇一個批次,我會根據我的批處理選擇讓我的表... :)