There is a mobile phone number in the page list, such as 15152462345, which is displayed as 1515****345.
Bank card number 62282542325242. Display 5242 (last 4 digits)
In the beginning, it was processed with js, but the card number was still on the page source code, causing customer information leakage
Later, I used the bean tag of <%@ taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %>, and an attribute was format
But no effect, check the source code org.apache.struts.taglib.bean.WriteTag
protected String formatValue(Objeorg.apache.struts.taglib.beanct valueToFormat)
throws JspException {
Format format = null;
Object value = valueToFormat;
Locale locale =
TagUtils.getInstance().getUserLocale(pageContext, this.localeKey);
boolean formatStrFromResources = false;
String formatString = formatStr;
// Return String object as is.
if (value instanceof java.lang.String) {
return (String) value;
} else {
// Try to retrieve format string from resources by the key from
// formatKey.
if ((formatString == null) && (formatKey != null)) {
formatString = retrieveFormatString(this.formatKey);
if (formatString != null) {
formatStrFromResources = true;
}
}
// Prepare format object for numeric values.
if (value instanceof Number) {
if (formatString == null) {
if ((value instanceof Byte) || (value instanceof Short)
|| (value instanceof Integer)
|| (value instanceof Long)
|| (value instanceof BigInteger)) {
formatString = retrieveFormatString(INT_FORMAT_KEY);
} else if ((value instanceof Float)
|| (value instanceof Double)
|| (value instanceof BigDecimal)) {
formatString = retrieveFormatString(FLOAT_FORMAT_KEY);
}
if (formatString != null) {
formatStrFromResources = true;
}
}
if (formatString != null) {
try {
format = NumberFormat.getNumberInstance(locale);
if (formatStrFromResources) {
((DecimalFormat) format).applyLocalizedPattern(
formatString);
} else {
((DecimalFormat) format).applyPattern(formatString);
}
} catch (IllegalArgumentException e) {
JspException ex =
new JspException(messages.getMessage(
"write.format", formatString));
TagUtils.getInstance().saveException(pageContext, ex);
throw ex;
}
}
} else if (value instanceof java.util.Date) {
if (formatString == null) {
if (value instanceof java.sql.Timestamp) {
formatString =
retrieveFormatString(SQL_TIMESTAMP_FORMAT_KEY);
} else if (value instanceof java.sql.Date) {
formatString =
retrieveFormatString(SQL_DATE_FORMAT_KEY);
} else if (value instanceof java.sql.Time) {
formatString =
retrieveFormatString(SQL_TIME_FORMAT_KEY);
} else if (value instanceof java.util.Date) {
formatString = retrieveFormatString(DATE_FORMAT_KEY);
}
}
if (formatString != null) {
format = new SimpleDateFormat(formatString, locale);
}
}
}
if (format != null) {
return format.format(value);
} else {
return value.toString();
}
}
If it is found to be a string, skip it directly.
So customize a class to override this method
The main problem is that format cannot be an expression. Take the first 3 digits + **** + the last 4 digits. It only has the function of taking the first few digits, such as %.3s is taking the first 3 digits
Of course, format can be regarded as a regular expression, but the first few digits of the regular expression are ^.{0,n}, and the last 4 digits are .{4}$. But it still cannot be integrated in one expression.
So the host can only customize a rule. [L]%.ns[/L] means take the last n digits, then reverse the string, take the first n digits, and then reverse it again, which is the last n digits
E.g:
<bean:write name="termOrder" property="outPhone.mobile" format="%.3s****[L]%.4s[/L]" />
Means to take the first 3 digits + **** + the last 4 digits
Attach code
/**
*
*/
package org.apache.struts.taglib.bean;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.jsp.JspException;
import com.cupdata.telsales.util.StringUtils;
/**
* @author cruze([email protected]) 2014-7-1
*
*/
public class MyWriteTag extends WriteTag{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
/**
* 自定义format,主要是字符串没有格式化
* 如果[L]存在,则[L]%.4s取最后4位
* 例如%.3s**%.3s**[L]%.4s[/L]
* added by cruze at 2014-6-30
*/
protected String formatValue(Object valueToFormat) throws JspException {
String formatString = formatStr;
Object value = valueToFormat;
if (value instanceof java.lang.String && !StringUtils.isNull(value)
&& !StringUtils.isNull(formatString)) {
String inValue=value.toString();
//判断有几个%则加入几个变量
//如果存在[L],则变量要取反
//最后将[L]再次取反
//去掉[L]和[/L]
List<Object> para=new ArrayList<Object>();
for(int i=0;i<formatString.length();i++){
if('%'==formatString.charAt(i)){
if(i>2&&"[L]".equals(formatString.substring(i-3, i))){
//字符串取反
para.add(org.apache.commons.lang.StringUtils.reverse(inValue));
}
else{
para.add(value);
}
}
}
//格式化
String res=String.format(formatString,para.toArray());
String returnRes=new String(res);
Pattern p = Pattern.compile("\\[L](.*?)\\[/L]");
Matcher m = p.matcher(res);
while(m.find()) {
String find=m.group().substring(3, m.group().length()-4);
String xifu=org.apache.commons.lang.StringUtils.reverse(find);
String tmp=returnRes.replace(m.group(), xifu);
returnRes=null;
returnRes=tmp;
}
return returnRes;
}
return super.formatValue(valueToFormat);
}
}
Modify struts-bean.tld
<tagclass>org.apache.struts.taglib.bean.WriteTag</tagclass>改为<tagclass>org.apache.struts.taglib.bean.MyWriteTag</tagclass>即可
effect: