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)}<br>152****4799 

${fn:substring(item.idCard,0,4)}****${fn:substring(item.idCard,14,18)}<br>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> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持来客网。