I am writing a custom Facelets TagHandler
that can be used in Facelets view .xhtml
files. Inside the apply
method, I am trying to access faceletContext.getFacesContext().getResponseWriter()
, but it returns null
.
Please note that I am not using managed beans. I am using plain servlets, but utilizing Facelets templates.
public class MyTag extends TagHandler {
public MyTag(TagConfig config) {
super(config);
}
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
// ctx.getFacesContext().getResponseWriter() return Null here
}
}
Please note that all configurations for the custom tag have been set up properly, and I have confirmed that the apply
method is being executed. However, getResponseWriter()
still returns null
.
UPDATE: The reason I wanted to access the getResponseWriter()
is that I need to write some HTML code to the response output. I also tried using the PrintWriter
of the current response inside the apply method, but it did not work.
Any help? Thanks!
I am writing a custom Facelets TagHandler
that can be used in Facelets view .xhtml
files. Inside the apply
method, I am trying to access faceletContext.getFacesContext().getResponseWriter()
, but it returns null
.
Please note that I am not using managed beans. I am using plain servlets, but utilizing Facelets templates.
public class MyTag extends TagHandler {
public MyTag(TagConfig config) {
super(config);
}
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
// ctx.getFacesContext().getResponseWriter() return Null here
}
}
Please note that all configurations for the custom tag have been set up properly, and I have confirmed that the apply
method is being executed. However, getResponseWriter()
still returns null
.
UPDATE: The reason I wanted to access the getResponseWriter()
is that I need to write some HTML code to the response output. I also tried using the PrintWriter
of the current response inside the apply method, but it did not work.
Any help? Thanks!
Share Improve this question edited Mar 18 at 13:31 LHA asked Mar 18 at 3:58 LHALHA 9,6778 gold badges55 silver badges91 bronze badges2 Answers
Reset to default 3Tag handlers build the view. They don't render the view. Use a Renderer instead, usually in combination with an UI component. Or simply use a tag file (with JSTL), especially as you ultimately don't seem to want to use UI components.
If you tell more about the concrete functional requirement (i.e. what exactly are you ultimately trying to achieve), then we may be able to give a more specific answer/direction.
See also:
- What's the view build time?
- When to use <ui:include>, tag files, composite components and/or custom components?
Update: it appears you wish to compute a chunk of HTML with Java and render it unescaped. In that case, the simplest approach is creating a @RequestScoped
bean and preparing the HTML in @PostConstruct
and then using a <h:outputText escape="false">
to render it.
@Named
@RequestScoped
public class Bean {
private String html;
@PostConstruct
public void init() {
html = computeHtmlSomehow();
}
public String getHtml() {
return html;
}
}
<ui:composition
xmlns:ui="jakarta.faces.facelets"
xmlns:h="jakarta.faces.html">
...
<h:outputText value="#{bean.html}" escape="false" />
...
</ui:composition>
The logic is complex because it relies on the underlying Servlet-based MVC framework developed by my company
Alternatively, you can also manually construct the bean and just let that servlet set it as a request attribute before forwarding to the Facelets page as instructed in your previous question How to set up a Facelets template for a Jakarta Servlet web application (not a Jakarta Faces application)
request.setAttribute("bean", bean);
See also:
- Component to inject and interpret String with HTML code into JSF page
I found out the solution is to not use: ctx.getFacesContext().getResponseWriter().
public class MyTag extends TagHandler {
public MyTag(TagConfig config) {
super(config);
}
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
// Prepare for html code
// This can be complete html code of div, label, input, table, etc.
String htmlCode = ...
// HtmlOutputText
HtmlOutputText ui = new HtmlOutputText();
ui.setTransient(true); // Stateless
ui.setEscape(false); // Will render raw html into the response.
// The value of this ui is the html code generated above
ui.setValue(htmlCode);
// Add ui into the parent
parent.getChildren().add(ui);
}
}