Spring MVC 学习笔记 4《请求转发规则》

版权声明:大家好,我是笨笨,笨笨的笨,笨笨的笨,转载请注明出处,谢谢! https://blog.csdn.net/jx520/article/details/87906479

SSM环境是用的这篇搭建好的 SSM框架整合 Spring + SpringMVC + Mybatis

/ssm/src/main/java/com/jerry/ssm/controller/ReturnTest.java

package com.jerry.ssm.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.RedirectView;

@Controller
@Scope("prototype")
@RequestMapping("/return")
public class ReturnTest {
	
	//============================================ 返回字符串 (现在就用这个)=================================================
	/**
	 * 【直接相应】抛弃视图层,直接返回结果给浏览器。
	 * http://localhost/return/rp/笨笨/18
	 */
	@RequestMapping("/rp/{name}/{age}")
	public void resp(HttpServletResponse resp, @PathVariable("name")String name, @PathVariable("age")int age) throws IOException {
		resp.getWriter().write("<a href='http://localhost/return/rp/笨笨/18' target='_self'>直接相应!</a>");
	}
	
	/**
	 * 【转发请求】服务器内部完成,只把最终结果返回。前段看到:总共产生1此请求
	 * http://localhost/return/forward.htm?name=笨笨&age=18
	 */
	@RequestMapping("/forward.htm")
	public String forward(HttpServletRequest req, String name, int age) {
		// 因为是转发,还在同一个 request 所以这里压入的值,index.jsp 能取到。
		req.setAttribute("name", name);
		req.setAttribute("age", age);
		return "forward:/return/index";
	}
	
	@RequestMapping("/index")
	public String index() {
		return "index";
	}
	
	
	/**
	 * 【重定向】甩锅给浏览器,让它另外发请求去。前段看到:总共产生2次请求,表象为地址栏网址发生改变。
	 * http://localhost/return/rd/笨笨/18
	 */
	@RequestMapping("/rd/{name}/{age}")
	public String redirect(RedirectAttributesModelMap model, HttpServletRequest req, @PathVariable("name")String name, @PathVariable("age")int age) {
		// 因为是重定向了,数据放到 request 里 rindex.jsp 收不到。
		// 全放到 session 中,也不合适。因为有些参数只在两次重定向请求之间传递。然后就没用了。放在 session 太浪费。
		
		// SpringMVC 封装了 RedirectAttributesModelMap 它将值放 session 里,但跳转后,它会帮我们清理掉这些值。
		model.addFlashAttribute("name", name);
		model.addFlashAttribute("age", age);
		
		// 对于计数这种要在多次请求见有效的,就可以存 session 里了
		HttpSession session = req.getSession();
		session.setAttribute("count", session.getAttribute("count")== null ? 1 : (int)session.getAttribute("count")+1);
		
		return "redirect:/return/rindex";
	}
	
	/**
	 * 上面重定向到这里
	 */
	@RequestMapping("/rindex")
	public String rindex(){
		return "rindex";
	}
	
	//========================================= 返回视图对象 (学下历史而已:上古)==============================================
	//--------------- 最开始返回 View,用 InternalResourceView, RedirectView 区分转发、重定向 ---------------
	/**
	 * http://localhost/return/rv/笨笨/18
	 */
	@RequestMapping("/rv/{name}/{age}")
	public View returnView(@PathVariable("name")String name, @PathVariable("age")int age) {
		//请求转发
		View view = new InternalResourceView("/return/index");
		return view;
	}
	
	/**
	 * http://localhost/return/rdv/笨笨/18
	 */
	@RequestMapping("/rdv/{name}/{age}")
	public View returnRedirectView(@PathVariable("name")String name, @PathVariable("age")int age) {
		//重定向
		View view = new RedirectView("/return/rindex");
		return view;
	}
	
	//========================================= 返回视图对象 (学下历史而已:近古)==============================================
	//--------------- 后来:返回 ModelAndView (转发、重定向,用创建 ModelAndView 的参数来区分) ---------------
	/**
	 * http://localhost/return/rmv/笨笨/18
	 * http://localhost/return/rmv/笨笨/18?type=1
	 */
	@RequestMapping("/rmv/{name}/{age}")
	public ModelAndView returnModeAndView(@PathVariable("name")String name, @PathVariable("age")int age, @RequestParam(defaultValue="0")int type) {
		ModelAndView mv = null;
		if(type == 0){
			mv = new ModelAndView("forward:/return/index");
		}else{
			mv = new ModelAndView("redirect:/return/rindex");
		}
		return mv;
	}
	

}

/ssm/src/main/webapp/WEB-INF/jsp/rindex.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<link href="/css/main.css" rel="stylesheet" />
	</head>
	<body>
		<div class="container">
			<div class="starter-template">
				<h1>大家好,我是${name},年年 ${age} 岁,谢谢!</h1>
				<p>我是被请求转发过来的,不信你看地址栏都变了!【${count}】次</p>
				<a href="http://localhost/return/rd/笨笨/18" target="_self">http://localhost/return/rd/笨笨/18</a>
			</div>
		</div>
	</body>
</html>

如果配置了自定义视图解析器,直接返回index 就会被自动拼上 前后缀: /WEB-INF/jsp/index.jsp

	@RequestMapping("/index")
	public String index() {
		return "index";
	}

如果想走默认视图解析器,加上 forward:

	@RequestMapping("/index")
	public String index() {
		return "forward:/index.jsp";
	}

猜你喜欢

转载自blog.csdn.net/jx520/article/details/87906479