springmvc中同一个controller中的方法转发到另外一个方法,request域并没有共享,是怎么回事?

飘雪朝晨 2017-11-01 02:46:53

@Controller
@RequestMapping("/items")
public class ItemsController {

@Autowired
private ItemsService itemsService;

// 商品查询
@RequestMapping("/queryItems")
public ModelAndView queryItems(HttpServletRequest request) throws Exception {

// 测试forward后request是否可以共享

System.out.println("forward之后可以共享 id:"+request.getParameter("id"));

List<ItemsCustom> itemsList = itemsService.findItemsList(null);

// 返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
// 相当于request的setAttribute,在jsp页面中可以通过itemsList取数据
modelAndView.addObject("itemsList", itemsList);

// 指定视图
// 下边的路径,如果在我们的视图解析器中配置了jsp路径的前缀和jsp路径的后缀,修改为
modelAndView.setViewName("items/itemsList");
return modelAndView;
}

@RequestMapping(value = "/editItems", method = { RequestMethod.POST,RequestMethod.GET })
public String editItems(Model model,@RequestParam(value = "id", required = true)Integer items_id)
throws Exception {
ItemsCustom itemsCustom = itemsService.findItemsById(items_id);

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 "forward:queryItems.action";
}

}

我在上面editItemsSubmit方法中转发到queryItems.action没问题,但是我调试的时候断点打在第42行,40行中的形参itemsCustom,id和42行中的itemsCustom,id的值都为空这是怎么回事啊?
editItems方法执行完后,itemsCustom和id的值都是有的,但是执行到editItemsSubmit方法时却为空是什么鬼?有没有大佬教教我啊?
...全文
1286 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
飘雪朝晨 2017-11-01
  • 打赏
  • 举报
回复
引用 3 楼 qq_39912309 的回复:
@RequestMapping(value = "/editItems", method = { RequestMethod.POST,RequestMethod.GET })
    public String editItems(Model model,@RequestParam(value = "id", required = true)Integer items_id)
            throws Exception {
        ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
 
        model.addAttribute("itemsCustom", itemsCustom);
        return "items/editItems";
    }
你这个是什么情况?递归调用吗?没看到你的哪个方法转发到editItemsSubmit这个方法啊?
找到问题所在了,因为form标签中enctype属性值的问题,改为application/x-www-form-urlencoded或者不写这个属性就能传过来值了。
飘雪朝晨 2017-11-01
  • 打赏
  • 举报
回复
引用 3 楼 qq_39912309 的回复:
@RequestMapping(value = "/editItems", method = { RequestMethod.POST,RequestMethod.GET })
    public String editItems(Model model,@RequestParam(value = "id", required = true)Integer items_id)
            throws Exception {
        ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
 
        model.addAttribute("itemsCustom", itemsCustom);
        return "items/editItems";
    }
你这个是什么情况?递归调用吗?没看到你的哪个方法转发到editItemsSubmit这个方法啊?
回复你了。。
飘雪朝晨 2017-11-01
  • 打赏
  • 举报
回复
不是啊,这个是查询出来的数据列表后面有个修改按钮,调用的就是这个方法,然后传id参数过来,根据id查询数据库记录,将记录放到model中,返回给editItems.jsp页面上显示啊,这个页面是一条记录的修改页面。,然后这个是个表单,提交按钮就是调用了editItemsSubmit这个方法。
qq_39912309 2017-11-01
  • 打赏
  • 举报
回复
@RequestMapping(value = "/editItems", method = { RequestMethod.POST,RequestMethod.GET })
    public String editItems(Model model,@RequestParam(value = "id", required = true)Integer items_id)
            throws Exception {
        ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
 
        model.addAttribute("itemsCustom", itemsCustom);
        return "items/editItems";
    }
你这个是什么情况?递归调用吗?没看到你的哪个方法转发到editItemsSubmit这个方法啊?
飘雪朝晨 2017-11-01
  • 打赏
  • 举报
回复
飘雪朝晨 2017-11-01
  • 打赏
  • 举报
回复

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧