1.在SpringMVC配置文件中配置视图解析器
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/"></property>
<!-- 视图后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
2.SpringMVC默认的视图解析器解析的是jsp视图
a.InternalResourceViewResolver是SpringMVC默认的视图解析器
b.perfix前缀(/表示jsp视图在webapp的根目录下)
c.suffix后缀
3.在SpringMVC中控制器的方法的返回值为void时,表示跳转到方法路径名的视图
此时会跳转到名字是:前缀+方法路径名+后缀
下述代码会跳转到/helloMVC.jsp视图
@RequestMapping("/helloMVC")
public void helloMVC(){
System.out.println("Hello MVC!");
}
4.在SpringMVC中控制器的方法返回值为String类型时,表示跳转到方法返回名的视图
此时会跳转到名字是:前缀+返回值+后缀
下述代码会跳转到/helloMVC.jsp的视图
@RequestMapping("/c/hello1")
public String helloMVC2(){
System.err.println("Hello MVC!");
return "helloMVC";
}
5.在SpringMVC中控制器方法的返回值为ModelAndView时,表示跳转到view指定的视图
a.创建ModelAndView对象
b.获取Model对象,本质时Map类型的集合,Model接口的底层时Map集合
c.使用Map集合下的put方法向request域中设置数据
d.指定跳转视图(前缀+指定视图+.jsp)
e.在视图中可以使用EL表达式获取request域中的数据${requestScope.key}
@RequestMapping("/c2/hello2")
public ModelAndView useMAV(){
System.out.println("Hello ModelAndView");
ModelAndView modelAndView = new ModelAndView();
Map<String, Object> model = modelAndView.getModel();
model.put("name","程序员");
model.put("hobby","JAVA");
modelAndView.setViewName("baizhan");
return modelAndView;
}
6.根据参数向request域中存放数据
a.使用原生的HttpServletRequest存放数据或者使用Model,ModelMap,Map对象向request域中存放数据
b.Model接口的底层就是一个Map集合,向控制器方法中添加Map参数,向Map集合中添加键值对,数据也会存放到request域中
@RequestMapping("/c2/hello3")
public String getRequest(HttpServletRequest request){
request.setAttribute("name","尚学堂");
return "baizhan";
}
@RequestMapping("/c2/hello4")
public String getRequest2(Model model, ModelMap modelMap){
//使用Model类型将数据存放到request域中
// model.addAttribute("name","北京尚学堂");
//使用ModelMap向request域中添加数据
modelMap.addAttribute("name","百战程序员");
return "baizhan";
}
//使用map参数向request域中添加数据 Model接口的底层就是一个Map集合 向Map集合中添加键值对,数据也会存到request域中
@RequestMapping("/c2/hello5")
public String getRequest3(Map map){
map.put("name","北京尚学堂");
return "baizhan";
}
7.向session域中设置数据
a.session作用域只对当前会话生效
b.SpringMVC并没有提供向session域中设置数据的方法,此时我们只能使用原生的HttpSession对象向session域中设置数据
@RequestMapping("/c3/hello")
public String getSession(HttpSession httpSession){
httpSession.setAttribute("address","北京");
return "baizhan";
}
8.向context域中设置数据
向context域中存放数据时,不能直接将ServletContext对象放到方法参数中,应该将HttpSession对象放到方法参数中,根据HttpSession对象获取ServletContext对象
注:context作用域表示在整个应用范围都生效,其生命周期也是相当长的
@RequestMapping("/c3/hello2")
public String getContext(HttpSession httpSession){
ServletContext servletContext = httpSession.getServletContext();
servletContext.setAttribute("age",10);
return "baizhan";
}
9.SpringMVC提供的请求转发和重定向的方式
a.如果我们使用原生的请求转发和重定向是非常麻烦的需要调用httpServletRequest.getRequestDispatcher("视图路径").forward(httpservletRequest,httpServletResponse);//请求转发
httpServletResponse.redirect("视图路径");//重定向
b.当我们使用SpringMVC提供的请求转发和重定向的方式会简单很多:
i:return "forward:视图路径"; //请求转发
ii:return "redirect:视图路径"; 重定向
@RequestMapping("/c5/hello")
public String useFR(HttpServletRequest httpServletRequest){
httpServletRequest.setAttribute("name","尚学堂");
//请求转发
// return "forward:/c5/hello2";
//重定向
return "redirect:/c5/hello2";
}
@RequestMapping("/c5/hello2")
public void useFR2(HttpServletRequest httpServletRequest){
System.out.println("hello");
System.out.println(httpServletRequest.getAttribute("name"));
}