zip包,然后自動下載下來
我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計、成都外貿(mào)網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、費縣ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的費縣網(wǎng)站制作公司
1.預(yù)先定義好模板
2.界面輸入相關(guān)參數(shù)
3.解析模板生成代碼并下載
最后放出源代碼:
package com.et.controller.system.createcode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.et.controller.base.BaseController;
import com.et.util.DelAllFile;
import com.et.util.FileDownload;
import com.et.util.FileZip;
import com.et.util.Freemarker;
import com.et.util.PageData;
import com.et.util.PathUtil;
/**
* 類名稱:FreemarkerController
* 創(chuàng)建人:Harries
* 創(chuàng)建時間:2015年1月12日
* @version
*/
@Controller
@RequestMapping(value=”/createCode”)
public class CreateCodeController extends BaseController {
/**
* 生成代碼
*/
@RequestMapping(value=”/proCode”)
public void proCode(HttpServletResponse response) throws Exception{
PageData pd = new PageData();
pd = this.getPageData();
/* ============================================================================================= */
String packageName = pd.getString(“packageName”); //包名 ========1
String objectName = pd.getString(“objectName”); //類名 ========2
String tabletop = pd.getString(“tabletop”); //表前綴 ========3
tabletop = null == tabletop?””:tabletop.toUpperCase(); //表前綴轉(zhuǎn)大寫
String zindext = pd.getString(“zindex”); //屬性總數(shù)
int zindex = 0;
if(null != zindext !””.equals(zindext)){
zindex = Integer.parseInt(zindext);
}
ListString[] fieldList = new ArrayListString[](); //屬性集合 ========4
for(int i=0; i zindex; i++){
fieldList.add(pd.getString(“field”+i).split(“,fh,”)); //屬性放到集合里面
}
MapString,Object root = new HashMapString,Object(); //創(chuàng)建數(shù)據(jù)模型
root.put(“fieldList”, fieldList);
root.put(“packageName”, packageName); //包名
root.put(“objectName”, objectName); //類名
root.put(“objectNameLower”, objectName.toLowerCase()); //類名(全小寫)
root.put(“objectNameUpper”, objectName.toUpperCase()); //類名(全大寫)
root.put(“tabletop”, tabletop); //表前綴
root.put(“nowDate”, new Date()); //當(dāng)前日期
DelAllFile.delFolder(PathUtil.getClasspath()+”admin/ftl”); //生成代碼前,先清空之前生成的代碼
/* ============================================================================================= */
String filePath = “admin/ftl/code/”; //存放路徑
String ftlPath = “createCode”; //ftl路徑
/*生成controller*/
Freemarker.printFile(“controllerTemplate.ftl”, root, “controller/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Controller.java”, filePath, ftlPath);
/*生成service*/
Freemarker.printFile(“serviceTemplate.ftl”, root, “service/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName+”Service.java”, filePath, ftlPath);
/*生成mybatis xml*/
Freemarker.printFile(“mapperMysqlTemplate.ftl”, root, “mybatis_mysql/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);
Freemarker.printFile(“mapperOracleTemplate.ftl”, root, “mybatis_oracle/”+packageName+”/”+objectName+”Mapper.xml”, filePath, ftlPath);
/*生成SQL腳本*/
Freemarker.printFile(“mysql_SQL_Template.ftl”, root, “mysql數(shù)據(jù)庫腳本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);
Freemarker.printFile(“oracle_SQL_Template.ftl”, root, “oracle數(shù)據(jù)庫腳本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);
/*生成jsp頁面*/
Freemarker.printFile(“jsp_list_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_list.jsp”, filePath, ftlPath);
Freemarker.printFile(“jsp_edit_Template.ftl”, root, “jsp/”+packageName+”/”+objectName.toLowerCase()+”/”+objectName.toLowerCase()+”_edit.jsp”, filePath, ftlPath);
/*生成說明文檔*/
Freemarker.printFile(“docTemplate.ftl”, root, “說明.doc”, filePath, ftlPath);
//this.print(“oracle_SQL_Template.ftl”, root); 控制臺打印
/*生成的全部代碼壓縮成zip文件*/
FileZip.zip(PathUtil.getClasspath()+”admin/ftl/code”, PathUtil.getClasspath()+”admin/ftl/code.zip”);
/*下載代碼*/
FileDownload.fileDownload(response, PathUtil.getClasspath()+”admin/ftl/code.zip”, “code.zip”);
}
}
Freemarker是一個模板框架。我們可以通過Freemarker進(jìn)行代碼生成或頁面的靜態(tài)生成。 現(xiàn)在簡單的說一下怎樣使用Freemarker Freemarker的主要生成類
public boolean generate(String templateFileName, Map data,
String fileName) {
try {
//取得模板的位置
String templateFileDir=templateFileName.substring(0, templateFileName.lastIndexOf("/"));
//取得模板的名字
String templateFile=templateFileName.substring(templateFileName.lastIndexOf("/"), templateFileName.length());
//取得生成文件的路徑
String genFileDir=fileName.substring(0, fileName.lastIndexOf("/"));
Template template = ConfigurationHelper.getConfiguration(templateFileDir).getTemplate(templateFile);
File fileDir=new File(genFileDir);
org.apache.commons.io.FileUtils.forceMkdir(fileDir);
File output = new File(fileName);
if(output.exists()){
//如何代碼已存在不重復(fù)生成
return false;
}
Writer writer = new FileWriter(output);
template.process(data, writer);
writer.close();
} catch (TemplateException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
代碼中的Map 是模板所需要的數(shù)據(jù),我們可以通過面向?qū)ο竦姆椒ò褦?shù)據(jù)存在模板中public boolean genDaoInterface(String fileName){
DaoModel daoModel=new DaoModel();
//設(shè)置Dao實現(xiàn)類的包名
daoModel.setPackageName(DaoConstant.PACKAGE);
//取得接口名
String className=StringUtils.substringBefore(fileName,".");
//設(shè)置接口名
daoModel.setClassName(className);
MapString, Object data = new HashMapString, Object();
data.put("model", daoModel);
//設(shè)置生成的位置
String filePath=new String("src/"+package2path(DaoConstant.PACKAGE)+"/"+fileName);
//代碼生成
return super.generate(DaoConstant.INTERFACE_TEMPLATE, data, filePath);
}
data.put("model", daoModel);由這句代碼可看出我們將可以在模板中直接調(diào)用這些數(shù)據(jù)package ${model.packageName};
public interface ${model.className} extends BaseHibernateDao {
}
將html格式不能轉(zhuǎn)換成java代碼??梢赞D(zhuǎn)換成jsp在myeclipse中運行。
1.新建一個Web項目,把準(zhǔn)備好的tmp.html粘貼到項目的WebRoot文件夾下
2.用MyEclipse JSP Editor方式打開tmp.html文件,在文件首行添加代碼:
%@ page language="java" import="java.util.*" contentType="text/html;charset=GBK" pageEncoding="GBK"%
注:這行指令不能少,language屬性,指定JSP頁面采用的腳步語言;import屬性,可以在JSP文件的腳步片段中引
用外在的類文件;contentType屬性,用來對編碼格式j(luò)inx設(shè)置,這里的“GBK”是中文編碼
3.保存文件,然后把tmp.html重命名成:tmp.jsp
利用freemarker生成JAVA BEAN
Freemarker模板代碼如下:
package ${packageName};
/**
* #if author == "adams" @author adams /#if
*/
pulic class ${className} {
#list attrs as a
private ${a.type} ${a.field};
/#list
#list attrs as a
public void set${a.field?cap_first}(${a.type} ${a.field}){
this.${a.field} = ${a.field};
}
public ${a.type} get${a.field?cap_first}(){
return this.${a.field};
}
/#list
}
Java代碼如下
package com.my.learn.freemarker;
public class Attr{
public String field;
public String type;
public Attr(String field, String type){
this.field = field;
this.type = type;
}
public String getField(){
return this.field;
}
public String getType(){
return this.type;
}
public void setField(String field){
this.field = field;
}
public void setType(String type){
this.type = type;
}
}
package com.my.learn.freemarker;
import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException;
public class FmAppUseage {
public static void main(String[] args){
ListObject list = new ArrayListObject();
list.add(new Attr("username", "String"));
list.add(new Attr("password", "String"));
list.add(new Attr("age", "int"));
list.add(new Attr("hobby", "String"));
MapString,Object root = new HashMapString, Object();
root.put("packageName", "com.my.learn.freemarker");
root.put("className", "User");
root.put("attrs", list);
root.put("author", "adams");
Configuration cfg = new Configuration();
String path = FmAppUseage.class.getResource("/").getPath()+"template";
try {
cfg.setDirectoryForTemplateLoading(new File(path));
Template template = cfg.getTemplate("/demo.ftl");
StringWriter out = new StringWriter();
template.process(root, out);
System.out.println(out.toString());
} catch (IOException e) {
System.out.println("Cause==" + e.getCause());
} catch (TemplateException e) {
System.out.println("Cause==" + e.getCause());
}
}
}
輸出結(jié)果如下:
package com.my.learn.freemarker;
/**
* @author adams
*/
pulic class User {
private String username;
private String password;
private int age;
private String hobby;
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return this.username;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return this.password;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void setHobby(String hobby){
this.hobby = hobby;
}
public String getHobby(){
return this.hobby;
}
}
當(dāng)在筆者剛做測試時,將Attr的類定義在了FmAppUseage類的內(nèi)部,導(dǎo)致不能正常運行,只能將其移除單獨成一個類時,便能正常運行了。 轉(zhuǎn)載僅供參考,版權(quán)屬于原作者。祝你愉快,滿意請采納哦
關(guān)鍵的關(guān)鍵是沒有必要啊,你做好模板,放到服務(wù)器下面,配置好FR自帶的Servlet形式的服務(wù)器,打開瀏覽器訪問就可以了呀。 查看原帖
求采納
用命令提示符來生成應(yīng)用程序
打開命令提示符,輸入javac 源代碼文件路徑\源代碼文件名稱.java ?或 javac -jar?源代碼文件路徑\源代碼文件名稱.jar
然后就可以看見源代碼文件的旁邊多了一個 源代碼文件名稱.class??文件 或?源代碼文件名稱.jar
直接用IDE中的構(gòu)建選項 NetbeansIDE 和 eclipse都是現(xiàn)在很受歡迎的IDE
本文題目:模板代碼生成java 程序開發(fā)模板
標(biāo)題路徑:http://chinadenli.net/article0/dodesio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、服務(wù)器托管、網(wǎng)站排名、網(wǎng)站建設(shè)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)