需求
textarea输入保存到数据库有换行符。输出到页面要求保留换行效果。web可以用“pre”等标签保留换行效果(\r\n)
问题:
- pdf4将web页面导出成PDF,“pre”处理的换行效果无法保留;
- FreeMarker生成的word,无法保留换行效果,强行实现也比较繁琐;
原来的解决思路
- 在前台处理:在jsp中或FreeMarker模板中用相应的方法切割字符串,再循环绑定到需要的地方。页面逻辑很复杂,容易出错;
- 在后台处理:在数据bean中定义一个List,在后端把字符串分割了放到list。然后直接在页面或word中进行循环;页面是不复杂了,但增加了后台代码,每个要分割的字段都需要处理,也费劲。
目前的解决思路:定义一个注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BreakLine {
public String name();
}
name定义了需要将哪个字段进行分割;
拥有该注解的字段是一个List,用来保存分割后的列表;
工具类定义
public class BreakLineUtil {
public static <T> void anaLines(List<T> objList) {
if (objList == null || objList.size() == 0) {
return;
}
for (Object object : objList) {
anaLine(object);
}
}
public static void anaLine(Object target) {
try {
Class cla = target.getClass();
Field[] fils = getAllFiled(cla);
if (fils == null || fils.length == 0) {
return;
}
for (int i = 0; i < fils.length; i++) {
Field fil = fils[i];
BreakLine line = fil.getAnnotation(BreakLine.class);
if (line != null) {
String name = line.name();
if (name == null || name.trim().length() == 0) {
continue;
}
fil.setAccessible(true);
Field supFil = getSuperFiledVal(fils, name);
if (supFil == null) {
continue;
}
supFil.setAccessible(true);
String oneLine = (String) supFil.get(target);
if (oneLine == null || oneLine.trim().length() == 0) {
continue;
}
List<String> line_list = breakLine(oneLine);
fil.set(target, line_list);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}
private static List<String> breakLine(String line) {
if (line == null || line.trim().length() == 0) {
return null;
}
List<String> line_list = new ArrayList<String>();
String[] lines = line.split("\r\n");
if (lines == null || lines.length == 0) {
return null;
}
for (String string : lines) {
line_list.add(string);
}
return line_list;
}
private static Field[] getAllFiled(Class cla) {
if (cla == null) {
return null;
}
Class classnew = cla;
List<Field> fils = new ArrayList<Field>();
for (int i = 0; i < 5; i++) {
Field[] fis = classnew.getDeclaredFields();
if (fis != null && fis.length > 0) {
for (Field field : fis) {
fils.add(field);
}
classnew = classnew.getSuperclass();
if (classnew == null) {
break;
}
}
}
if (fils == null || fils.size() == 0) {
return null;
}
Integer len = fils.size();
Field[] finalFils = new Field[len];
for (int i = 0; i < len; i++) {
finalFils[i] = fils.get(i);
}
return finalFils;
}
private static Field getSuperFiledVal(Field[] supFils, String name) {
for (Field field : supFils) {
if (field.getName().equals(name)) {
return field;
}
}
return null;
}
}
测试
数据Bean
public class BookBean {
private String desc;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
子类
public class BookModel extends BookBean {
public BookModel(BookBean bean) {
if (bean != null) {
BeanUtil.copyBean(bean, this);
BreakLineUtil.anaLine(this);
}
}
@BreakLine(name = "desc")
private List<String> desc_list;
public List<String> getDesc_list() {
return desc_list;
}
public void setDesc_list(List<String> desc_list) {
this.desc_list = desc_list;
}
}