深圳網(wǎng)站建設(shè)找哪家好中視頻自媒體平臺(tái)注冊(cè)
五、域?qū)ο蠊蚕頂?shù)據(jù)
0、三個(gè)域?qū)ο蠓秶?/h3>
request:一次請(qǐng)求 第1~6都是向request共享
session:一次會(huì)話(瀏覽器開啟到瀏覽器關(guān)閉,與服務(wù)器關(guān)閉無關(guān),session有鈍化和活化操作,可以持久化數(shù)據(jù))
servletContext:整個(gè)應(yīng)用的范圍(服務(wù)器開啟到服務(wù)器關(guān)閉)
1、使用ServletAPI向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){request.setAttribute("testScope", "hello,servletAPI");return "success";
}request域內(nèi)數(shù)據(jù)獲取方式:<p th:text="${testRequestScope}"></p>
2、使用ModelAndView向request域?qū)ο蠊蚕頂?shù)據(jù)
這個(gè)很重要,因?yàn)閺脑创a中看,不過你用的1-6哪一種方式,最終都是封裝到modelAndView里面去
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){/*** ModelAndView有Model和View的功能* Model主要用于向請(qǐng)求域共享數(shù)據(jù)* View主要用于設(shè)置視圖,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)*/ModelAndView mav = new ModelAndView();//向請(qǐng)求域共享數(shù)據(jù)mav.addObject("testScope", "hello,ModelAndView");//設(shè)置視圖,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)mav.setViewName("success");return mav;
}
3、使用Model向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModel")
public String testModel(Model model){model.addAttribute("testScope", "hello,Model");return "success";
}
4、使用map向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){map.put("testScope", "hello,Map");return "success";
}
5、使用ModelMap向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){modelMap.addAttribute("testScope", "hello,ModelMap");return "success";
}
6、Model、ModelMap、Map的關(guān)系
Model、ModelMap、Map類型的參數(shù)其實(shí)本質(zhì)上都是 BindingAwareModelMap 類型的
繼承關(guān)系如下圖:

public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
7、控制器方法統(tǒng)一返回ModelAndView對(duì)象
通過打斷點(diǎn)的方式運(yùn)行,調(diào)試界面會(huì)有一個(gè)方法棧,里面的方法直接或間接的調(diào)用了我們打斷點(diǎn)的那一行代碼
在里面找到DispatcherServlet,1061行
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
8、向session域共享數(shù)據(jù)
此處建議使用原生ServletAPI,雖然SpringMVC提供了注解@SessionAttribute,這個(gè)注解可以將共享在請(qǐng)求域的數(shù)據(jù)再共享一份到session中,
@RequestMapping("/testSession")
public String testSession(HttpSession session){session.setAttribute("testSessionScope", "hello,session");return "success";
}獲取session中數(shù)據(jù):
<p th:text="${session.testSessionScope}"></p>
9、向application域共享數(shù)據(jù)
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){ServletContext application = session.getServletContext();application.setAttribute("testApplicationScope", "hello,application");return "success";
}獲取servletContext中數(shù)據(jù)信息:
<p th:text="${application.testApplicationScope}"></p>
六、SpringMVC的視圖
SpringMVC中的視圖是View接口,視圖的作用渲染數(shù)據(jù),將模型Model中的數(shù)據(jù)展示給用戶
SpringMVC視圖的種類很多,默認(rèn)有轉(zhuǎn)發(fā)視圖和重定向視圖
當(dāng)工程引入jstl的依賴,轉(zhuǎn)發(fā)視圖會(huì)自動(dòng)轉(zhuǎn)換為JstlView
若使用的視圖技術(shù)為Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的視圖解析器,由此視圖解析器解析之后所得到的是ThymeleafView
1、ThymeleafView
當(dāng)控制器方法中所設(shè)置的視圖名稱沒有任何前綴時(shí),此時(shí)的視圖名稱會(huì)被SpringMVC配置文件中所配置的視圖解析器解析,視圖名稱拼接視圖前綴和視圖后綴所得到的最終路徑,會(huì)通過轉(zhuǎn)發(fā)的方式實(shí)現(xiàn)跳轉(zhuǎn)
@RequestMapping("/testHello")
public String testHello(){return "hello";
}
2、轉(zhuǎn)發(fā)視圖
SpringMVC中默認(rèn)的轉(zhuǎn)發(fā)視圖是InternalResourceView
SpringMVC中創(chuàng)建轉(zhuǎn)發(fā)視圖的情況:
當(dāng)控制器方法中所設(shè)置的視圖名稱以"forward:"為前綴時(shí),創(chuàng)建InternalResourceView視圖,此時(shí)的視圖名稱不會(huì)被SpringMVC配置文件中所配置的視圖解析器解析,而是會(huì)將前綴"forward:"去掉,剩余部分作為最終路徑通過轉(zhuǎn)發(fā)的方式實(shí)現(xiàn)跳轉(zhuǎn)
例如"forward:/","forward:/employee"
@RequestMapping("/testForward")
public String testForward(){return "forward:/testHello";
}
習(xí)筆記/Spring5/img/img003.png)
3、重定向視圖
SpringMVC中默認(rèn)的重定向視圖是RedirectView
轉(zhuǎn)發(fā)和重定向區(qū)別:
轉(zhuǎn)發(fā)可以獲取請(qǐng)求域的數(shù)據(jù),但是重定向不可以,因?yàn)椴皇且淮握?qǐng)求
轉(zhuǎn)發(fā)可以訪問WEB-INF下面的資源,但是重定向不可以獲取,因?yàn)榘踩詥栴}
轉(zhuǎn)發(fā)不能跨域,但是重定向可以跨域
當(dāng)控制器方法中所設(shè)置的視圖名稱以"redirect:"為前綴時(shí),創(chuàng)建RedirectView視圖,此時(shí)的視圖名稱不會(huì)被SpringMVC配置文件中所配置的視圖解析器解析,而是會(huì)將前綴"redirect:"去掉,剩余部分作為最終路徑通過重定向的方式實(shí)現(xiàn)跳轉(zhuǎn)
例如"redirect:/","redirect:/employee"
@RequestMapping("/testRedirect")
public String testRedirect(){return "redirect:/testHello";
}
習(xí)筆記/Spring5/img/img004.png)
注:
重定向視圖在解析時(shí),會(huì)先將redirect:前綴去掉,然后會(huì)判斷剩余部分是否以/開頭,若是則會(huì)自動(dòng)拼接上下文路徑
4、視圖控制器view-controller
當(dāng)控制器方法中,僅僅用來實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),即只需要設(shè)置視圖名稱時(shí),可以將處理器方法使用view-controller標(biāo)簽進(jìn)行表示
<!--path:設(shè)置處理的請(qǐng)求地址view-name:設(shè)置請(qǐng)求地址所對(duì)應(yīng)的視圖名稱
-->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller><mvc:annotation-driven/>
注:
當(dāng)SpringMVC中設(shè)置任何一個(gè)view-controller時(shí),其他控制器中的請(qǐng)求映射將全部失效,此時(shí)需要在SpringMVC的核心配置文件中設(shè)置開啟mvc注解驅(qū)動(dòng)的標(biāo)簽:
<mvc:annotation-driven />