直接上代码,调试firefox,F12,如装了firebug先禁用:
package com.up360.wechat.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.http.HttpServletRequest;
/**
* 功能或描述:
*
* @Author: DR.YangLong
* @Date: 14-7-16
* @Time: 下午01:18
* @Email: 410357434@163.com
* @Version: 1.0
* @Module: 修正: 日期:
*/
@Controller
@RequestMapping(value = "/redirect")
public class RedirectController {
//不带参数
@RequestMapping("/test1")
public ModelAndView redirect1(){
ModelAndView mv=new ModelAndView("redirect:/redirect/result.action");
//使用/开头将使用重定向,不用将变为forward
return mv;
}
//RedirectView构造参数(地址,不使用相对路径,兼容http1.0,暴露请求参数)对应默认值(url,false,true,true)。
//public RedirectView(String url, boolean contextRelative, boolean http10Compatible, boolean exposeModelAttributes)
@RequestMapping("/test2")
public RedirectView redirect2(){
RedirectView redirectView=new RedirectView("/redirect/result.action");
return redirectView;
}
/**
* 乱码设置Tomcat server.xml的Connector添加 useBodyEncodingForURI="true"
* 参数将会拼接到链接后面
* @return
*/
@RequestMapping("/test3")
public ModelAndView redirect3(){
ModelAndView modelAndView=new ModelAndView("redirect:/redirect/result0.action");
modelAndView.addObject("msg","ModelAndView第一种方式带参!");
return modelAndView;
}
@RequestMapping("/test4")
public ModelAndView redirct4(RedirectAttributes redirectAttributes){
//这个会将数据返回客户端,客户端再携带过来,参数会拼接到链接后面
/*redirectAttributes.addAttribute("msg","ModelAndView第二种方式带参");*/
//数据放到session中,下一个控制器接收到后从session清除,参数不会拼接到链接后面,非跨域建议此方式
redirectAttributes.addFlashAttribute("msg", "ModelAndView第二种方式带参");
ModelAndView modelAndView=new ModelAndView("redirect:/redirect/result.action");
return modelAndView;
}
@RequestMapping("/result0")
public @ResponseBody String result(String msg,HttpServletRequest request){
//如果是对象,可直接用对象绑定,不用使用ModelMap
return "返回结果1直接绑定数据模型:"+msg;
}
@RequestMapping("/result")
public @ResponseBody String result(ModelMap map,HttpServletRequest request){
//如果是对象,可直接用对象绑定,不用使用ModelMap
String msg=(String)map.get("msg");
return "返回结果2直接绑定数据模型:"+msg;
}
}
分享到:
相关推荐
本篇文章主要介绍了浅谈Spring 重定向指南,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring mvc重定向导致内存溢出解决方案
本文介绍了在Spring中实现重定向的三种不同方法,在执行这些重定向时如何处理/传递属性以及如何处理HTTP POST请求的重定向。关于Spring 重定向(Redirect)指南的相关知识大家参考下本
spring MVC简易框架,用于项目开发初始架构
SpringColud1简易分布式 ● cloud-config-server:配置服务器-(通过git获取配置) ● cloud-eureka-server:eureka注册服务器 ● cloud-simple-service:一个使用mybatis的数据库应用,服务端 ● cloud-simple-...
spring mvc controller间跳转 重定向 传参
随着Spring框架的流行,Spring Security也被人们更加关注,这个文档就是介绍Spring Security的简易配置流程。
NULL 博文链接:https://zhengshuo.iteye.com/blog/573192
主要介绍了SpringCloud URL重定向及转发代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
strut2+spring+springjdbctemplate做的简易登录系统
Spring基础:Spring AOP简单使用
SpringCloud微服务。简易demo航班机票查询
自主实现的简易Spring框架,通过注解实现IoC容器和AOP。
使用java ssh框架(struts2+hibernate+spring)开发的简易电子商城系统,有用户登录注册功能、商城购物功能,有前端页面,开发环境:编译器为eclipse,数据库为mysql5.7(mysql8.0用不了,需要重新修改配置文件),...
springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot ...
struts2 hibernate spring整合开发简易论坛
JDK1.7,Maven为阿里镜像。操作按照API的操作来(资源分默认为2,没有免费选项)
Spring 参考手册 Spring速查手册 spring初学者使用
弹簧重定向 在Spring中重定向的不同方法
简易的中秋博饼游戏(Swing+Spring+MyBatis).zip 简易的中秋博饼游戏(Swing+Spring+MyBatis).zip 简易的中秋博饼游戏(Swing+Spring+MyBatis).zip 简易的中秋博饼游戏(Swing+Spring+MyBatis).zip 简易的中秋博饼游戏...