springmvc(六)之Controller类中方法的三种返回值类型

1.方法的返回类型

        springmvc 通过注解实现的 Controller 常用的返回值类型主要有三种:ModelAndView、void、字符串。

1.1返回ModelAndView

  • Controller类方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view

使用构造方法添加视图

private static final String SUCCESS = "success";

@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
	String viewName = SUCCESS;
	//1.使用构造方法添加视图
	ModelAndView modelAndView = new ModelAndView(viewName);
	// 2.添加模型数据
	modelAndView.addObject("time", new Date());
	return modelAndView;
}

1.2返回void

  • 在Controller类方法形参上可以定义request和response,使用request或response指定响应结果

1.2.1request

使用request转向页面,如下:

request.getRequestDispatcher("页面路径").forward(request, response);
@RequestMapping("/testRequest")
public void testRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// 1.向页面传递参数
	request.setAttribute("name", "zzc");
	request.getRequestDispatcher("/WEB-INF/views/success.jsp").forward(request, response);
}

【注意】:如果使用原始的方式做页面跳转,那么必须给定jsp页面的完整路径。

1.2.2response

通过response实现页面重定向,如下:

response.sendRedirect("url")
@RequestMapping("/testResponse")
public void testResponse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// 1.向页面传递参数
	request.setAttribute("name", "zzc");
	response.sendRedirect("testRequest");
}
@RequestMapping("/testResponse")
public void testResponse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	response.sendRedirect("/springmvc-01-helloworld/hello.jsp");
}

springmvc-01-helloworld:项目名

response.sendRedirect(“url”)中的url是Controller中方法映射的url,也可以是WebContent目录下的jsp文件,但不能是WEB-INF目录下对应的jsp。因为此目录下的文件是被保护起来的,不能直接访问。只能通过后台的跳转来间接的访问,所以就需要请求 SpingMVC 来返回相应的页面。

也可以通过response指定响应结果,例如响应json数据如下:

response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");

1.3返回字符串

1.3.1指定逻辑视图名

Controller类方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。

@RequestMapping("/testSimple")
public String testSimple(Integer id) {
	System.out.println(id);
	return SUCCESS;
}

1.3.2redirect重定向

  • Contrller类方法返回结果重定向到一个url地址
  • redirect方式相当于“response.sendRedirect()”,转发后浏览器的地址栏变为转发后的地址,因为转发即执行了一个新的request和response。由于新发起一个request,原来的参数在转发时就不能传递到下一个url
@RequestMapping("/testResponse")
public String testResponse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	return "redirect:testRequest";
}

@RequestMapping("/testRequest")
public void testRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// 1.向页面传递参数
	request.setAttribute("name", "zzc");
	request.getRequestDispatcher("/WEB-INF/views/success.jsp").forward(request, response);
}

1.3.3forward转发

  • Controller类方法执行后继续执行另一个Controller类方法
  • forward方式相当于“request.getRequestDispatcher().forward(request,response)”,转发后浏览器地址栏还是原来的地址。转发并没有执行新的request和response,而是和转发前的请求共用一个request和response。所以转发前请求的参数在转发后仍然可以读取到。
@RequestMapping("/testForward")
public String testForward() { 
	return "forward:/test/testRequest";
}

@RequestMapping("/testRequest")
public void testRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// 1.向页面传递参数
	request.setAttribute("name", "zzc");
	request.getRequestDispatcher("/WEB-INF/views/success.jsp").forward(request, response);
}

2.配置直接转发

可以直接相应转发的页面,无需经过处理器的方法

在springmvc.xml配置文件中,配置一下内容:

<!-- 配置直接转向 -->
<mvc:view-controller path="/success" view-name="success" />

但是这样,会让springmvc中的视图解析器失效
所以还要添加,以下配置:

<mvc:annotation-driven />

所以要配置:

<!-- 配置直接转向 -->
<mvc:view-controller path="/success" view-name="success" />
<mvc:annotation-driven />
发布了78 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Lucky_Boy_Luck/article/details/100103702
今日推荐