2014-12-06 2 views
1

데이터베이스에서 JSON 배열로 데이터를 생성하는 활동 파일로 Java 웹 애플리케이션을 작성합니다. 자,이 JSON 배열을 jQuery에 전달하고 싶습니다. 어떻게 가능합니까?JSON 배열을 Struts 2 액션 클래스에서 jQuery로 전달하는 방법은 무엇입니까?

참고하시기 바랍니다. Struts 2, Spring 및 Hibernate 프레임 워크를 적용했습니다. 이 응용 프로그램에 PHP를 사용하지 않습니다.

업데이트 :

이 내 스트럿츠 2 액션 방법 :

public static String jsonData = null; 

public String generateJson() throws Exception 
{ 
    JSONObject json = null; 
    JSONArray jsonArray = new JSONArray(); 

    this.records = this.recordManager.getAllRecords(); 

    for (RecordEntity record : records) 
    { 
     json = new JSONObject(); 

     json.put("id", record.getId()); 
     json.put("firstname", record.getFirstName()); 
     json.put("lastname", record.getLastName()); 
     json.put("due", record.getDue()); 
     json.put("email", record.getEmail()); 
     json.put("website", record.getWebsite()); 
     jsonArray.put(json); 
    } 

    System.out.println(jsonArray.toString()); 

    jsonData = jsonArray.toString(); // jsonData will be passed to jQuery 
    return SUCCESS; 
} 

그리고 이것은 내 jQuery를합니다. 또한, 시작하는

this를 참조

$('#record').bootstrapTable({ 
    method : 'get', 
    data: jQuery.parseJSON(VALUE_OF_jsonData_HERE), 
    cache : ... 
    ... 
    ... 
}); 
+1

서블릿이 JSON으로 인코딩 된 데이터를 응답 출력에 인쇄한다면, 정규 Ajax 요청을 통해 가져 오기만하면됩니다. ['$ .getJSON'] (http://api.jquery.com/jQuery.getJSON/) –

+0

어떻게? 미안하지만, 나는 서블릿과 Struts 2에 익숙하지 않다는 것을 언급하지 않았다. – Dylan

+0

은 json을 페이지에서 자바 스크립트 변수로 직접 출력 할 수있다. 접근 방법은 사용 방법에 따라 다소 다릅니다. – charlietfl

답변

0

가 당신을 도울 것입니다 아약스사용 :이 구조 그래서 나는 BootstrapTable를 사용하고, 나는 jsonData의 값이 jQuery를 전달해야 할 Ajax을 먼저 읽으십시오.

+0

예, 이미 사용했습니다. 질문을 수정하고 코드를 추가했습니다. :) – Dylan

관련 문제