温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使JSF支持带有HTMLMessage

发布时间:2021-07-14 09:47:33 来源:亿速云 阅读:129 作者:chen 栏目:编程语言

本篇内容主要讲解“如何使JSF支持带有HTMLMessage”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使JSF支持带有HTMLMessage”吧!

JSF背景介绍

JSF 的 HtmlMessage 标签一般用于显示系统的 Error 或者 Validation 消息,至于其标准用法可以参考官方文档。在大部分的文章中,都讲解了其如何绑定到标签或者基于 table 和 list 去显示消息,但是这些消息都是标准的文字格式,没有夹带有 HTML tag。比如说,原有系统的所有 error 消息都是存在与 property 文件中

  1. ems=<b>Amount Countb> must be more than <font color=green>150$font> 

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

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

JSF解决方法

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

经过求证发现,所有的 FacesMessage 可以通过 FacesContext 获取到,而且 Message 中的消息都是没有转义之前的内容,这样就可以通过自定义标签来解决上面的问题了。

  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. errorMessageserrorMessages = 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. }  

因为仅仅是为了在页面上显示 validation 信息,所以没有必要在 faces-config.xml 中注册。为了在JSP页面中可用,需要在TLD中进行声明如下:

  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> 

到此,相信大家对“如何使JSF支持带有HTMLMessage”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jsf
AI