根据模板及表元数据生成新增jsp页面

(1),模板文件add_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 {
                $("#addForm").submit();
			}
        }
    </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="addForm" id="addForm" action="<%=childPath %>/testT00_userConter/testT00_userAddSave" method="post">

				<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%">
								&nbsp;
							</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="原左大括<#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list>.${listItm.columnName}原右大括" />
							</td>
							<td width="15%">
								&nbsp;
							</td>
						</tr>
					</#list>

					
						<tr>
							<td width="15%">
								&nbsp;
							</td>
							<td width="20%" class="text" style="text-align: right;">
								&nbsp;
							</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%">
								&nbsp;
							</td>
						</tr>

					</tbody>
				</table>
			</form>
		</div>
  		
  </body>
</html>

(2),根据模板及表元数据生成新增jsp页面

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date: 2022/12/03 08:27
 * @desc: 测试入口
 */
public class TestCls {

    public static void main(String[] args) throws Exception {
        new TestCls().outTemplate();
    }

    // 输出到模板
    private void outTemplate() throws Exception {

        Configuration configuration = new Configuration();
        configuration.setDirectoryForTemplateLoading(new File("F:\\xbl\\myResource\\temp_test_"));
        Template template = configuration.getTemplate("add_jsp_.ftl");

        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());

        Writer outputWriter = new OutputStreamWriter(new FileOutputStream(new File("F:\\xbl\\myResource\\temp_test_\\add_.jsp") ) );
//        Writer outputWriter = new OutputStreamWriter(System.out );
        template.process(dataMap, outputWriter);
        System.out.println("输出完成!");
        outputWriter.close();
    }

    // 表注释
    private List<EntryInfoPo> testTableComment() throws Exception {

        List<EntryInfoPo> listTableComment = new ArrayList<EntryInfoPo>();
        EntryInfoPo entryInfoPo = new EntryInfoPo();
        entryInfoPo.setColumnName("testT00_userDto");
        entryInfoPo.setColumnComment("用户管理");
        listTableComment.add(entryInfoPo);
        return listTableComment;
    }

    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");
            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&amp;characterEncoding=utf8";
        Connection connection = DriverManager.getConnection(databaseUrl, "root", "root");
        return connection;
    }
}

(3),创建一个空文件add_.jsp,执行步骤(2)能生成页面

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值