sitelink1 http://javacan.tistory.com/160 
sitelink2  
sitelink3 http://1 
extra_vars4 ko 
extra_vars5  
extra_vars6 sitelink1 
DWR을 이용해서 웹 어플리케이션을 개발하다 보면 성능 상의 이유로 DWR이 객체를 변환해서 생성하는 JSON 코드를 서버 코드에서 직접 생성하고 싶을 때가 있다. 이 경우 다음과 같은 절차에 따라 작업을 진행하면 된다.

1. web.xml 파일의 DwrServlet에 publishContainerAs 초기화 파라미터 값 설정
 

<servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  <init-param>
     <param-name>publishContainerAs</param-name>
     <param-value>dwrContainer</param-value>
  </init-param>
</servlet>


DwrServlet은 publishContainerAs 초기화 파라미터에 설정된 값이 있으면 ServletContext.setAttribute() 메서드를 이용해서 DWR Container를 ServletContext에 보관한다. 이때 publishContainerAs 초기화 파라미터의 값을 속성 이름으로 사용한다.

2. DWR이 제공하는 클래스를 이용해서 객체를 JSON으로 변환한다.
 

Container dwrContainer = (Container)getServletContext().getAttribute("dwrContainer");
ConverterManager converterManager = (ConverterManager)dwrContainer.getBean(
                                                                    ConverterManager.class.getName());
ScriptBuffer sb = new ScriptBuffer("dwrObject=");
SomeObject object = ...; // JSON으로 변환할 객체
sb.appendData(object); // 
String json = null;
try {
 json = ScriptBufferUtil.createOutput(sb, converterManager);
 json = StringEscapeUtils.escapeJavaScript(json);
} catch (MarshallException e) {
 throw new RuntimeException(e);
}
request.setAttribute("JSON_OBJECT", json);


위 코드에서 StringEscapeUtils 클래스는 Jakarta Commons Lang 프로젝트에 포함된 클래스이다.

ScriptBufferUtil.createOutput() 메서드는 ScriptBuffer에 설정한 객체로부터 자바 스크립트 코드를 생성해 주는데, 이때 생성된 자바 스크립트 코드는 서버 측의 객체를 자바 스크립트의 JSON 형식으로 변환해주는 코드가 된다.

3. 자바 스크립트의 eval()을 이용해서 자바 스크립트 객체로 변환

이제 남은 작업은 eval()을 이용해서 DWR 모듈이 생성한 자바 스크립트 코드를 실행해서 그 결과를 객체로 저장하면 된다.
 

var someObj = (function () { return eval("${JSON_OBJECT}"); })();

번호 제목 글쓴이 날짜 조회 수
81 마우스 드래그(drag)시 iframe 위에서 컨트롤 잃는 현상과 해결 방안 황제낙엽 2017.12.12 18479
80 매우 간단한 AJAX 예제 - prototype.js 이용 (JSP) 황제낙엽 2007.08.24 7497
79 [re] XML+JS 연동 다중셀렉트박스 (2) - [AJAX] <font color="brown">(MS Explorer 전용)</brown> 황제낙엽 2006.02.22 1559
78 Redux: React 앱의 효율적인 데이터 교류 file 황제낙엽 2020.05.19 740
77 json을 이용한 로그인 구현 file 황제낙엽 2011.03.25 601
76 How Prototype extends the DOM (Prototype으로 DOM을 확장하는 법) 황제낙엽 2011.03.24 444
75 $.ajax() 공략 file 황제낙엽 2011.03.27 415
74 Dojo로 HTML 위젯 개발하기 - Dojo HTML 위젯 황제낙엽 2009.03.18 363
73 Building Your Own Widget Library with YUI 황제낙엽 2009.04.16 321
72 15+ jQuery Popup Modal Dialog Plugins and Tutorials 황제낙엽 2011.03.30 280
71 XML+JS 연동 다중셀렉트박스 (1) - [AJAX] <font color="brown">(MS Explorer 전용)</brown> 황제낙엽 2005.12.02 241
70 Jasmine 관련 황제낙엽 2020.01.13 240
69 다섯 가지의 Ajax 우수 사례 황제낙엽 2011.04.07 219
68 Spring에서 DWR Annotation 사용하기 file 황제낙엽 2009.10.28 151
67 [펌] 6. DOJO AND JSON file 황제낙엽 2009.03.11 137
66 Ajax 관련 사이트 정리 황제낙엽 2006.04.20 132
65 웹 개발 패러다임의 전환 - Flex와 Ajax의 동거 황제낙엽 2006.12.21 125
64 XML+JS 연동 다중셀렉트박스 (1) - [AJAX] <font color="brown">(MS Explorer 전용)</brown> 황제낙엽 2005.12.02 125
63 DWR 2.0 and Spring 2.x - The DWR namespace handler 황제낙엽 2009.05.16 124
62 관심 사이트 황제낙엽 2009.03.12 116