I am using spring mvc3 now,and I found that most of my controller own the same logic. For exmaple:
PostController:
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";
}
}
If I create a new simple model which need the curd operation,I will have to create a new controller,a new dao which is copy-paste manually.
So I wonder if I can find or create a tool which can generate controller and dao and etc accordingly like rails does?
Is there any tool I can use out-of-box? If not,I can create it,however by now,I just thought that if I have to create this kind of tool,I may just do something character replacement,that's to say,create a public template for controller and dao,then replace something accordingly,but I wonder how to handle the package/import/ problem,and which language is better (java or ruby)?
BTW,please do not recommend play!framework for me. I do not like it. Since I just want to use spring mvc. So the tool will only focus on controller and dao based on spring 3.
Any suggestion?
Grails certainly provides this type of functionality out of the box, while I believe Spring Roo also does this for you. It's worth checking both out.