Spring MVC CRUD控制器最佳实践

Hen*_* N. 5 spring-mvc

我试图找到一个Spring MVC简单CRUD控制器的最佳实践方法.网络和论坛上有很多CRUD控制器的例子,但大多数都有两个问题之一:

  1. 保存/或更新或删除后,它们会显示发生保存和更新的消息,但是他们正在点击的网址仍然包含"/ update/{id}"或"/ delete/{id}".这是"错误的",因为显示的内容通常是对象列表.

要么

  1. 控制器重定向到"showAll"视图,但是没有消息表明发生了动作,这不是用户友好的.

有没有人有一个没有这两个问题的crud控制器的例子?

谢谢,

亨利

@Controller
@RequestMapping(value="/role")
public class RoleController {
    private static final Logger log = Logger.getLogger(RoleController.class);

    @Autowired
    private RoleValidator validator = null;
    @Autowired
    private RoleService service = null;


    public void setService(RoleService  service) {
        this.service = service;
    }

    public void setValidator(RoleValidator validator) {
        this.validator = validator;
    }


    @RequestMapping(method=RequestMethod.GET)
    public String showForm(ModelMap model){
        List<Role> domainObjectList = service.getRoles();
        model.addAttribute("domainObjectList", domainObjectList);
        return "role";
    }

    @RequestMapping(value="/add", method=RequestMethod.GET)
    public String preAdd(ModelMap model){
        Role domainObject = new Role();
        model.addAttribute("domainObject", domainObject);
        addConstrainedFields(model);
        return "roleEdit";
    }

    @RequestMapping(value="/add", method=RequestMethod.POST)
    public ModelAndView add(@ModelAttribute(value="domainObject") Role domainObject, BindingResult result) {
        validator.validate(domainObject, result);
        ModelAndView mv = new ModelAndView("role");
        if(result.hasErrors()){
            mv = new ModelAndView("roleEdit");
            mv.addObject("domainObject", domainObject);
            return mv;
        }
        service.insertRole( domainObject );
        mv.addObject("domainObjectList", service.getRoles());
        mv.addObject("messageKey","label.form.item.added");
        //PROBLEM: the URL will remain "/add", but the content will be one of showing all roles + message that role was added. 
        return mv;
    }

    @RequestMapping(value="/update/{id}")
    public String preUpdate(@PathVariable Integer id, ModelMap model) {
        Role domainObject = service.getRole( id );
        model.addAttribute("domainObject", domainObject);
        return "roleEdit";
    }


    @RequestMapping(value="/update", method=RequestMethod.POST)
    public String update(@ModelAttribute(value="domainObject") Role domainObject, ModelMap model, BindingResult result){
        validator.validate(domainObject, result);
        ModelAndView mv = new ModelAndView("role");
        if(result.hasErrors()){        
            model.addAttribute("domainObject", domainObject);
            return "roleEdit";
        }
        service.insertRole(domainObject);
        model.addAttribute("messageKey","label.form.item.added");
        model.addAttribute("domainObjectList", service.getRoles());
        //PROBLEM: the message that the object was updated will be lost, but the URL will be /role and we will show all roles.
        return "redirect:/role";
    }

    @RequestMapping(value="/delete/{id}")
    public String delete(@PathVariable Integer id, ModelMap model) {
        Role domainObject = service.getRole( id );
        if (domainObject == null) {
            model.addAttribute("messageKey","label.form.item.notfound");
            return showForm(model);
        }
        service.deleteRole(domainObject);
        model.addAttribute("messageKey","label.form.item.deleted");
        return showForm(model);
    }

    @RequestMapping(value="/delete", method=RequestMethod.POST)
    public ModelAndView delete(@ModelAttribute(value="domainObject") Role domainObject, BindingResult result){
        validator.validate(domainObject, result);
        ModelAndView mv = new ModelAndView("role");
        if(!result.hasErrors()){
            service.deleteRole(domainObject);
            mv.addObject("messageKey","label.form.item.deleted");
            domainObject = new Role();
            mv.addObject("domainObject", domainObject);
        }
        mv.addObject("domainObjectList", service.getRoles());
        return mv;
    }

}
Run Code Online (Sandbox Code Playgroud)

Nei*_*gan 10

  1. 您应该使用RESTful URL设计,并使用gEt到rEad,posT到creaTe,pUt到Update,以及Delete to Delete.对不使用PUT或DELETE的用户代理使用HiddenHttpMethodFilter.

  2. 使用Post-Redirect-Get模式可以避免重新发布POST.

  3. 使用Flash属性在后续页面上显示成功/失败消息.