insert text-resource into jsf page
there's a RESTful service which is Producing te开发者_运维技巧xt/plain
and I want to add that content into my JSF page. Is that possible? Is there a tag for that?
If you're using JSP as view technology, you can use the JSTL <c:import>
for this. It's as simple as
<c:import url="http://example.com/service" />
It however doesn't run in sync with JSF as you'd expect from the coding. Also, this is not supported in Facelets. So if you want to render/display it conditionally (and thus run in sync with JSF) or are using Facelets, then you'll have to do this in the managed bean. To start, you can use java.net.URL
and consorts to get content from an URL.
InputStream input = new URL("http://example.com/service").openStream();
Reader reader = new InputStreamReader(input, "UTF-8"); // You may want to verify charset based on response headers.
StringBuilder builder = new StringBuilder();
for (int data; (data = reader.read()) > -1;) {
builder.append((char) data);
}
String result = builder.toString();
Put this in the constructor or action method, depending on the conditions it needs to execute. You can however also wrap this all in flavor of a standalone custom/composite component or EL function.
Related questions:
- How to use java.net.URLConnection to fire and handle HTTP requests?
精彩评论