web: _show -> _info 造轮子编程

9 篇文章 0 订阅

问题场景

用Java进行web开发过程当中,当遇到很多很多个字段的实体时,最苦恼的莫过于编辑字段的查看和修改界面,发现2个页面存在很多重复信息,能不能写一遍?有没有轮子用都不如自己造。

解决方式

笔者根据自己页面的长期特点编写了自己的转换规则,其中采用模板模式实现,个性化转换直接继承抽象类即可。

效果

before:
这里写图片描述

after:
这里写图片描述

CODE

package regular;

/**
 * Created on 2017/11/13
 * Author: youxingyang.
 */
abstract class BaseTransfer {
    abstract String transfer(String string);
}

package regular;

import file.FileUtils;
import java.io.*;
import java.util.regex.Pattern;

/**
 * Created on 2017/11/13
 * Author: youxingyang.
 */
public class Ul2Tr extends BaseTransfer {
    private static Pattern blank = Pattern.compile("^[//s&&[^//n]]*$");

    /**
     * '<ul class="forminfo">
            <li><label >样本编号     :</label>
            <input type="text" name="sampleNum" id="sampleNum" class="input_txt" value="${chromosomeNew.sampleNum}"  readonly="readonly"/>
            </li>
        </ul>'
     ==>
     * '<tr class="info">
            <th>样本编号:</th>
            <td><input type="text" name="sampleNum" id="sampleNum" class="input_txt" value="${chromosomeNew.sampleNum}"/></td>
        </tr>'
     * @param string
     * @return
     */
    @Override
    public String transfer(String string) {
        String res = "";
        if (string != null) {
            if (string.contains("forminfo")) {
                res = string.replaceAll("ul", "tr").replaceAll("forminfo", "info");
            } else if (string.contains("<li><label >") && string.contains("</label>")) {
                res = "\t" + string.replaceAll("<li>", "").replaceAll(" ", "").replaceAll(":", ":").replaceAll("label", "th");
            } else if (string.contains("<input ") && string.contains("readonly=\"readonly\"/>")) {
                string = string.replaceAll("\t", "");
                string = string.substring(string.indexOf('<'));
                string = string.substring(0, string.lastIndexOf(' ') + 1);
                res = "\t" + "\t" + "<td>" + string + "/></td>";
            } else if (string.contains("ul>")) {
                res = string.replaceAll("ul", "tr");
            }
        }
        return res;
    }

    /**
     * 转换show -> info
     * @param src
     * @param des
     */
    public void transferFile(String src, String des) {
        File file = new File(src);
        if (!file.exists()) {
            return;
        }
        StringBuilder sb = new StringBuilder("");
        //read and transfer
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            String line;
            while (reader.ready()) {
                line = reader.readLine();
                if (!blank.matcher(line).find()) {
                    String newLine = transfer(line);
                    if (!"".equals(newLine)) {
                        sb.append(newLine).append("\n");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        File desFile = new File(des);
        if (desFile.exists()) {
            if (desFile.delete()) {
                System.out.println(desFile.getAbsolutePath() + " is deleted");
            }
        }
        if (createFile(des)) {
            //write
            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(desFile), "utf-8"));
                bw.write(sb.toString());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bw != null) {
                    try {
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 创建一个文件
     * @param filePath
     * @return
     */
    public static boolean createFile(String filePath) {
        File file = new File(filePath);
        // 判断文件是否存在
        if (file.exists()) {
            System.out.println("目标文件已存在" + filePath);
            return false;
        }
        // 判断文件是否为目录
        if (filePath.endsWith(File.separator)) {
            System.out.println("目标文件不能为目录!");
            return false;
        }
        // 判断目标文件所在的目录是否存在
        if (!file.getParentFile().exists()) {
            // 如果目标文件所在的文件夹不存在,则创建父文件夹
            System.out.println("目标文件所在目录不存在,准备创建它!");
            // 判断创建目录是否成功
            if (!file.getParentFile().mkdirs()) {
                System.out.println("创建目标文件所在的目录失败!");
                return false;
            }
        }
        try {
            // 创建目标文件
            if (file.createNewFile()) {
                System.out.println("创建文件成功:" + filePath);
                return true;
            } else {
                System.out.println("创建文件失败!");
                return false;
            }
            // 捕获异常
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("创建文件失败!" + e.getMessage());
            return false;
        }
    }

    public static void main(String[] args) {
        new Ul2Tr().transferFile("C:\\Users\\domainclient\\Desktop\\src.txt", "C:\\Users\\domainclient\\Desktop\\des.txt");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值