The return value of the JAVA framework springmvc controller

1. Return value: ModelView object.

Use modelAndView.setViewName to set the returned page. Use modelAndView.addObject to set the returned data.

1     @RequestMapping("/edit")
2     public ModelAndView editTable(HttpServletRequest request){
3         ModelAndView modelAndView=new ModelAndView();
4         String id=request.getParameter("id");
5         goods goodser= this.goodsService.findByIdSer(Integer.parseInt(id));
6         modelAndView.setViewName("edit");
7         modelAndView.addObject("goods",goodser);
8         returnmodelAndView;
9      }

 Second, the return value: string

String is the page name stripped of extensions

1     @RequestMapping("/edit")
2     public String editTable(HttpServletRequest request,Model model){
3         String id=request.getParameter("id");
4         goods goodser= this.goodsService.findByIdSer(Integer.parseInt(id));
5         model.addAttribute("goods",goodser);
6         return "edit";
7     }

3. Return value: void

Determine the set return value through the parameter httpservletrequest, set the value and forward through the request.

1     @RequestMapping("/edit")
2     public void editTable(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
3         String id=request.getParameter("id");
4         goods goodser= this.goodsService.findByIdSer(Integer.parseInt(id));
5         request.setAttribute("gooods",goodser);
6         request.getRequestDispatcher("WEB-INF/jsp/success.jsp").forward(request,response);
7     }

 Four: forwarding

Forward through return forward:*.action, and set data through Model. Absolute paths are those with /

1     @RequestMapping("/update")
2     public String upddateById(goods goods,Model model){
3         this.goodsService.updateById(goods);
4         model.addAttribute("id",goods.getId());
5     return  "forward:edit.action";
6     }

Absolute path writing: Absolute paths and relative paths are applied according to requirements.

 

1    @RequestMapping("/update")
2     public String upddateById(goods goods,Model model){
3         this.goodsService.updateById(goods);
4         model.addAttribute("id",goods.getId());
5     return  "redirect:/goods/edit.action";
6     }

 

 

 

Note that you need to bring the extension .action here

The url has not changed.

5. Redirect:

Set the data through the Model by returning " redirect :*.action"  . Absolute paths are those with /.

1     @RequestMapping("/update")
2     public String upddateById(goods goods,Model model){
3         this.goodsService.updateById(goods);
4         model.addAttribute("id",goods.getId());
5     return  "redirect:edit.action";
6     }

 url changed.

The bottom layer of Model also sets attributes through request.setAttribute, but better encapsulates the request, and the value can be set whether it is forwarding or redirecting.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325168190&siteId=291194637