简体   繁体   English

如何从 bean 中的字符串字段在 html 页面上呈现 JSF 标记

[英]How to render JSF tags on html page from String field in a bean

How can I render JSF tags like HTML tags?如何渲染 JSF 标签,如 HTML 标签?

For example I have some html code in a string field:例如,我在字符串字段中有一些 html 代码:

<pre>String code = "example <br/> example";</pre>

If I use如果我使用

<pre><h:outputText value="#{bean.code}" escape="false"/></pre>

I will get我会得到

<pre>example
example</pre>

Now I want to render jsf component in the same way.现在我想以同样的方式渲染 jsf 组件。 For example:例如:

 <pre>String code = '<h:button value="something" action="something"/>';</pre>

But it will be rendered as it is.但它将按原样呈现。 As I think it must be translated first by JSF servlet to html tags.我认为它必须首先由 JSF servlet 翻译为 html 标签。 How could I render JSF controlls in that way?我怎么能以这种方式渲染 JSF 控件? I've got texts containing lots of JSF controls (articles) stored in a database and I need to easy render JSF controlls from that.我的文本包含大量存储在数据库中的 JSF 控件(文章),我需要从中轻松渲染 JSF 控件。

This is a very bad idea.这是一个非常糟糕的主意。 The JSF tags has to go in a Facelet file and the text has to go in DB. JSF 标签在 Facelet 文件中必须为 go,而文本在 DB 中必须为 go。 You should not manually add JSF tags to the text before storing in DB.在存储到数据库之前,您不应手动将 JSF 标签添加到文本中。 You should do it in the Facelet file while displaying text from DB.您应该在显示来自 DB 的文本时在 Facelet 文件中执行此操作。 That's how the "V" of MVC is supposed to work.这就是 MVC 的“V”应该如何工作的。

If you really insist to do so (this is prone to security attacks and you have to do a lot of validation to prevent that), then best what you could do is to create a Facelets ResourceResolver which gets the data from the DB, saves it as a Facelet file in memory or on disk and returns the URL to it.如果您真的坚持这样做(这很容易受到安全攻击,并且您必须进行大量验证以防止这种情况发生),那么您可以做的最好的事情是创建一个 Facelets ResourceResolver从数据库中获取数据并保存它作为 memory 或磁盘上的 Facelet 文件,并将URL返回给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM