그래서 당신은 서버에 클라이언트 코드에서 AJAX 요청을 할 응답을받을 어떤 방법으로 그것을 처리 할 here 발견? 소켓이 필요 없습니다. 대신 Tapestry에 내장 된 AJAX 기능을 사용하십시오. 당신은 자바 스크립트를 통해 페이지 내 추가 콘텐츠를로드하는 경우
는, 기회는 당신이 모든 코드를 작성할 필요가 없습니다 것입니다. AJAX section from the Tapestry docs을 읽었으며 Zone의 정의와 작동 방식을 이해하십시오.
여기에 기본적인 예입니다. 틀 :
<div id="myZone" t:type="Zone" t:id="myZone">
... [Initial content, if any] ...
</div>
<a t:type="ActionLink" t:id="updateContent" t:zone="myZone">Update</a>
그리고 클래스 :
@Inject
private Zone myZone;
@Inject
private Request request;
@OnEvent(component = "updateContent")
Object updateContent() {
... [your code] ....
if (this.request.isXHR()) {
return this.myZone.getBody();
} else {
return this;
}
}
태피스트리 링크에 적절한 이벤트 리스너를 등록하고 적절한 장소에 업데이트 된 콘텐츠를 삽입과 같은 다른 모든 것을 할 것입니다. if (this.request.isXHR())
은 JavaScript가 활성화되지 않은 고객을 위해 degrade gracefully 페이지를 보장합니다.
JSON 객체를 반환하고 자신의 JavaScript 코드로 처리하는 것과 같이 완전히 다른 작업을 수행하려는 경우 these JSON classes 중 하나를 이벤트 처리기에서 반환 할 수 있습니다.
또한 클라이언트 측 코드를 직접 작성하려면 Tapestry와 함께 제공되는 내장형 크로스 브라우저 AJAX functionality of Prototype을 사용해야합니다.의견에 따라
편집 :
당신 때문에 same origin policy의 AJAX를 통해 다른 서버 (호스트 + 포트)에 액세스 할 수 없습니다. 그러나 Tapestry 앱을 통해 호출을 프록시 할 수 있습니다. 이것을 설명하기 위해 코드를 수정했습니다 (포트 2112에서 청취하는 것이 HTTP 서버라고 가정 할 경우 필요에 따라 변경).
@OnEvent(component = "updateContent")
Object updateContent() throws IOException {
final URL url = new URL("http://localhost:2112");
final HttpURLConnection con = url.openConnection();
final String content;
InputSteam input = null;
try {
input = con.getInputStream();
content = IOUtils.toString(input);
} finally {
IOUtils.closeQuietly(input);
}
return new StreamResponse() {
@Override
public String getContentType() {
return "text/javascript";
}
@Override
public InputStream getStream() throws IOException {
return new ByteArrayInputStream(content.getBytes("UTF-8"));
}
@Override
public void prepareResponse(Response response) {
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
}
}
}
달성하고자하는 것에 대해 더 자세히 설명 할 수 있습니까? – mdma