版權聲明:歡迎轉載,請注明沉默王二原創。 https://blog.csdn.net/qing_gee/article/details/48948719
JavaWeb登陸成功後跳轉到上一個頁面,這個标題注定要詞不達意,你可能會遇到這樣的情形,當點選頁面的某個請求時,由于使用者未登入,需要跳轉到登入頁,使用者登入成功後,再跳轉到上一個頁面;還有一種情況,多個頁面都有登入按鈕,登入成功後需要重新整理對應的登入資訊。
圖文并茂
1.場景
12.場景
2代碼實作
場景1的代碼這次重點說明以下,場景2的代碼可以參照本系列其他文章
再談ajax局部重新整理全局攔截器
public class LoginInterceptor implements Interceptor {
private static Logger logger = Logger.getLogger(LoginInterceptor.class);
@Override
public void intercept(Invocation inv) {
BaseController controller = (BaseController) inv.getController();
String lastRequestURL = controller.getRequest().getRequestURL().toString();
logger.info("請求位址:" + lastRequestURL);
Members loginMem = controller.getSessionAttr("username");
// 使用者如果沒有登入,那麼就跳轉到登入頁面
if (loginMem == null || loginMem.equals("")) {
try {
controller.redirect("/mem/initLogin?forwardURL=" + URLEncoder.encode(lastRequestURL, "UTF-8"));
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage());
}
} else {
inv.invoke();
}
}
}
注意:
1. 全局攔截器可參照jfinal官方文檔添加到config類中。
2. 如果使用者沒有登入,則跳轉到登入頁,并且将目前的url路徑作為參數傳遞給form表單,至于form表單怎麼擷取,請看下個小節。
登入頁
<form class="pop_login_form" action="${ctx}/mem/login?callbackType=forward" method="post"
onsubmit="return validateCallback(this, ajaxDone)">
1. action中增加callbackType=forward;
2. onsubmit中callback函數ajaxDone;
3. validateCallback函數中将上個小節中傳遞的參數進行添加;
4. 其餘的可參照
function validateCallback(form, callback, confirmMsg) {
var $form = $(form);
if (!$form.valid()) {
return false;
}
var _submitFn = function() {
$.ajax({
type : form.method || 'POST',
url : addForwardForUrl($form.attr("action")),
data : $form.serializeArray(),
dataType : "json",
cache : false,
success : callback || YUNM.ajaxDone,
error : YUNM.ajaxError
});
}
if (confirmMsg) {
$.showConfirm(confirmMsg, {
okCall : _submitFn
});
} else {
_submitFn();
}
return false;
}
function addForwardForUrl(url) {
var locationURL = location.href;
// URL中附帶了跳轉路徑
var forwardIndex = locationURL.indexOf("forwardURL");
if (forwardIndex != -1) {
var forwardURL = locationURL.substring(forwardIndex);
// 說明已經有了跳轉路徑
if (url.indexOf("?") != -1) {
url += "&" + forwardURL;
} else {
url += "?" + forwardURL;
}
}
return url;
}
1. 如此之前攔截器中提供的url就可以作為參數傳遞給form的送出請求中。
login函數
@Clear({LoginInterceptor.class})
public void login() throws UnsupportedEncodingException {
logger.info("進行會員登入操作...");
// 擷取使用者名
String name = getPara("username");
// 擷取密碼
String password = getPara("password");
Members mem = Members.me.getMemByUsername(name);
if (mem != null) {// 使用者不存在
// 密碼不比對
if (!mem.getStr("password").equals(CipherUtils.generatePassword(password))) {
ajaxDoneError("密碼不正确!");
} else {
// 儲存session
setSessionAttr("username", mem);
String forwardURL = getPara("forwardURL");
if (forwardURL == null || forwardURL.equals("")) {
forwardURL = "/";
} else {
forwardURL = URLDecoder.decode(forwardURL, "UTF-8");
}
ajaxDoneSuccess("登入成功!", forwardURL);
}
} else {
ajaxDoneError("使用者不存在!");
}
}
1. login函數中,将forwardurl取出來,作為登入頁中的callback函數的參數值;
2. 具體ajaxDoneSuccess方法可以參照以下内容
public void ajaxDoneSuccess(String message, String forwardURL) {
ajaxDone(200, message, forwardURL);
}
protected void ajaxDone(int statusCode, String message, String forwardURL) {
// 回調類型
if (getAttr("callbackType") == null) {
String callbackType = getPara("callbackType");
if (callbackType != null && !callbackType.equals("")) {
setAttr("callbackType", callbackType);
if (callbackType.equals("forward") || callbackType.equals("closeCurrentThenForward")) {
String contextPath = getRequest().getContextPath();
if (forwardURL.indexOf(contextPath) == -1) {
forwardURL = contextPath + forwardURL;
}
setAttr("forwardURL", forwardURL);
}
}
}
renderJson();
}
如果有callbacktype參數,則将forwardurl作為json資料傳遞到前端的ajaxdone方法中。
ajaxDone方法
function ajaxDone(json) {
YUNM.ajaxDone(json);
if (json[YUNM.keys.statusCode] == YUNM.statusCode.ok || json[YUNM.keys.statusCode] == YUNM.statusCode.info) {
if ("forward" == json.callbackType) {
if (json.forwardURL) {
location.href = json.forwardURL;
}
}
跳轉到上一個頁面。
笑對現實的無奈,不能後退的時候,不再傍徨的時候,永遠向前 路一直都在──陳奕迅《路一直都在》
本文出自:【
沉默王二的部落格 】
- 當點選頁面的某個請求時,由于使用者未登入,需要跳轉到登入頁,使用者登入成功後,再跳轉到上一個頁面 ↩
- 還有一種情況,多個頁面都有登入按鈕,登入成功後需要傳回到上一個界面。