2011-11-16 43 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下拉列表中选择一个批次,我会根据我的批处理选择让我的表... :)

相关问题