zip包,然后自動下載下來
我們提供的服務有:成都做網站、成都網站設計、成都外貿網站建設、微信公眾號開發、網站優化、網站認證、費縣ssl等。為上1000+企事業單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的費縣網站制作公司
1.預先定義好模板
2.界面輸入相關參數
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
* 創建人:Harries
* 創建時間: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(); //表前綴轉大寫
String zindext = pd.getString(“zindex”); //屬性總數
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(); //創建數據模型
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()); //當前日期
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數據庫腳本/”+tabletop+objectName.toUpperCase()+”.sql”, filePath, ftlPath);
Freemarker.printFile(“oracle_SQL_Template.ftl”, root, “oracle數據庫腳本/”+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進行代碼生成或頁面的靜態生成。 現在簡單的說一下怎樣使用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()){
//如何代碼已存在不重復生成
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 是模板所需要的數據,我們可以通過面向對像的方法把數據存在模板中public boolean genDaoInterface(String fileName){
DaoModel daoModel=new DaoModel();
//設置Dao實現類的包名
daoModel.setPackageName(DaoConstant.PACKAGE);
//取得接口名
String className=StringUtils.substringBefore(fileName,".");
//設置接口名
daoModel.setClassName(className);
MapString, Object data = new HashMapString, Object();
data.put("model", daoModel);
//設置生成的位置
String filePath=new String("src/"+package2path(DaoConstant.PACKAGE)+"/"+fileName);
//代碼生成
return super.generate(DaoConstant.INTERFACE_TEMPLATE, data, filePath);
}
data.put("model", daoModel);由這句代碼可看出我們將可以在模板中直接調用這些數據package ${model.packageName};
public interface ${model.className} extends BaseHibernateDao {
}
將html格式不能轉換成java代碼。可以轉換成jsp在myeclipse中運行。
1.新建一個Web項目,把準備好的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屬性,用來對編碼格式jinx設置,這里的“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());
}
}
}
輸出結果如下:
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;
}
}
當在筆者剛做測試時,將Attr的類定義在了FmAppUseage類的內部,導致不能正常運行,只能將其移除單獨成一個類時,便能正常運行了。 轉載僅供參考,版權屬于原作者。祝你愉快,滿意請采納哦
關鍵的關鍵是沒有必要啊,你做好模板,放到服務器下面,配置好FR自帶的Servlet形式的服務器,打開瀏覽器訪問就可以了呀。 查看原帖
求采納
用命令提示符來生成應用程序
打開命令提示符,輸入javac 源代碼文件路徑\源代碼文件名稱.java ?或 javac -jar?源代碼文件路徑\源代碼文件名稱.jar
然后就可以看見源代碼文件的旁邊多了一個 源代碼文件名稱.class??文件 或?源代碼文件名稱.jar
直接用IDE中的構建選項 NetbeansIDE 和 eclipse都是現在很受歡迎的IDE
本文題目:模板代碼生成java 程序開發模板
標題路徑:http://m.kartarina.com/article0/dodesio.html
成都網站建設公司_創新互聯,為您提供品牌網站建設、手機網站建設、服務器托管、網站排名、網站建設、搜索引擎優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯