springMVC学习笔记:转发(forward)和重定向(redirect)小结

forward转发会携带数据,在服务器内部转发,速度较快,只有一次请求java

@RequestMapping(value = "/comment")
	public String comment(String secret, String tiebaId, String comment) {
		HttpSession session = httpServletRequest.getSession();
		Object userid = session.getAttribute("userId");
		if ("secret".equals(secret)) { // 私密评论
			if (userid == null) {
				return "login";
			}
			int userId = (int) userid;
			tiebaService.commentSecret(Integer.valueOf(tiebaId), comment, userId);
		} else {// 公开评论
			if (userid == null) {
				userid = 0;
			} else {
			}
			int userId = (int) userid;
		}
		tiebaService.comment(Integer.valueOf(tiebaId), comment, userId);
		return"forward:showDetail.do";
	}

此时forward转发到的地址也会接收到secret,tiebaID,comment数据不会丢失 若是此时将“return "forward:showDetail.do";”改成 “return "forward:showDetail.do?tiebaId="+tiebaId";” 那么转发的地址将会结果收到两个tiebaId,并以“,”隔开 而redirect重定向的地址会丢失数据服务器

相关文章
相关标签/搜索