springmvc跳转页面的几种放方法

来源:互联网 发布:java转义html特殊字符 编辑:程序博客网 时间:2024/06/05 06:59
Springmvc跳转页面的几种方法@Controller//为了对url进行分类管理 ,可以在这里定义根路径,最终访问url是根路径+子路径//比如:商品列表:/items/queryItems.action@RequestMapping("/items")public class ItemsController {@Autowiredprivate ItemsService itemsService;// 商品查询@RequestMapping("/queryItems")//第一种使用ModelAndViewpublic ModelAndView queryItems(HttpServletRequest request) throws Exception {//测试forward后request是否可以共享System.out.println(request.getParameter("id"));// 调用service查找 数据库,查询商品列表List<ItemsCustom> itemsList = itemsService.findItemsList(null);// 返回ModelAndViewModelAndView modelAndView = new ModelAndView();// 相当 于request的setAttribut,在jsp页面中通过itemsList取数据modelAndView.addObject("itemsList", itemsList);// 指定视图// 下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为// modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");// 上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀modelAndView.setViewName("items/itemsList");return modelAndView;}//商品信息修改页面显示//@RequestMapping("/editItems")//限制http请求方法,可以post和get//@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})//public ModelAndView editItems()throws Exception {//////调用service根据商品id查询商品信息//ItemsCustom itemsCustom = itemsService.findItemsById(1);////// 返回ModelAndView//ModelAndView modelAndView = new ModelAndView();//////将商品信息放到model//modelAndView.addObject("itemsCustom", itemsCustom);//////商品修改页面//modelAndView.setViewName("items/editItems");////return modelAndView;//}@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})//@RequestParam里边指定request传入参数名称和形参进行绑定。//通过required属性指定参数是否必须要传入//通过defaultValue可以设置默认值,如果id参数没有传入,将默认值和形参绑定。//通过继承Modelpublic String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception {//调用service根据商品id查询商品信息ItemsCustom itemsCustom = itemsService.findItemsById(items_id);//通过形参中的model将model数据传到页面//相当于modelAndView.addObject方法model.addAttribute("itemsCustom", itemsCustom);return "items/editItems";}//商品信息修改提交@RequestMapping("/editItemsSubmit")//第三种.使用直接跳转,但是不能传递需要参数public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom)throws Exception {//调用service更新商品信息,页面需要将商品信息传到此方法itemsService.updateItems(id, itemsCustom);//重定向到商品查询列表//return "redirect:queryItems.action";//页面转发//return "forward:queryItems.action";return "success";}}

原创粉丝点击