(1),模板modify_jsp.ftl文件
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String childPath = request.getContextPath();
String basePath = request.getScheme()+"://"
+request.getServerName()+":"+request.getServerPort()+ childPath +"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<%@ include file="/pub/scripts.jsp"%>
<script type="text/javascript">
function submit_save() {
var varName_ = $("#name_").val();
if(varName_ == null || varName_ == '') {
alert("请先输入名称!")
return false;
} else {
$("#modifyForm").submit();
}
}
// 循环字段集,名称字符与元素id获取元素
<#list mapDataList as listItm >
美元符("井号${listItm.columnName}").val() 双斜杆 ${listItm.columnComment}
</#list>
</script>
</head>
<body>
<!-- 标题区域 -->
<div class="tleBg">
<div class="title">
<span class="txt"> <em></em><#list mapDataTableComment as listItmCom > ${listItmCom.columnComment} </#list>- 修改
</span> <a href="javascript:;" class="search-hide"></a>
</div>
<div class="clr"></div>
</div>
<!-- 查询维度区域 -->
<div class="t_list" id="addtable">
<form name="modifyForm" id="modifyForm" action="<%=childPath %>/<#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list>Conter/<#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list>ModifySave" method="post">
<input type="hidden" name="tid" value="美元符左大括<#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list>Dto.tid右大括" />
<table border="0" cellspacing="0" width="100%" cellpadding="0" class="t_list_1">
<tbody>
<#list mapDataList as listItm >
<tr style="line-height: 36px;">
<td width="15%">
</td>
<td width="20%" class="text" style="text-align: right;">
${listItm.columnComment}:
</td>
<td>
<input type="text" id="${listItm.columnName}" name="${listItm.columnName}" style="width: 300px;" class="inputStyle" value="美元符左大括${listItm.columnName}Dto.name_右大括" />
</td>
<td width="15%">
</td>
</tr>
</#list>
<tr>
<td width="15%">
</td>
<td width="20%" class="text" style="text-align: right;">
</td>
<td>
<input type="button" id="btn_add" value="保 存" class="t_button_gray" onclick="return submit_save();" />
<input type="reset" id="btn_rest" value="重 置" class="t_button_gray" />
</td>
<td width="15%">
</td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
(2),生成修改jsp文件
public class TestCls {
public static void main(String[] args) throws Exception {
TestCls testCls = new TestCls();
String testT00_po = "testT00_user";
String generFileName = testT00_po + "Modify.jsp";
testCls.outTemplate(testT00_po, "用户数据"
, generFileName, "modify_jsp.ftl" );
System.out.println("执行成功!");
}
// 输出到模板
private void outTemplate(String entryInfoPoName, String tableComment
, String templateFileName , String templFileName ) throws Exception {
Configuration configuration = new Configuration();
String templateDir = "F:\\xbl\\myResource\\temp_test_";
configuration.setDirectoryForTemplateLoading(new File(templateDir));
Template template = configuration.getTemplate(templFileName);
Map<String, List<EntryInfoPo>> dataMap = new HashMap<String, List<EntryInfoPo>>();
List<EntryInfoPo> listEntryInfo = new ArrayList<EntryInfoPo>();
listEntryInfo = testMetaAddComment();
dataMap.put("mapDataList", listEntryInfo );
// 表注释
dataMap.put("mapDataTableComment", testTableComment(entryInfoPoName, tableComment ));
String generFileDir = "F:\\xbl\\myResource\\temp_test_\\gener_file_\\";
File generFile = new File(generFileDir + templateFileName);
if(generFile.exists()) {
generFile.delete();
}
generFile.createNewFile();
Writer outputWriter = new OutputStreamWriter(new FileOutputStream(generFile ) );
// Writer outputWriter = new OutputStreamWriter(System.out );
template.process(dataMap, outputWriter);
System.out.println("输出完成!");
outputWriter.close();
}
// 表注释
private List<EntryInfoPo> testTableComment(String entryInfoPoName, String tableComment) throws Exception {
List<EntryInfoPo> listTableComment = new ArrayList<EntryInfoPo>();
EntryInfoPo entryInfoPo = new EntryInfoPo();
entryInfoPo.setColumnName(entryInfoPoName);
entryInfoPo.setColumnComment(tableComment);
entryInfoPo.setUpperColumnName(firTextUpper( entryInfoPoName ) );
listTableComment.add(entryInfoPo);
return listTableComment;
}
// 首字母大写
private String firTextUpper(String scontext ) {
int textLength = scontext.length();
String sufText = scontext.substring(1, textLength);
String firText = scontext.substring(0, 1);
firText = firText.toUpperCase();
String finText = firText + sufText;
return finText;
}
private List<EntryInfoPo> testMetaAddComment() throws Exception {
List<EntryInfoPo> listEntryInfoPo = new ArrayList<EntryInfoPo>();
Connection connection = getDataBaseConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("show full columns from t00_user ");
while (resultSet.next()) {
String columnComment = resultSet.getString("Comment");
String columnName = resultSet.getString("Field");
columnName = columnName.toLowerCase();
EntryInfoPo entryInfoPo = new EntryInfoPo();
entryInfoPo.setColumnName(columnName);
entryInfoPo.setColumnComment(columnComment);
listEntryInfoPo.add(entryInfoPo);
}
return listEntryInfoPo;
}
private Connection getDataBaseConnection() throws Exception {
// 连接数据库
Class.forName("com.mysql.jdbc.Driver");
String databaseUrl = "jdbc:mysql://127.0.0.1:3306/grcdb05?useUnicode=true&characterEncoding=utf8";
Connection connection = DriverManager.getConnection(databaseUrl, "root", "root");
return connection;
}
}
(3),注意替换空格及替换文字