2014-08-30 88 views
-1

我必須要顯示的公司名稱列表,並在jsp中形成自己的聯繫一樣,如何使用顯示器來顯示數據列表:表

-company1 
    contact1 
    contact2 
-company2 
    contact1 
    contact2 
.............. 

我不知道該怎麼做任何建議或鏈接的例子? PS:在DB我公司和聯繫人(通過FK關係)的表

回答

0

如果你正在使用Hibernate,您可以使用您的DAO爲了一旦你的信息獲得的信息(即公司的List)你可與排在首位的AJAX和JSON做到這一點,你必須下載JSON插件Struts2的 貼這在你的Maven POM文件或donwload jar文件

<dependency> 
    <groupId>org.apache.struts</groupId> 
    <artifactId>struts2-json-plugin</artifactId> 
    <version>2.3.16.3</version> 
</dependency> 

也爲了讓您的JSON對象,你必須序列所有分類

public class Company implements Serializable {// code getters; setters;} 

,並在動作類

@ParentPackage("json-default") 
@Action(value = "getCompanies", results = { 
@Result(type = "json", name = "success")}) 
public class FetchCompanies extends ActionSupport { 
    public List<Company> listOfCompanies = new ArrayList<Company>(); 

    @Override 
    public String execute() { 
     CompanyDAO dao = new CompanyDAO(); 
     listOfCompanies = dao.query("FROM Company"); 
     return SUCCESS; 
    } 

    //Getter Setter 
} 

,然後創建一個.js文件與AJAX調用來填充表

$(document).ready(function() { 
    $.ajax({ 
     url: "getCompanies.action", 
     type: "POST", 
     dataType: "json", 
     success: function(data) { 
      jQuery.each(data.listOfCompanies, function(i, val) { 
       $('#yourBody').append("<tr><td>" + val.attribute1OfYourCompanyClass + "</td></tr>"); 
      }); 
     } 
    }); 
}); 

,這是你的HTML可能看起來怎麼樣

 <div> 
      <table> 
       <thead> 
        <tr> 
         <th>Attribute1</th> 
        </tr> 
       </thead> 
       <tbody id="yourBody"> 
        //In this section ajax call will insert your information 
       </tbody> 
      </table> 
     </div>