EditViewGenerator.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.izouma.codegenerator;
  2. import com.izouma.awesomeadmin.model.GenCode;
  3. import org.apache.velocity.Template;
  4. import org.apache.velocity.VelocityContext;
  5. import org.apache.velocity.app.Velocity;
  6. import org.apache.velocity.tools.ToolManager;
  7. import org.hibernate.validator.internal.util.privilegedactions.GetResource;
  8. import java.io.BufferedWriter;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.OutputStreamWriter;
  12. import java.util.HashSet;
  13. import java.util.Properties;
  14. import java.util.Set;
  15. public class EditViewGenerator {
  16. public static void GenEditView(GenCode model) {
  17. try {
  18. Set<String> imports = new HashSet<>();
  19. String templatePath = GetResource.class.getClassLoader().getResource("templates").getPath();
  20. Properties pro = new Properties();
  21. pro.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
  22. pro.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, templatePath);
  23. pro.setProperty("directive.foreach.counter.name", "velocityCount");
  24. pro.setProperty("directive.foreach.counter.initial.value", "1");
  25. Velocity.init(pro);
  26. ToolManager manager = new ToolManager(true, true);
  27. VelocityContext context = new VelocityContext(manager.createContext());
  28. context.put("imports", imports);
  29. context.put("model", model);
  30. Template t = Velocity.getTemplate("FormTemplate.vm");
  31. String targetFile = model.getClassName() + ".vue";
  32. File file = new File(model.getViewPath(), targetFile);
  33. if (!file.getParentFile().exists())
  34. file.getParentFile().mkdirs();
  35. if (!file.exists())
  36. file.createNewFile();
  37. FileOutputStream outStream = new FileOutputStream(file);
  38. OutputStreamWriter writer = new OutputStreamWriter(outStream,
  39. "UTF-8");
  40. BufferedWriter sw = new BufferedWriter(writer);
  41. t.merge(context, sw);
  42. sw.flush();
  43. sw.close();
  44. outStream.close();
  45. System.out.println("成功生成表单页:" + file.getAbsolutePath());
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }