SpringMVC架構之響應頁面的五種方式
- 上一篇我為大家講解到了使用SpringMVC架構接收頁面資料的四種方式。本篇呢,我就為大家講解一下SpringMVC架構如何響應頁面。
- 第一種方式使用modelandview實作。代碼如下
//第一種,使用modelandview實作
@RequestMapping(value = "/mavSelet")
public ModelAndView selectAll(){
modelAndView.addObject("list",123);
modelAndView.setViewName("index01.jsp");
return modelAndView;
}
- 第二種方式使用httpservletrrequest接口設定域的方式響應頁面的方式
//方式二,使用httpservletrrequest接口設定域的方式響應頁面的方式
@RequestMapping(value = "/requestS")
public String selectAll01(HttpServletRequest request){
request.setAttribute("list",123);
//傳回到index01.jsp頁面
return "index01.jsp";
}
- 第三種方式 使用model,将資料放在model中
//方式三,使用model,将資料放在model中
@RequestMapping(value = "/model")
public String selectAll02(Model model){
model.addAttribute("list",123);
return "index01.jsp";
}
//方式四,使用map,将資料放在map中
@RequestMapping("/hashmap")
public String selectAll03(HashMap<String, String> hashMap){
hashMap.put("list", "123");
return "index01.jsp";
}
//第五種使用modelmap方式
@RequestMapping(value = "/modelmap")
public String selectAll04(ModelMap modelMap){
modelMap.addAttribute("list",123);
return "index01.jsp" ;
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>${list}</h1>
</body>
</html>
- 使用EL表達式
- 以上就是SpringMVC響應頁面的五種方式