Spring MVC学习之——数据传递(在页面上添加数据)

Spring MVC之——数据传递

1.ModelAndView传递

  • 编写controller

    @Controller
    @RequestMapping("/account")
    public class AccountController {
    
        //也可以不创建ModelAndView,直接在参数中指定
        @RequestMapping(value = "/findAccount9")
        public ModelAndView findAccount9(ModelAndView mv) {
            mv.addObject("msg", "欢迎你 springmvc");
            mv.setViewName("success");
            return mv;
        }
    }
    
  • 在index.jsp里面定义超链接

    <a href="/account/findAccount9">ModelAndView参数传递a>
    

Model传递

  • 编写controller

    @Controller
    @RequestMapping("/account")
    public class AccountController {
    
        @RequestMapping(value = "/findAccount10")
        public String findAccount10(Model model) {
            model.addAttribute("msg", "欢迎你 springmvc");
            return "success";
        }
    }
    
  • 在index.jsp里面定义超链接

    <a href="/account/findAccount10">Model参数传递a>
    

ServletAPI传递

  • 编写controller

    @Controller
    @RequestMapping("/account")
    public class AccountController {
    
        @RequestMapping("/findAccount11")
        public String findAccount11(HttpServletRequest request, 
                                    HttpServletResponse response){
            request.setAttribute("msg","欢迎你 springmvc");
            return "success";
        }
    }
    
  • 在index.jsp里面定义超链接

    <a href="/account/findAccount11">ServletAPI传递a>
    

你可能感兴趣的:(Spring,MVC,spring,mvc,学习)