SpringMvc---控制层响应数据到jsp页面

1、先放上接收数据的jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  原生的:${type}
  Hello ${requestScope.type}.
  Hello ${sessionScope.type}.
  </body>
</html>

2、使用原生的servletAPI

原生的servletAPI的这种方式只能通过原生的获取:${type}
需要先引入servlet的包
File->Project Structure->Modules->Dependencies->library
将tomcat导入进来,如下图所示:

在这里插入图片描述

@RequestMapping("servletAPI")
public String servletAPI(HttpServletRequest request){
    request.setAttribute("type","servletAPI");
    return "main";
}

3、使用Model

@RequestMapping("/model")
public String model(Model model){
    model.addAttribute("type","model");
    return "main";
}

4、使用ModelMap

@RequestMapping("/modelMap")
public String model(ModelMap modelMap){
    modelMap.addAttribute("type","modelMap");
    return "main";
}

5、使用Map

@RequestMapping("/map")
public String map(Map map){
    map.put("type","map");
    return "main";
}

注:Model、ModelMap、Map底层都是输送的一个BindingAwareModelMap去给页面传值的。

6、使用ModelAndView

@RequestMapping("/modelAndView")
public ModelAndView map(){
    ModelAndView modelAndView = new ModelAndView("main");
    modelAndView.addObject("type","modelMap");
    return modelAndView;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值