java反射实现生成easyui中datagrid基础field

当使用datagrid实现页面时经常会遇到对象的所有属性都要添加进去,很是麻烦,于是便写了该工具代码,方便大家.可以直接生成jsp的基础页面,以及生成datagrid中所有属性的field,

注意: model支持任意级目录扫描,使用时注意配置各个路径

文件名配置方式为正则方式


package com.change.generate.bean;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;


public class AutoCreateDataGridColumn {
	private static final String BEAN_URL = "com.change.func.model";//model基础目录
	private static final String FILTER_NAME = "Whs\\w+\\.java";// 扫描的文件名,支持正则方式
	private static final String JSP_PATH = "/WebContent/securityJsp";//WEB文件目录
	private static final boolean WRITE_TO_FILE = false;//写入到文件
	private static final boolean PRINT_INFO_FLAG = true;// 打印信息开关
	private static final boolean PRINT_ERROR_FLAG = true;// 打印错误开关
	private static final boolean FORCE_FLAG = false;// 强制生成开关,若为true,即使文件已存在也重新生成
	private static final String RT_1 = "\r\n";// 换行
	private static final String BLANK_4 = "	";// ≈TAB
	private static final String BLANK_8 = BLANK_4 + BLANK_4;
	private static final String BLANK_12 = BLANK_8 + BLANK_4;

	public static void main(String[] args)
	{
		String fileName = System.getProperty("user.dir") + "/src/"
				+ BEAN_URL.replace(".", "/");
		File file = new File(fileName);
		beanScan(file, "", true);
	}
	
	/**
	 * 
	 * <p>
	 * bean文件扫描
	 * </p>
	 * <p>
	 * 扫描指定目录下的所有java文件,生成对应的service和action,与bean同目录结构
	 * </p>
	 * 
	 * @param file
	 *            需要扫描的文件
	 * @param parentPack
	 *            上级包名:若无传递"",若有使用.开头,如".log"
	 */
	public static void beanScan(File file, String parentPack, boolean isFirst)
	{
		if (file.exists())
		{
			if (file.isDirectory())
			{
				File[] fs = file.listFiles();
				for (int i = 0; i < fs.length; i++)
				{
					File f = fs[i];
					String parent = "";
					if (!isFirst)
					{
						parent = parentPack + "." + file.getName();
					}
					beanScan(f, parent, false);
				}
			}
			else if (file.isFile() && file.getName().matches(FILTER_NAME))
			{
				try
				{
					createByBean(file, parentPack);// 创建相关类
				} catch (Exception e)
				{
					printError(file.getAbsolutePath(), e);
				}
			}
		}
	}

	/**
	 * 
	 * <p>
	 * 创建对应的对象
	 * </p>
	 * <p>
	 * 创建serviceI,serviceImpl,Action
	 * </p>
	 * 
	 * @param f
	 *            被创建的对象
	 * @param parentPack
	 *            上级包名:若无传递"",若有使用.开头,如".log"
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public static void createByBean(File f, String parentPack) throws Exception
	{
		Class c = Class.forName(BEAN_URL + parentPack + "."
				+ f.getName().substring(0, f.getName().indexOf(".")));
		createBeanPage(c, parentPack);

	}

	/**
	 * 追加顶部
	 * @param sb
	 */
	private static void appendHead(StringBuffer sb){
		sb.append("<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%>");
		sb.append(RT_1);
		sb.append("<%");
		sb.append(RT_1);
		sb.append(BLANK_4+"String contextPath = request.getContextPath();");
		sb.append(RT_1);
		sb.append("%>");
		sb.append(RT_1);
		sb.append("<!DOCTYPE html>");
		sb.append(RT_1);
		sb.append("<html>");
		sb.append(RT_1);
		sb.append("<head>");
		sb.append(RT_1);
		sb.append("<title>标题</title>");
		sb.append(RT_1);
		sb.append("<jsp:include page=\"../../inc.jsp\"></jsp:include>");
		sb.append(RT_1);
		sb.append("<script type=\"text/javascript\">");
		sb.append(RT_1);
		sb.append("/*##全局变量块##*/");
		sb.append(RT_1);
		sb.append("var grid;");
		sb.append(RT_1);
		sb.append("/*##业务块##*/");
		sb.append(RT_1);
		sb.append("/*##业务扩展##*/");
		sb.append(RT_1);
		sb.append("/*##过滤数据##*/");
		sb.append(RT_1);
		sb.append("/*##初始化##*/");
		sb.append(RT_1);
		sb.append("/**");
		sb.append(RT_1);
		sb.append("* [initGrid 初始化datagrid]");
		sb.append(RT_1);
		sb.append("*/");
		sb.append(RT_1);
		sb.append("function initGrid() {");
		sb.append(RT_1);
		sb.append(BLANK_4+"grid = $('#grid').datagrid({");
		sb.append(RT_1);
		sb.append(BLANK_8+"queryParams:{},");
		sb.append(RT_1);
		sb.append(BLANK_8+"url: sy.contextPath + '',");
		sb.append(RT_1);
		sb.append(BLANK_8+"idField: 'no',");
		sb.append(RT_1);
		sb.append(BLANK_8+"sortName: 'no',");
		sb.append(RT_1);
		sb.append(BLANK_8+"frozenColumns: [[{");
		sb.append(RT_1);
		sb.append(BLANK_12+"field: 'checkbox',");
		sb.append(RT_1);
		sb.append(BLANK_12+"checkbox: true");
		sb.append(RT_1);
		sb.append(BLANK_8+"}");
	}
	/**
	 * 追加底部
	 * @param sb
	 */
	private static void appendFoot(StringBuffer sb){
		sb.append(RT_1);
		sb.append(BLANK_8+"]]");
		sb.append(RT_1);
		sb.append(BLANK_4+"});");
		sb.append(RT_1);
		sb.append("};");
		sb.append(RT_1);
		sb.append("</script>");
		sb.append(RT_1);
		sb.append("</head>");
		sb.append(RT_1);
		sb.append("<body>");
		sb.append(RT_1);
		sb.append(BLANK_4+"<table id=\"grid\" data-options=\"\"></table>");
		sb.append(RT_1);
		sb.append("</body>");
		sb.append(RT_1);
		sb.append("</html>");
	}
	/**
	 * 根据属性生成grid
	 * @param c
	 * @param parentPack
	 * @throws IOException
	 */
	@SuppressWarnings("rawtypes")
	private static void createBeanPage(Class c, String parentPack) throws IOException {
		String cName = c.getSimpleName();
		String fileName = System.getProperty("user.dir") + JSP_PATH
				+parentPack.replace(".", "/")
				+ "/" + cName + ".jsp";
		// TODO Auto-generated method stub
		Field[] fs = c.getDeclaredFields();
		File targetFile = new File(fileName);
		if ((!targetFile.isFile()||FORCE_FLAG)||!WRITE_TO_FILE) {
			File pf = targetFile.getParentFile();
			if (!pf.isDirectory())
			{
				pf.mkdirs();
			}
			StringBuffer sb = new StringBuffer();
			appendHead(sb);
			for (int i = 0; i < fs.length; i++) {
				Field f = fs[i];
				String name = f.getName();
				sb.append(",{"+RT_1);
				sb.append(BLANK_12+"width: '100',");
				sb.append(RT_1);
				sb.append(BLANK_12+"title: '',");
				sb.append(RT_1);
				sb.append(BLANK_12+"field: '"+name+"'");
				sb.append(RT_1);
				sb.append(BLANK_8+"}");
			}
			appendFoot(sb);
			printLog(fileName);
			if (WRITE_TO_FILE) {
				FileWriter fw = new FileWriter(targetFile, false);
				fw.write(sb.toString());
				fw.flush();
				fw.close();
			}else {
				System.out.println(sb);
			}
		}
	}

	private static void printLog(String info)
	{
		if (PRINT_INFO_FLAG)
		{
			System.out.println("generate file:" + info + " success!");
		}
	}

	private static void printError(String info, Exception e)
	{
		if (PRINT_ERROR_FLAG)
		{
			System.err.println("generate file:" + info + " error!Exception:"
					+ e.getMessage());
			e.printStackTrace();
		}
	}
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值