在spring mvc下实现提交后重定向,比webwork/struts2下要稍微麻烦一些。一般post提交后,都会有重定向,而且可能重定向到被修改对象的详细页面上。这样至少需要传递一个id,比如:
这里代码是继承SimpleFormController类的片段:
@Override protected ModelAndView onSubmit(Object command) throws Exception { Product product = (Product) command; this.productDao.saveOrUpdate(product); ModelAndView modelAndView = new ModelAndView( this.getSuccessView(), "id", product.getId()); return modelAndView; }
配置文件片段:
< bean name ="/save.htm" class ="product.SaveProductController" > < property name ="formView" value ="input" /> < property name ="successView" value ="redirect:/browse.htm" />
这样既做到重定向传递参数,又让uri是通过配置文件而不是写在代码中。