使 JSF 支持带有 HTML 标签 的 Message

背景介绍

JSF 的 HtmlMessage 标签一般用于显示系统的 Error 或者 Validation 消息,至于其标准用法可以参考官方文档。在大部分的文章中,都讲解了其如何绑定到标签或者基于 table 和 list 去显示消息,但是这些消息都是标准的文字格式,没有夹带有 HTML tag。比如说,原有系统的所有 error 消息都是存在与 property 文件中,而且格式为
xml 代码
  1. ems=<b>Amount Countb> must be more than <font color=green>150$font>

这种格式的消息在被 HtmlMessage tag 渲染显示后,全部变为了转义后的字符,也就是在页面中显示的和上面的文字完全相同。

由于 HtmlOutputText 控件有 escape 属性,所以可以很好地解决这种问题,可是在 HtmlMessage 控件中没有提供这种属性。

解决方法

正是受到 HtmlOutputText 的影响,才想到能不能自己写一个类似的标签来解决相应的问题。那么问题的关键是这些 validation message 是在什么时候被转义的,是在传递到标签之前还是之后。如果是在传递到标签之前,那么这种方法是没有任何意义的,因为无论你的标签怎么写,你所接受到的都是已经转义后的内容了。

经过求证发现,所有的 FacesMessage 可以通过 FacesContext 获取到,而且 Message 中的消息都是没有转义之前的内容,这样就可以通过自定义标签来解决上面的问题了。
java 代码
  1. package com.xxx.xxx;
  2. import javax.servlet.jsp.*;
  3. import javax.servlet.jsp.tagext.*;
  4. import java.io.IOException;
  5. import java.util.Iterator;
  6. import javax.faces.application.FacesMessage;
  7. import javax.faces.context.FacesContext;
  8. public class ValidatorTag implements javax.servlet.jsp.tagext.Tag{
  9. private PageContext pageContext;
  10. private Tag parent;
  11. public ValidatorTag() {
  12. super();
  13. }
  14. /**
  15. * configure tag context
  16. */
  17. public void setPageContext(PageContext pageContext){
  18. this.pageContext = pageContext;
  19. }
  20. /**
  21. * configure the last level tag
  22. */
  23. public void setParent(Tag parent){
  24. this.parent = parent;
  25. }
  26. public int doStartTag()throws JspTagException{
  27. return SKIP_BODY;
  28. }
  29. public int doEndTag() throws JspTagException{
  30. try{
  31. // there is tag logic
  32. String errorMessages = "";
  33. FacesContext ctx = FacesContext.getCurrentInstance();
  34. if(ctx.getMessages()!=null){
  35. for (Iterator i = ctx.getMessages(); i.hasNext(); ) {
  36. FacesMessage o = (FacesMessage) i.next();
  37. errorMessages = errorMessages + o.getDetail();
  38. }
  39. }
  40. if(!errorMessages.equals(""))
  41. pageContext.getOut().write(errorMessages);
  42. }catch(IOException e){
  43. throw new JspTagException(e.getMessage());
  44. }
  45. return EVAL_PAGE;
  46. }
  47. public void release(){
  48. }
  49. public PageContext getPageContext() {
  50. return pageContext;
  51. }
  52. public Tag getParent() {
  53. return parent;
  54. }
  55. }

因为仅仅是为了在页面上显示 validation 信息,所以没有必要在 faces-config.xml 中注册。为了在JSP页面中可用,需要在TLD中进行声明如下:
xml 代码
  1. xml version="1.0" encoding="ISO-8859-1" ?>
  2. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptablibrary_2_0.xsd"
  5. version="2.0">
  6. <description>Demo</description>
  7. <tlib-version>1.0</tlib-version>
  8. <short-name>Demo Tag</short-name>
  9. <uri>http://com.demo/tags</uri>
  10. <tag>
  11. <description>This is a Tag for Validation Error Message</description>
  12. <name>errorMessage</name>
  13. <tag-class>com.cxxx.xxx.ValidatorTag</tag-class>
  14. <body-content>empty</body-content>
  15. </tag>
  16. </taglib>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值