I am using Tapestry5.3. I have a dynamic web project (prject name: test) with test.tml and test.java.

Test.tml

<!DOCTYPE html>
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
      xmlns:p="tapestry:parameter">

<head>
</head>
<body>
${stringValue}
</body>
</html>

Test.java

public String getGenerateJson() {
return "welcome";
}

I have one more dynamic web project (project name: test2) with test2.tml and test2.java. Both projects are running into my tomcat server.

From test2.java, I make a http connetion to test.java for getting olny test.java return value (welcome) in test2.java.

Test2.java

final GetData data = new HttpGetData();
final String str = data.getContent("http://10.0.1.62:8080/Test/test");
System.out.println("String: " + str); 

Output:

String:

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><link type="text/css" rel="stylesheet" href="/test/assets/1b4371c8cdb7af3/core/default.css"/><link type="text/css" rel="stylesheet" href="/test/assets/1b4371c8cdb7af3/jquery/themes/ui-lightness/jquery-ui-1.8.15.custom.css"/><meta content="Apache Tapestry Framework (version 5.3.1)" name="generator"/></head><body>
welcome
</body></html>

But I got output like that.

Is chance to get only welcome from test.java. Please give me your valuable suggestion or idea to achieve like that.

有帮助吗?

解决方案

The simplest solution is to return a StreamResponse from your page's onActivate event. You won't need a template (.tml) to do this.

Object onActivate(EventContext context) {
   return new TextStreamResponse("text/plain", "welcome");
}

Since plain text isn't a great solution, you'll probably want to use JSON instead.

Object onActivate(EventContext context) {
   JSONObject json = new JSONObject("greeting", "welcome");
   return new TextStreamResponse("application/json", json.toCompactString());
}

For a simple way of creating restful webservices in tapestry, you might want to consider tapestry-resteasy

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top