本文目錄一覽:
java代碼生成器怎麼用
zip包,然後自動下載下來
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」);
}
}
如何用Java代碼段生成四位數字加字母的驗證碼?
不知道你問的是不是生成這種圖片驗證碼?如果只要一個隨機四位數 那這行代碼就夠了(new Random().nextInt(9000) + 1000;),如果是生成頁面圖片驗證碼就是下面的了: //設定 響應模式 resp.setContentType(“image/jpeg”); // 生成令牌環數據; Integer token = new Random().nextInt(9000) + 1000; // 保存令牌環數據到session中 req.getSession().setAttribute(IMAGE_TOKEN_NAME, token); // 生成令牌環圖片 ServletOutputStream out = resp.getOutputStream(); BufferedImage img = new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(Color.YELLOW); g.fillRect(0, 0, img.getWidth(), img.getHeight()); g.setColor(Color.BLUE); g.setFont(new Font(“”, Font.BOLD, 18)); g.drawString(String.valueOf(token), 10, 16); ImageIO.write(img, “jpg”, out); out.close();
下面簡單的介紹他們的功能和用途,執行效率等。每個都有各自的優缺點看你是做甚什麼方面的研究開發用。.net,是網站編程,現在很多都用這個,但是這個語言編程都有統一思路,很好掌握。窒息那個效率不是很高;php 支持跨平台,很容易學會,執行的效率很高;asp是ASP.net的前身,它比較穩定,比.net要弱一點。但是比.net好學。jsp 是網頁編程,這個學習大約一周就能搞定,不過這個得多實踐,不然的話,時間長了,就容易忘記。
我自己做的系統裡面用作驗證碼的JSP的%@page contentType=”image/jpeg;charset=utf-8″%%@page import=”java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*” %%@ page import=”java.io.OutputStream” %html body %! Color getRandColor(int fc,int bc) { Random rd=new Random(); if(fc255) fc=255; if(bc255) bc=255; int red=fc+rd.nextInt(bc-fc); int green=fc+rd.nextInt(bc-fc); int blue=fc+rd.nextInt(bc-fc); return new Color(red,green,blue); } % % Random r=new Random(); response.addHeader(“Pragma”,”No-cache”); response.addHeader(“Cache-Control”,”no-cache”); response.addDateHeader(“expires”,0); int width=90; int height=23; BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics gc=pic.getGraphics(); gc.setColor(getRandColor(200,250)); gc.fillRect(0,0,width,height); String[] rNum ={“0″,”1″,”2″,”3″,”4″,”5″,”6″,”7″,”8″,”9″,”a”,”b”,”c”,”d”,”e”,”f”, “g”,”h”,”i”,”j”,”k”,”l”,”m”,”n”,”o”,”p”,”q”,”r”,”s”,”t”,”u”,”v”,”w”, “x”,”y”,”z”,”A”,”B”,”C”,”D”,”E”,”F”,”G”,”H”,”I”,”J”,”K”,”L”,”M”,”N”, “O”,”P”,”Q”,”R”,”S”,”T”,”U”,”V”,”W”,”X”,”Y”,”Z”}; int[] style = {Font.PLAIN,Font.BOLD,Font.ITALIC,Font.PLAIN+Font.BOLD, Font.BOLD+Font.ITALIC,Font.PLAIN+Font.ITALIC,Font.PLAIN+Font.BOLD+Font.ITALIC}; gc.setColor(Color.WHITE); gc.drawLine(0,30,90,10); gc.setColor(getRandColor(160,200)); for (int i=0;i50;i++) { int x = r.nextInt(width); int y = r.nextInt(height); int xl = r.nextInt(10); int yl = r.nextInt(10); gc.drawLine(x,y,x+xl,y+yl); } gc.setColor(getRandColor(60,150)); String rt = “”; for(int i=0;i4;i++){ String temp = rNum[r.nextInt(62)]; rt = rt+temp; gc.setFont(new Font(“Times New Roman”,style[r.nextInt(7)],15)); gc.drawString(temp,5+i*15+r.nextInt(10),10+r.nextInt(10)); } gc.dispose(); session.setAttribute(“randNum”,rt); OutputStream os=response.getOutputStream(); ImageIO.write(pic,”JPEG”,os); System.out.println(“當前驗證碼為:”+session.getAttribute(“randNum”)); os.flush(); os.close(); os=null; response.flushBuffer(); out.clear(); out = pageContext.pushBody(); % /body/html
怎樣把一個java源代碼做成一個軟體成品?
1.其實就是用一個外部程序 調用java虛擬機運行你的java程序。
2.可以做一個批處理文件,在裡面調用java 虛擬機運行你的java程序。
3.也可以用某種編程語言,像vb ,c 或c++編個程序,生成exe,能調用java虛擬機運行你的程序,很簡單的。
【源代碼】
源代碼(也稱源程序),是指一系列人類可讀的計算機語言指令。 在現代程序語言中,源代碼可以是以書籍或者磁帶的形式出現,但最為常用的格式是文本文件,這種典型格式的目的是為了編譯出計算機程序。
延展閱讀;
Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行複雜的編程。
Java動態生成代碼
可以的,我說說大概思路,很簡單,你自己具體實現吧,把代碼寫給你沒意義的:
1.將你這段字元串輸出到一個文件里,用Java類文件的方式命名。
2.調用外部javac命令將該文件編譯。
3.用類載入器(ClassLoad)動態載入新的class文件並用Class.forName()註冊該類,然後就可以正常使用了。
上面的每一步都能在baidu中找到實現方法,自己發揮吧。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/246260.html