【问题标题】:find/create a tool to generate java sources(model/controller/dao) in spring mvc在spring mvc中查找/创建一个工具来生成java源(model/controller/dao)
【发布时间】:2012-09-04 08:17:30
【问题描述】:

我现在使用的是 spring mvc3,我发现我的大部分控制器都拥有相同的逻辑。例如:

后控制器:

package com.king.controller;

@Controller
@RequestMapping("/posts")
public class PostController {
    @Autowired
    private PostDao postDao;

    // GET /posts /posts.json
    @RequestMapping(value = { "" }, method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("posts", postDao.list());
        return "posts/index";
    }

    // GET /posts/1 GET /posts/1.json
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String show(@PathVariable int id, Model model) {
        Post post = postDao.query(id);
        if (post != null) {
            model.addAttribute("post", post);
            return "posts/show";
        } else
            throw new RuntimeException("not found");

    }

    // GET /posts/new
    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String newForm(Model model) {
        model.addAttribute(new Post());
        return "posts/new";
    }

    // GET /posts/1/edit
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public String edit(@PathVariable int id, Model model) {
        Post post = postDao.query(id);
        if (post != null) {
            model.addAttribute(post);
            return "posts/edit";
        } else
            throw new RuntimeException("not found");
    }

    // POST /posts /posts.json
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String create(@Valid Post post, BindingResult result) {
        if (!result.hasErrors()) {
            postDao.add(post);
            return "redirect:/posts";
        } else {
            return "posts/new";
        }
    }

    // PUT /posts/1 /posts/1.json
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String update(@PathVariable int id, @Valid Post post, BindingResult result, RedirectAttributes redirectAttrs) {
        if (!result.hasErrors()) {
            post.setId(id);
            postDao.update(post);
            return "redirect:/posts/" + id;
        } else {
            return "posts/edit";
        }
    }

    // DELETE /posts/1 /posts/1.json
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String destroy(@PathVariable int id, RedirectAttributes re) {
        Post p = new Post(id, "", "");
        postDao.delete(p);
        return "redirect:/posts";
    }
}

如果我创建一个需要 curd 操作的新简单模型,我将不得不创建一个新控制器,一个手动复制粘贴的新 dao。

所以我想知道我是否可以找到或创建一个可以像 rails 一样生成控制器和 dao 等的工具?

有什么工具可以开箱即用吗?如果没有,我可以创建它,但是现在,我只是想如果我必须创建这种工具,我可能只是做一些字符替换,也就是说,为控制器和 dao 创建一个公共模板,然后替换一些东西因此,但我想知道如何处理包/导入/问题,以及哪种语言更好(java 或 ruby​​)?

顺便说一句,请不要为我推荐 play!framework。我不喜欢它。因为我只想使用spring mvc。所以该工具将只关注基于spring 3的控制器和dao。

有什么建议吗?

【问题讨论】:

  • 不是答案,但您可以将 GenericDAO 用于 DAO 层。在我看来,像脚手架这样的解决方案不适合生产环境。

标签: spring-mvc code-generation


【解决方案1】:

Telosys (http://www.telosys.org/) 是专为此类工作设计的轻量级代码生成器。

它可以生成所有控制器、JSP、DAO 等(以及更多)。

原理很简单:为您定义的每个实体应用相同的模板。 所有模板都是可定制的(基于 Velocity 模板引擎的“.vm”文件),因此您可以拥有您想要生成的内容(代码结构、cmets 等)

此外,Spring MVC 的模板包已经可用,您可以从 GitHub 安装它:https://github.com/telosys-templates-v3/java7-web-mvc-spring-T300

更多信息请看这篇文章https://modeling-languages.com/telosys-tools-the-concept-of-lightweight-model-for-code-generation/

【讨论】:

    【解决方案2】:

    Grails 确实提供了这种开箱即用的功能,而我相信 Spring Roo 也会为您做到这一点。两者都值得一试。

    【讨论】:

    • 看来spring roo会使用maven。我们离线工作。
    • 完全离线?您不能使用 Nexus 的实例来管理工件下载,这意味着只有那台机器需要连接到网络?
    • 是的,完全离线。现在我在指定的可以在另一个房间访问网络的计算机上回复你。
    • 一旦你生成了所有必要的代码,你就可以随时取消和'unrooize'你的项目。
    • 非mavenizing 方法是一种可行的方法,但这样会失去好处。如果你有一台可以访问这个站点的机器,它就可以访问网络。在上面设置一个 Nexus 实例,然后其他机器可以简单地连接到该机器。它存储下载的工件,因此每个工件只从网上下载一次。
    【解决方案3】:

    也检查一下。

    http://www.myeclipseide.com/documentation/quickstarts/ME4STutorialScaffoldingMVC/scaffoldingarticle.html

    这将引导您生成一个可立即运行的 Spring MVC 应用程序,该应用程序为域模型实现 CRUD 应用程序模式。

    免责声明:这是一个 30 天试用版

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多