springmvc处理重定向或者转发的返回值

本文深入探讨了SpringMVC中控制器方法返回值如何实现转发与重定向,详细解释了DispatcherServlet如何处理ModelAndView对象,解析视图名称,并通过InternalResourceViewResolver和UrlBasedViewResolver处理转发和重定向请求。

 

springmvc支持直接在返回字符串上申明该视图是转发或者是重定向

@RequestMapping(value="/taoge")
	public String taogetest(HttpServletRequest request,Model model){
		return "forward:toregister";
        # return "redirect:toregister";
	}

这两种方式都能完成control之间的跳转,它们具体是怎么实现的呢

主要是在DispatcherServlet.render(ModelAndView, HttpServletRequest, HttpServletResponse) line: 1234 上

/**
	 * Render the given ModelAndView.
	 * <p>This is the last stage in handling a request. It may involve resolving the view by name.
	 * @param mv the ModelAndView to render
	 * @param request current HTTP servlet request
	 * @param response current HTTP servlet response
	 * @throws ServletException if view is missing or cannot be resolved
	 * @throws Exception if there's a problem rendering the view
	 */
	protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// Determine locale for request and apply it to the response.
		Locale locale = this.localeResolver.resolveLocale(request);
		response.setLocale(locale);

		View view;
		if (mv.isReference()) {  #判断当前view名是否是可用的,如果不能直接用,那么需要解析该视图名
			// We need to resolve the view name.
			view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request); #这个方法是真正解析视图的方法
			if (view == null) {
				throw new ServletException("Could not resolve view with name '" + mv.getViewName() +
						"' in servlet with name '" + getServletName() + "'");
			}
		}

DispatcherServlet.resolveViewName(String, Map<String,Object>, Locale, HttpServletRequest) line: 1296    
 

protected View resolveViewName(String viewName, Map<String, Object> model, Locale locale,
			HttpServletRequest request) throws Exception {

		for (ViewResolver viewResolver : this.viewResolvers) {
			View view = viewResolver.resolveViewName(viewName, locale);
			if (view != null) {
				return view;
			}
		}
		return null;
	}

 InternalResourceViewResolver(AbstractCachingViewResolver).resolveViewName(String, Locale) line: 145    

	public View resolveViewName(String viewName, Locale locale) throws Exception {
		if (!isCache()) { 是否存在缓存
			return createView(viewName, locale);
		}
		else {
			Object cacheKey = getCacheKey(viewName, locale); 从缓存中取出对应的键
			View view = this.viewAccessCache.get(cacheKey);
			if (view == null) { 如果在缓存map还是找不到该视图,那么就重新生成一个
				synchronized (this.viewCreationCache) {
					view = this.viewCreationCache.get(cacheKey);
					if (view == null) {
						// Ask the subclass to create the View object.
						view = createView(viewName, locale); 重新生成一个视图
						if (view == null && this.cacheUnresolved) {
							view = UNRESOLVED_VIEW;
						}
						if (view != null) {
							this.viewAccessCache.put(cacheKey, view);
							this.viewCreationCache.put(cacheKey, view);
							if (logger.isTraceEnabled()) {
								logger.trace("Cached view [" + cacheKey + "]");
							}
						}
					}
				}
			}
			return (view != UNRESOLVED_VIEW ? view : null);
		}
	}

 UrlBasedViewResolver类中453行

@Override
	protected View createView(String viewName, Locale locale) throws Exception {
		// If this resolver is not supposed to handle the given view,
		// return null to pass on to the next resolver in the chain.
		if (!canHandle(viewName, locale)) {
			return null;
		}
		// Check for special "redirect:" prefix. 判断是否存在前置字符串redirect:
		if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
			String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
			RedirectView view = new RedirectView(redirectUrl, isRedirectContextRelative(), isRedirectHttp10Compatible());
			view.setHosts(getRedirectHosts());
			return applyLifecycleMethods(viewName, view);
		}
		// Check for special "forward:" prefix.
		if (viewName.startsWith(FORWARD_URL_PREFIX)) { 判断是否存在前置字符串forward:
			String forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length());
			return new InternalResourceView(forwardUrl);
		}
		// Else fall back to superclass implementation: calling loadView.
		return super.createView(viewName, locale);
	}

 

### 实现Spring MVC POST 请求重定向 在Spring MVC框架中,实现POST请求后的重定向操作是一个常见的需求。当接收到`POST`请求并完成相应的业务逻辑处理之后,通常会希望将用户重定向到另一个页面或资源上。 对于带有路径变量的情况,可以在控制器方法内定义@RequestMapping注解来指定HTTP请求的方法类型以及URL模式,并利用字符串形式的视图名称作为返回值来进行重定向。例如: ```java @RequestMapping(value = "/files/{path}", method = RequestMethod.POST) public String upload(@PathVariable("path") String path, ...) { // 执行上传文件或其他业务逻辑... return "redirect:/view/files/" + path; // 或者更简洁的方式:"redirect:files/{path}"[^2] } ``` 这里需要注意的是,在执行重定向的过程中,如果想要保留某些数据供目标地址使用,则可以借助于`RedirectAttributes`对象传递临时性的属性给下一个请求。这有助于保持用户体验的一致性和连贯性而不必担心GET请求可能带来的重复提交问题。 另外一种情况是关于如何确保模型(Model)中的数据能够在重定向后仍然可用。由于默认情况下,原始请求的数据不会被自动携带至新的请求中,因此需要显式地向`RedirectAttributes`添加这些信息以便它们能在后续请求中访问得到[^3]。 ```java import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.support.RedirectAttributes; // ... @RequestMapping(method = RequestMethod.POST) public String handleFormSubmission(RedirectAttributes redirectAttrs){ // 处理表单... // 将消息添加到flash属性中 redirectAttrs.addFlashAttribute("message", "成功保存!"); return "redirect:/success"; } ``` 上述代码片段展示了如何通过`addFlashAttribute()`方法把特定的信息存储起来用于下一次请求显示出来。这种方式非常适合用来反馈操作结果或是其他短暂存在的状态提示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值