My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

joi, 17 decembrie 2015

Using OmniFaces ResourceInclude and OutputFormat to display in a JSF page the decorated output from a Servlet/JSP

OmniFaces provides a component capable to  catch the output from a JSP or Servlet resource and render it as output to the JSF writer. For example, let's suppose that we have the following simple Servlet:

@WebServlet("/TextServlet")
public class TextServlet extends HttpServlet {

 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  out.println("<h3>I'm a servlet that returns text ...</h3>");
 }

 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  processRequest(request, response);
 }

 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  processRequest(request, response);
 }
}

The OmniFaces component is named ResourceInclude and can be used via <o:resourceInclude/> tag as in the below example (output to the JSF writer the text output from TextServlet):

<o:resourceInclude path="/TextServlet" />


More examples and details about ResourceInclude are available in Mastering OmniFaces book.

Now, let's suppose that we want to modify (decorate) the Servlet output before we display it in page. A nice trick will consist in using another OmniFaces component named, OutputFormat. This component extends the standard <h:outputFormat/> with support for capturing the output and exposing it into the request scope by the variable name as specified by the var attribute. For example, we can capture the TextServlet output and expose it via <o:outputFormat/> as below:

<o:outputFormat var="_text">
 <o:resourceInclude path="/TextServlet" />
</o:outputFormat>

This time the TextServlet output is not displayed on screen. This output was stored in the request scope and it is available via _text anywhere in page. This means that we can pass this output to an action method which decorates it accordingly:

@Named
@RequestScoped
public class TextBean {

 public String decorate(String text) {
  return "***" + text + "***";
 }
}

<h:outputText value="#{textBean.decorate(_text)}" escape="false"/>


More examples and details about OutputFormat are available in Mastering OmniFaces book.

The complete application is available here.

Special thanks to Arjan Tijms for providing this tip.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

Visitors Starting 4 September 2015

Locations of Site Visitors