jsp隐藏关键 敏感信息,只显示前后字段

今天写jsp页面,要求对字段中间部分隐藏,只显示前几位和后几位。搜了一下发现网上大都是隐藏前面指定字段,或者是利用正则表达式隐藏手机号或是身份证。这样的话必须预先知道字段长度,而我不想知道长度只显示前3位和后4位。

没办法,谁让我需要隐藏的字段长度未定呢。

解决方案:1、如果知道字段长度的话可以用正则表达式或是jsp标签库里的fn函数

  正则表达式

phone.replaceAll("(\d{3})\d{4}(\d{4})","$1****$2");  
152****4799
idCard.replaceAll("(\d{4})\d{10}(\w{4})","$1*****$2");
4304*****7733

  fn函数

${fn:substring(item.mobile,0,3)}****${fn:substring(item.mobile,7,11)}
152****4799
${fn:substring(item.idCard,0,4)}****${fn:substring(item.idCard,14,18)}
4304****7733

2、不知道字段长度,只显示前部分和后部分,只能用fn了

${fn:substring(item.account,0,3)}****${fn:substring(item.account,fn:length(item.account)-4,(fn:length(item.account)))}

这样就只显示前3位和后4位了

再贴上只显示前几位,后几位用.......省略号代替的例子,用于太长的标题

  <c:if test="${fn:length(itrm.fundName) > 10 }">${fn:substring(item.fundName, 0, 10) }... </c:if>  //最大显示10位,多于超出的用省略号表示
  <c:if test="${fn:length(item.fundName) <= 10 }">${item.fundName}</c:if>

OK!

  

原文地址:https://www.cnblogs.com/yangchas/p/7367299.html