SpringMVC后端向前端返回数据的方式

1、通过request对象返回数据:

后台:

@RequestMapping("/test1")
    public String test(HttpServletRequest request){
        String str1 = "HelloWorld!";
        request.setAttribute("str1",str1);
        return "";
    }

前台获取数据:

${requestScope.str1}

2、通过ModelAndView对象返回数据:

后台:

@RequestMapping("/test2")
    public ModelAndView test2(){
        String str2 = "Hello!!";
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("str2",str2);
        modelAndView.setViewName("url");//这里的url是服务器内地址,不能是任意地址;
        return modelAndView;
    }

前台获取数据:

${requestScope.str2}

3、通过ModelMap对象返回数据:

后台: 

@RequestMapping(value="/test3")
	public String test3(ModelMap map) 
	{
		String str3 = "hello!!!";
		map.addAttribute("str3", str3);
		map.put("str3", str3);
        //以上两种方式选一种即可
		return "";	
	}

前台获取数据:

${requestScope.str3}

猜你喜欢

转载自blog.csdn.net/qq_40181063/article/details/87972196