jsp文件做模板文件生成代码
pojo类的jsp模板
1 <%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%> 2 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 4 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 5 /* 6 * @(#)${ModelName}.java $$Revision: 1.1 $$ $$Date: 2013/01/23 03:49:23 $$ 7 * 8 * Copyright 2000 TopFounder. All rights reserved. 9 */ 10 package com.${companyname}.${projectname}.${modulename}.${modelname}; 11 12 /** 13 * This file used to <br> 14 * Created by ${author} 15 */ 16 public class ${ModelName} { 17 <c:forEach var="field" items="${fields}"> 18 //this field is ${field.desc} 19 private ${field.type} ${field.fieldName} ; 20 </c:forEach> 21 <c:forEach var="field" items="${fields}"> 22 public ${field.type} get${field.FieldName}(){ 23 return this.${field.fieldName}; 24 } 25 26 public void set${field.FieldName}(${field.type} ${field.fieldName}){ 27 this.${field.fieldName}=${field.fieldName}; 28 }</c:forEach> 29 }
生成pojo类的java核心代码
public static String createStaticHTMLPage(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, String fileFullPath, String jspPath) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); RequestDispatcher rd = servletContext.getRequestDispatcher(jspPath);//jspPath为模板的jsp文件名 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final ServletOutputStream servletOuputStream = new ServletOutputStream() { public void write(byte[] b, int off, int len) { byteArrayOutputStream.write(b, off, len); } public void write(int b) { byteArrayOutputStream.write(b); } }; final PrintWriter printWriter = new PrintWriter(new OutputStreamWriter( byteArrayOutputStream, "UTF-8")); HttpServletResponse httpServletResponse = new HttpServletResponseWrapper( response) { public ServletOutputStream getOutputStream() { return servletOuputStream; } public PrintWriter getWriter() { return printWriter; } }; rd.include(request, httpServletResponse); printWriter.flush(); File codeFile = new File(fileFullPath);//需要生成的pojo的文件,如果BaseUser.java FileUtils.createDirAndFile(codeFile); FileOutputStream fileOutputStream = new FileOutputStream(codeFile); byteArrayOutputStream.writeTo(fileOutputStream); fileOutputStream.close(); return fileFullPath; // response.sendRedirect(fileName); }
版权声明:本文为yangzhilong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。