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重定向的地址會丟失數據服務器

相關文章
相關標籤/搜索