编写自定义 HtmlResponseWriter JSF

发布于 2024-09-07 20:18:09 字数 414 浏览 4 评论 0原文

对于许多现在不重要的内部问题,我们有一个 Servlet 过滤器,它可以更改 application/xhtml+xml 的所有结果并重写为 text/html;charset=UTF-8 code> 因此,即使使用facelets,它也可以在 IE 6.0 上正常工作。

我的问题是关于 HtmlResponseWriter,它是负责渲染的组件。是否可以扩展它并重写它的方法,以便我们实现过滤器的预期效果?

  • 内容类型将始终输出为text/html;
  • 编码将始终为 UTF-8;
  • 脚本标记将被包装在 <; 中。 ! -- <[[CDATA]]> -->

提前致谢。

For many internal problems that doesn't count now, We have a Servlet filter that changes all outcome that's application/xhtml+xml and rewrite to text/html;charset=UTF-8 so even using the facelets it'll work with no problem with IE 6.0.

My question is on the HtmlResponseWriter, which is the component responsible for the rendering. Is it possible to extend it and override its methods so we accomplish the desired effect of the filter?

  • The content type will always be output as text/html;
  • The encoding will be always UTF-8;
  • The script tag will be wrapped inside a < ! -- <[[CDATA ]]> --> .

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百善笑为先 2024-09-14 20:18:09

是的,我们扩展了 JSF(实际上是 Oracle ADF)组件,以满足无法开箱即用的特殊要求。您将需要获取这些渲染的所有源文件,并对要删除的有问题的 HTML(application/xhtml+xml)进行递归搜索。这只是为了确保它实际上位于 HtmlResponseWriter 类中。 JSF 组件框架可能很复杂,因此您永远不知道,可能还有其他实例呈现此标头。

由于 HtmlResponseWriter 不像某些组件那样声明为 final,因此您可以扩展它,并重写 application/xhtml+xml 所在的方法正在打印并在 注册它 faces-config.xml。唯一的障碍是,如果您需要重写的方法中引用了在 HtmlResponseWriter 内部声明的私有变量。如果是这种情况,您将无法在重新实现中引用它们,或者必须完全重新构建一个新的 HtmlResponseWriter 类(通过扩展 ResponseWriter 和模仿每个方法和实例变量)。扩展 HTMLResponseWriter 的好处是,您将自动获取对其所做的任何更改(来自 JSF 更新)(当然,覆盖方法中的更改除外)。

更新:
这就是我对 faces-config.xml 所做的操作,但它使用的是 Oracle ADF:

<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee">
  <application>
    <default-render-kit-id>oracle.adf.rich</default-render-kit-id>
  </application>
  <render-kit>
    <render-kit-id>oracle.adf.rich</render-kit-id>
    <renderer>
      <component-family>org.apache.myfaces.trinidad.Input</component-family>
      <renderer-type>oracle.adf.rich.Text</renderer-type>
      <renderer-class>com.company.jsf.renders.text.CustomRenderer</renderer-class>
    </renderer>
  </render-kit>
</faces-config>

Yes, we have extended JSF (actually Oracle ADF) components in order to meet special requirements that could not be done out of the box. You will need to get all of the source files of those renders and do a recursive search for the offending HTML you want removed, the application/xhtml+xml. This is just to make sure it is in fact inside the HtmlResponseWriter class. JSF component frameworks can be complex so you never know, there may be other instances where this header is rendered.

Since the HtmlResponseWriter isn't declared final like some components are, you can just extend this, and override the method where application/xhtml+xml is being printed and register it in faces-config.xml. The only obstacles to this is if there are private variables declared inside of HtmlResponseWriter being referenced in the method that you need to override. If that is the case you will either not be able to reference them in your reimplementation or you will have to completely re-build a new HtmlResponseWriter class (by extending the ResponseWriter and mimicking each method and instance variable). The benefit of extending the HTMLResponseWriter is that you will pick up any changes (from JSF updates) to it automatically (except in the overridden method of course).

Update:
This is what I did for my faces-config.xml, but it is using Oracle ADF:

<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee">
  <application>
    <default-render-kit-id>oracle.adf.rich</default-render-kit-id>
  </application>
  <render-kit>
    <render-kit-id>oracle.adf.rich</render-kit-id>
    <renderer>
      <component-family>org.apache.myfaces.trinidad.Input</component-family>
      <renderer-type>oracle.adf.rich.Text</renderer-type>
      <renderer-class>com.company.jsf.renders.text.CustomRenderer</renderer-class>
    </renderer>
  </render-kit>
</faces-config>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文