如何从 bean 中的字符串字段在 html 页面上呈现 JSF 标签
如何像 HTML 标签一样呈现 JSF 标签?
例如,我在字符串字段中有一些 html 代码:
<pre>String code = "example <br/> example";</pre>
如果我使用,
<pre><h:outputText value="#{bean.code}" escape="false"/></pre>
我将得到
<pre>example
example</pre>
Now I Want to render jsf component in the same way。例如:
<pre>String code = '<h:button value="something" action="something"/>';</pre>
但它会按原样渲染。我认为它必须首先由 JSF servlet 翻译为 html 标签。我怎样才能以这种方式呈现 JSF 控件?我的文本包含大量存储在数据库中的 JSF 控件(文章),我需要从中轻松呈现 JSF 控件。
How can I render JSF tags like HTML tags?
For example I have some html code in a string field:
<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. 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. How could I render JSF controlls in that way? I've got texts containing lots of JSF controls (articles) stored in a database and I need to easy render JSF controlls from that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个非常糟糕的主意。 JSF 标签必须放入 Facelet 文件中,文本必须放入 DB 中。在将文本存储到数据库之前,不应手动将 JSF 标记添加到文本中。您应该在 Facelet 文件中执行此操作,同时显示数据库中的文本。这就是 MVC 的“V”应该如何工作的。
如果您真的坚持这样做(这很容易受到安全攻击,并且您必须进行大量验证来防止这种情况),那么您能做的最好的事情就是创建一个 Facelets
ResourceResolver
从数据库获取数据,将其作为 Facelet 文件保存在内存或磁盘上,然后将URL
返回给它。This is a very bad idea. The JSF tags has to go in a Facelet file and the text has to go in DB. You should not manually add JSF tags to the text before storing in DB. You should do it in the Facelet file while displaying text from DB. That's how the "V" of MVC is supposed to work.
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 theURL
to it.