今天看了下 有人url美化地址優化 erp-open 自己早就加入美化功能 你們能夠看下 這段code是本身實現 有可能不是很高效
package com.erp.filter;
import java.util.regex.Pattern;
public class UrlFilter {
private String url;
private String type;
private String request;
private String gz;
public String getGz() {
return gz;
}
public void setGz(String gz) {
this.gz = gz;
}
public UrlFilter() {
}
public UrlFilter(String url) {
this.url = url;
}
public UrlFilter(String url, String type) {
super();
this.url = url;
this.type = type;
}
public UrlFilter(String url, String type, String request) {
super();
this.url = url;
this.type = type;
this.request = request;
}
public UrlFilter(String url, String type, String request, String gz) {
super();
this.url = url;
this.type = type;
this.request = request;
this.gz = gz;
}
public String getUrl() {
return url;
}
public String getRequest() {
return request;
}
public void setRequest(String request) {
if
(request == null){
this.request = "";
}else
this.request = request;
}
public void setUrl(String url) {
this.url = url;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public int hashCode() {
return 1;
}
@Override
public boolean equals(Object obj) {
Pattern pattern = Pattern.compile(((UrlFilter)obj).url);
return pattern.matcher(this.getUrl()).matches();
}
}
url xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<urls>
<url http="/user/userinit.html" type="1">create.do?method=init</url>
<url http="/user/userAdd.html" type="2">create.do?method=add</url>
<url http="/user/([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).html" request="true" type="2">create.do?method=ajax1&t1=$1&t2=$2&t3=$3&t4=$4</url>
</urls>
listener 加載配置
if(Resouce.urlMap == null){
Resouce.urlMap = new HashMap<UrlFilter, UrlFilter>();
SAXReader sr = new SAXReader();
org.dom4j.Document doc = null;
try {
doc = sr.read(new File(arg0.getServletContext().getRealPath("\\WEB-INF\\")+"/urlrewrite.xml"));
} catch (DocumentException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
Resouce.urlMap = null;
return;
}
String parm = "";
Element el_root = doc.getRootElement();
Iterator it = el_root.elementIterator();
while (it.hasNext()) {
Element type = (Element) it.next();
parm = type.attributeValue("parm");
Resouce.urlMap.put(new UrlFilter(type.attributeValue("http"), type.attributeValue("type")),new UrlFilter(type.getText(), type.attributeValue("type"),type.attributeValue("request"),type.attributeValue("http")));
}
}
package com.erp.filter;
import java.io.IOException;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.erp.Resource.Resouce;
public class ReUrl implements Filter {
public void destroy() {
}
/**
* 根據地址美化URL地址
*/
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = ((HttpServletResponse) res);
String url = "";
url = request.getRequestURI().replaceAll(request.getContextPath(), "");
HashMap<UrlFilter, UrlFilter> map = Resouce.urlMap;
UrlFilter filter = new UrlFilter(url);
if (map.containsKey(filter)) {
UrlFilter filter1 = map.get(filter);
if (filter1.getRequest() == null) {
} else {
if (filter1.getRequest().equals("true")) { //替換url地址參數 根據正則表達式實現
Pattern p = Pattern.compile(filter1.getGz());
Matcher matcher = p.matcher(url);
int count = matcher.groupCount();
int index = 1;
while (matcher.find(0)) {
filter1.setUrl(filter1.getUrl().replace("$" + index,
matcher.group(index)));
index++;
if (index > count) {
break;
}
}
}
}
if (filter1.getType() != null) {
if (filter1.getType().equals("2")) {
Resouce.clearParms(request);
}
}
request.getRequestDispatcher("../" + filter1.getUrl()).forward(
request, response);
} else {
chain.doFilter(request, response);
}
}
public void init(FilterConfig arg0) throws ServletException {
System.out.println(1);
}
}
我表示沒有進行優化只是簡單實現了下 但願你們不要噴