springmvc中forward和redirect
来源:互联网 发布:淘宝农村服务站加盟 编辑:程序博客网 时间:2024/06/06 10:46
springmvc中forward和redirect
一、跳转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
@Controller
public
class
Login{
/**转发**/
@RequestMapping
(
"/login.do"
)
public
String login(HttpServletRequest request,HttpServletResponse response){
request.setAttribute(
"message"
,
"hello"
);
return
"forward:/index.do"
;
//forward在跳转后可以取到message值
}
@RequestMapping
(
"/index.do"
)
public
String index(HttpServletRequest request,HttpServletResponse response){
return
"welcome"
;
}
/**重定向**/
@RequestMapping
(
"/logout.do"
)
public
String logout(HttpServletRequest request,HttpServletResponse response){
request.setAttribute(
"message"
,
"hello"
);
return
"redirect:/register.do"
;
//redirect在跳转后无法取到message值
}
@RequestMapping
(
"/register.do"
)
public
String register(HttpServletRequest request,HttpServletResponse response){
return
"register"
;
}
}
另外forward跳转后地址栏URL不会改变 而redirect会改变
二、传参
1
2
3
4
5
6
7
@RequestMapping
(value =
"update"
, method = RequestMethod.POST)
public
String update(
@Valid
@ModelAttribute
(
"user"
) User user,
RedirectAttributes redirectAttributes) {
accountService.updateUser(user);
redirectAttributes.addFlashAttribute(
"message"
,
"更新用户"
+ user.getLoginName() +
"成功"
);
return
"redirect:/admin/user"
;
}
为了防止用户刷新重复提交,save操作之后一般会redirect到另一个页面,同时带点操作成功的提示信息。因为是Redirect,Request里的attribute不会传递过去,如果放在session中,则需要在显示后及时清理,不然下面每一页都带着这个信息也不对。Spring在3.1才提供了这个能力,原理是放到session中,session在跳到页面后马上移除对象。所以你刷新一下后这个值就会丢掉。
道,可道,非常道;名,可名,非常名
阅读全文
0 0
- springmvc中forward和redirect
- springmvc中forward和redirect
- SpringMVC-forward和redirect
- 【SpringMVC】 forward 和 redirect
- springmvc:Forward和Redirect
- SpringMVC的forward和redirect
- SpringMVC 中Controller之间的 forward 和 redirect
- Java 中Redirect和Forward
- springMVC中的redirect和forward跳转
- SpringMVC 的redirect 和forward 跳转
- Springmvc 在同一个controller和不同的controller中进行重定向redirect和转发forward:
- Springmvc 在同一个controller和不同的controller中进行重定向redirect和转发forward
- SpringMVC 控制器中方法之间的跳转(客户端跳转(redirect)和服务器端跳转(forward))
- Jsp中Forward和Redirect的区别。
- JSP中forward和redirect的区别
- Jsp中Forward和Redirect的区别
- Servlet中 forward 和 redirect的区别
- Servlet中forward和redirect的区别
- Servlet的页面跳转
- 【MyBatis学习11】MyBatis中的延迟加载
- Linux命令—命令详细介绍
- 随笔
- js 复选框的全选,全不选,反选等操作
- springmvc中forward和redirect
- 【MyBatis学习12】MyBatis中的一级缓存
- Webpack中的loader
- Jeddict研究过程中的总结
- R语言安装mxnet问题
- 求数组中最大两个数值
- sed,awk工具学习
- 【MyBatis学习13】MyBatis中的二级缓存
- 服务器源码安装TensorFlow