?? urlinterceptor.java
字號:
package com.ponyjava.common.struts2.interceptor;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class UrlInterceptor extends MethodFilterInterceptor {
private static final long serialVersionUID = 1L;
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
ActionContext ctx = invocation.getInvocationContext();
HttpServletRequest req = (HttpServletRequest) ctx
.get(StrutsStatics.HTTP_REQUEST);
if (action instanceof UrlAware) {
UrlAware aware = (UrlAware) action;
String url = req.getRequestURL().toString();
int pointIndex = url.indexOf('.', url.lastIndexOf('/'));
if (pointIndex == -1) {
url += "index.do";
pointIndex = url.indexOf('.', url.lastIndexOf('/'));
}
String paramStr = req.getQueryString();
if (paramStr != null && !paramStr.trim().equals("")) {
url += "?" + paramStr;
}
aware.setWholeUrl(url);
int lineIndex = url.indexOf('_', url.lastIndexOf('/'));
int mlineIndex = url.indexOf('-', url.lastIndexOf('/'));
if (pointIndex != -1) {
int preIndex = 0;
if (lineIndex != -1) {
// 有下劃線(有分頁)
preIndex = lineIndex;
} else if (mlineIndex != -1) {
// 有中劃線(有定制參數)
preIndex = mlineIndex;
} else {
// 什么都沒有
preIndex = pointIndex;
}
aware.setPageLink(url.substring(0, preIndex));
int suffixIndex = 0;
if (mlineIndex != -1) {
// 有中劃線
suffixIndex = mlineIndex;
} else {
suffixIndex = pointIndex;
}
aware.setPageSuffix(url.substring(suffixIndex));
// 取頁數
if (preIndex == suffixIndex) {
// 沒有分頁,為第一頁。
aware.setPageNo(1);
} else {
// 去掉下劃線"_"。
String page = url.substring(preIndex + 1, suffixIndex);
try {
aware.setPageNo(Integer.parseInt(page));
} catch (Exception e) {
aware.setPageNo(1);
}
}
} else {
// never
throw new RuntimeException("路徑錯誤。不過這是不可能發生的!");
}
String[] pathParams = ctx.getName().split("/");
aware.setPathParams(pathParams);
}
return invocation.invoke();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -