Commit fa5d5b6a by fsn

重构模板

parent 1f2ca6dd
package ${packageName}; package ${controller.packageName};
@for(import in imports!){ <% for(import in controller.imports!){ %>
import ${import}; import ${import};
@} <% } %>
/** /**
* ${bizChName}控制器 * ${context.bizChName}控制器
* *
* \@author fengshuonan * @author fengshuonan
* \@Date ${tool.currentTime()} * @Date ${tool.currentTime()}
*/ */
\@Controller @Controller
\@RequestMapping("/${bizEnName}") @RequestMapping("/${context.bizEnName}")
public class ${tool.firstLetterToUpper(bizEnName)}Controller extends BaseController { public class ${tool.firstLetterToUpper(context.bizEnName)}Controller extends BaseController {
private String PREFIX = "/system/${bizEnName}/"; private String PREFIX = "/system/${context.bizEnName}/";
/** /**
* 跳转到${bizChName}首页 * 跳转到${context.bizChName}首页
*/ */
\@RequestMapping("") @RequestMapping("")
public String index() { public String index() {
return PREFIX + "${bizEnName}.html"; return PREFIX + "${context.bizEnName}.html";
} }
/** /**
* 跳转到添加${bizChName} * 跳转到添加${context.bizChName}
*/ */
\@RequestMapping("/${bizEnName}_add") @RequestMapping("/${context.bizEnName}_add")
public String ${bizEnName}Add() { public String ${context.bizEnName}Add() {
return PREFIX + "${bizEnName}_add.html"; return PREFIX + "${context.bizEnName}_add.html";
} }
/** /**
* 跳转到修改${bizChName} * 跳转到修改${context.bizChName}
*/ */
\@RequestMapping("/${bizEnName}_update") @RequestMapping("/${context.bizEnName}_update")
public String ${bizEnName}Update() { public String ${context.bizEnName}Update() {
return PREFIX + "${bizEnName}_edit.html"; return PREFIX + "${context.bizEnName}_edit.html";
} }
/** /**
* 新增${bizChName} * 新增${context.bizChName}
*/ */
\@RequestMapping(value = "/add") @RequestMapping(value = "/add")
\@ResponseBody @ResponseBody
public Object add() { public Object add() {
return super.SUCCESS_TIP; return super.SUCCESS_TIP;
} }
/** /**
* ${bizChName}详情 * 删除${context.bizChName}
*/ */
\@RequestMapping(value = "/detail") @RequestMapping(value = "/delete")
\@ResponseBody @ResponseBody
public Object detail() { public Object delete() {
return null; return SUCCESS_TIP;
} }
/** /**
* 修改${bizChName} * 修改${context.bizChName}
*/ */
\@RequestMapping(value = "/update") @RequestMapping(value = "/update")
\@ResponseBody @ResponseBody
public Object update() { public Object update() {
return super.SUCCESS_TIP; return super.SUCCESS_TIP;
} }
/** /**
* 删除${bizChName} * ${context.bizChName}详情
*/ */
\@RequestMapping(value = "/delete") @RequestMapping(value = "/detail")
\@ResponseBody @ResponseBody
public Object delete() { public Object detail() {
return SUCCESS_TIP; return null;
} }
} }
package com.stylefeng.guns.core.template.config;
/**
* 全局配置
*
* @author fengshuonan
* @date 2017-05-08 20:21
*/
public class ContextConfig {
private String projectPath = "D:\\ideaSpace\\guns";//模板输出的项目目录
private String bizChName; //业务名称
private String bizEnName; //业务英文名称
public String getBizChName() {
return bizChName;
}
public void setBizChName(String bizChName) {
this.bizChName = bizChName;
}
public String getBizEnName() {
return bizEnName;
}
public void setBizEnName(String bizEnName) {
this.bizEnName = bizEnName;
}
public String getProjectPath() {
return projectPath;
}
public void setProjectPath(String projectPath) {
this.projectPath = projectPath;
}
}
...@@ -11,10 +11,9 @@ import java.util.List; ...@@ -11,10 +11,9 @@ import java.util.List;
*/ */
public class ControllerConfig { public class ControllerConfig {
private String bizChName; private String controllerPathTemplate = "\\src\\main\\java\\com\\stylefeng\\guns\\modular\\system\\controller\\{}Controller.java";
private String bizEnName; private String packageName;//包名称
private String packageName; private List<String> imports;//所引入的包
private List<String> imports;
public ControllerConfig(){ public ControllerConfig(){
init(); init();
...@@ -30,22 +29,6 @@ public class ControllerConfig { ...@@ -30,22 +29,6 @@ public class ControllerConfig {
this.packageName = "com.stylefeng.guns.modular.system.controller"; this.packageName = "com.stylefeng.guns.modular.system.controller";
} }
public String getBizChName() {
return bizChName;
}
public void setBizChName(String bizChName) {
this.bizChName = bizChName;
}
public String getBizEnName() {
return bizEnName;
}
public void setBizEnName(String bizEnName) {
this.bizEnName = bizEnName;
}
public String getPackageName() { public String getPackageName() {
return packageName; return packageName;
} }
...@@ -61,4 +44,12 @@ public class ControllerConfig { ...@@ -61,4 +44,12 @@ public class ControllerConfig {
public void setImports(List<String> imports) { public void setImports(List<String> imports) {
this.imports = imports; this.imports = imports;
} }
public String getControllerPathTemplate() {
return controllerPathTemplate;
}
public void setControllerPathTemplate(String controllerPathTemplate) {
this.controllerPathTemplate = controllerPathTemplate;
}
} }
package com.stylefeng.guns.core.template.engine;
import com.stylefeng.guns.core.template.config.ContextConfig;
import com.stylefeng.guns.core.template.config.ControllerConfig;
/**
* 模板生成父类
*
* @author fengshuonan
* @date 2017-05-08 20:17
*/
public class AbstractTemplateEngine {
private ContextConfig contextConfig = new ContextConfig(); //全局配置
private ControllerConfig controllerConfig = new ControllerConfig(); //控制器的配置
public ContextConfig getContextConfig() {
return contextConfig;
}
public void setContextConfig(ContextConfig contextConfig) {
this.contextConfig = contextConfig;
}
public ControllerConfig getControllerConfig() {
return controllerConfig;
}
public void setControllerConfig(ControllerConfig controllerConfig) {
this.controllerConfig = controllerConfig;
}
}
package com.stylefeng.guns.core.template.engine;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.core.template.config.ControllerConfig;
import com.stylefeng.guns.core.util.ToolUtil;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* guns项目模板生成 引擎
*
* @author fengshuonan
* @date 2017-05-07 22:15
*/
public class GunsEngine {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.put("RESOURCE.root","");
ControllerConfig controllerConfig = new ControllerConfig();
controllerConfig.setBizChName("测试");
controllerConfig.setBizEnName("test");
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
Configuration cfg = new Configuration(properties);
GroupTemplate groupTemplate = new GroupTemplate(resourceLoader,cfg);
groupTemplate.registerFunctionPackage("tool", new ToolUtil());
Template template = groupTemplate.getTemplate("gunsTemplate/Controller.java.btl");
template.binding(BeanKit.beanToMap(controllerConfig));
String render = template.render();
template.renderTo(new FileOutputStream("D:\\ideaSpace\\guns\\src\\main\\java\\com\\stylefeng\\guns\\modular\\system\\controller\\TestController.java"));
System.out.println(render);
}
}
package com.stylefeng.guns.core.template.engine;
import com.stylefeng.guns.core.template.config.ContextConfig;
import com.stylefeng.guns.core.util.ToolUtil;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* guns项目模板生成 引擎
*
* @author fengshuonan
* @date 2017-05-07 22:15
*/
public class GunsTemplateEngine extends AbstractTemplateEngine {
private GroupTemplate groupTemplate;
public GunsTemplateEngine() {
initBeetlEngine();
}
public void initBeetlEngine() {
Properties properties = new Properties();
properties.put("RESOURCE.root", "");
properties.put("DELIMITER_STATEMENT_START", "<%");
properties.put("DELIMITER_STATEMENT_END", "%>");
Configuration cfg = null;
try {
cfg = new Configuration(properties);
} catch (IOException e) {
e.printStackTrace();
}
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
groupTemplate = new GroupTemplate(resourceLoader, cfg);
groupTemplate.registerFunctionPackage("tool", new ToolUtil());
}
public void start() throws IOException {
//初始化控制器模板
Template template = groupTemplate.getTemplate("gunsTemplate/Controller.java.btl");
template.binding("controller", super.getControllerConfig());
template.binding("context", super.getContextConfig());
String format = ToolUtil.format(super.getContextConfig().getProjectPath() + super.getControllerConfig().getControllerPathTemplate(),
ToolUtil.firstLetterToUpper(super.getContextConfig().getBizEnName()));
template.renderTo(new FileOutputStream(format));
System.out.println("生成控制器成功!");
}
public static void main(String[] args) throws IOException {
ContextConfig contextConfig = new ContextConfig();
contextConfig.setBizChName("测试");
contextConfig.setBizEnName("test");
GunsTemplateEngine gunsTemplateEngine = new GunsTemplateEngine();
gunsTemplateEngine.setContextConfig(contextConfig);
gunsTemplateEngine.start();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment