目录
我们可以通过控制器方法的返回值设置跳转的视图。控制器支持如void,String,ModelAndView类型。
返回值是void会跳转到名字是前缀(也就是你前面编写的视图解析器中的)+方法路径名+后缀的jsp页面
@RequestMapping("/test") public void t1(){ System.out.println("hello!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); }路径是test,方法执行完毕以后会跳转到test.jsp页面中(前提是配置好了视图解析器,不然可能会报错)
此时跳转到的的是前缀+返回值+后缀的jsp页面。
@RequestMapping("/c2/h1") public String t2(){ System.out.println("90"); return "test"; }
该对象是SpringMvc提供的对象,它可以向request域设置数据并指定跳转的页面。该对象包含Model对象和View对象。
Model:向request域中设置数据。
View:指定跳转的页面
- @RequestMapping("/c2/h1")
- public ModelAndView t3(){
- //创建ModelAndView对象
- ModelAndView modelAndView=new ModelAndView();
- //获取Model对象,本质上是一个Map
- Map
model=modelAndView.getModel(); - //使用Model对象向request域设置数据
- model.put("name","大大大大大牛");
- //使用View对象设置跳转的路径为/test.jsp
- modelAndView.setViewName("test");
- return modelAndView;
- }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Titletitle> head> <body> <h1>request=${requestScope.name}h1> body> html>这里的${requestScope.name}是el表达式写法,所以在web.xml中要添加支持el表达式
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> web-app>