WEB开发网
开发学院软件开发Java spring 3.0 应用springmvc 构造RESTful URL 详细讲... 阅读

spring 3.0 应用springmvc 构造RESTful URL 详细讲解

 2009-09-21 00:00:00 来源:WEB开发网   
核心提示: 3. Controller编写Java代码/***@RequestMapping("/userinfo")具有层次关系,方法级的将在类一级@RequestMapping之一,*如下面示例,访问方法级别的@RequestMapping("/new"),则UR

3. Controller编写

Java代码   

/** 
 * @RequestMapping("/userinfo") 具有层次关系,方法级的将在类一级@RequestMapping之一, 
 * 如下面示例, 访问方法级别的@RequestMapping("/new"),则URL为 /userinfo/new 
 */ 
@Controller 
@RequestMapping("/userinfo") 
public class UserInfoController extends BaseSpringController{ 
 //默认多列排序,example: username desc,createTime asc 
 protected static final String DEFAULT_SORT_COLUMNS = null; 
 
 private UserInfoManager userInfoManager; 
 
 private final String LIST_ACTION = "redirect:/userinfo"; 
 
 /** 
 * 通过spring自动注入 
 **/ 
 public void setUserInfoManager(UserInfoManager manager) { 
 this.userInfoManager = manager; 
 } 
 
 /** 列表 */ 
 @RequestMapping 
 public ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) { 
 PageRequest<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS); 
 //pageRequest.getFilters(); //add custom filters 
  
 Page page = this.userInfoManager.findByPageRequest(pageRequest); 
 savePage(page,pageRequest,request); 
 return new ModelAndView("/userinfo/list","userInfo",userInfo); 
 } 
 
 /** 进入新增 */ 
 @RequestMapping(value="/new") 
 public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception { 
 return new ModelAndView("/userinfo/new","userInfo",userInfo); 
 } 
 
 /** 显示 */ 
 @RequestMapping(value="/{id}") 
 public ModelAndView show(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception { 
 UserInfo userInfo = (UserInfo)userInfoManager.getById(id); 
 return new ModelAndView("/userinfo/show","userInfo",userInfo); 
 } 
 
 /** 编辑 */ 
 @RequestMapping(value="/{id}/edit") 
 public ModelAndView edit(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception { 
 UserInfo userInfo = (UserInfo)userInfoManager.getById(id); 
 return new ModelAndView("/userinfo/edit","userInfo",userInfo); 
 } 
 
 /** 保存新增 */ 
 @RequestMapping(method=RequestMethod.POST) 
 public ModelAndView create(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception { 
 userInfoManager.save(userInfo); 
 return new ModelAndView(LIST_ACTION); 
 } 
 
 /** 保存更新 */ 
 @RequestMapping(value="/{id}",method=RequestMethod.PUT) 
 public ModelAndView update(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception { 
 UserInfo userInfo = (UserInfo)userInfoManager.getById(id); 
 bind(request,userInfo); 
 userInfoManager.update(userInfo); 
 return new ModelAndView(LIST_ACTION); 
 } 
 
 /** 删除 */ 
 @RequestMapping(value="/{id}",method=RequestMethod.DELETE) 
 public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) { 
 userInfoManager.removeById(id); 
 return new ModelAndView(LIST_ACTION); 
 } 
 
 /** 批量删除 */ 
 @RequestMapping(method=RequestMethod.DELETE) 
 public ModelAndView batchDelete(HttpServletRequest request,HttpServletResponse response) { 
 String[] items = request.getParameterValues("items"); 
 for(int i = 0; i < items.length; i++) { 
  java.lang.Long id = new java.lang.Long(items[i]); 
  userInfoManager.removeById(id); 
 } 
 return new ModelAndView(LIST_ACTION); 
 } 
 
}

上一页  1 2 3 4 5  下一页

Tags:spring 应用 springmvc

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接