因为要用Jfinal 写CMS,所以需要用到大量Urlwrite 功能
自己写了一个分享给大家,
写的比较糙,特别是@@分隔符,有改进办法欢迎讨论
没有用XML 直接用properties,
流程是 properties的配置转成List,通过Handler然后逐行匹配url请求 进行转化成真正的url请求,符合条件直接return,停止循环,所以优先级比较高的建议放在配置文件上方.
参数最多加了4个,可以在实现类里自己添加,没有写成循环所有,因为可能出现需要$2不需要$1的情况
全局处理器 加入写好的Handler
/** * 配置全局处理器 */ @Override public void configHandler(Handlers me) { me.add(new UrlRewriteHandler()); }
Handle 文件
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jfinal.handler.Handler; public class UrlRewriteHandler extends Handler { public UrlRewrite ur; public UrlRewriteHandler() { ur=new UrlRewrite("urlrewrite.properties"); } @Override public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) { // TODO Auto-generated method stub target=ur.rewriteUrl(target.toLowerCase()); nextHandler.handle(target, request, response, isHandled); } }
实现类
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.jfinal.kit.LogKit; /** * 读取配置文件 * 转化配置文件 * Todd */ public class UrlRewrite { private List urllist; public UrlRewrite(String fileName) { InputStream inputStream = null; try { inputStream = getClassLoader().getResourceAsStream(fileName); // properties.load(Prop.class.getResourceAsStream(fileName)); if (inputStream == null) { throw new IllegalArgumentException("Properties file not found in classpath: " + fileName); } // properties = new Properties(); // properties.load(new InputStreamReader(inputStream)); // System.out.println("Rewrite rules loading.."); loadlist(new BufferedReader(new InputStreamReader(inputStream))); } catch (IOException e) { throw new RuntimeException("Error loading properties file.", e); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException e) { LogKit.error(e.getMessage(), e); } } } private ClassLoader getClassLoader() { ClassLoader ret = Thread.currentThread().getContextClassLoader(); return ret != null ? ret : getClass().getClassLoader(); } // 加载urlwrite文件 ,转化List格式传回 private List loadlist(BufferedReader reader) throws IOException { String line; urllist = new ArrayList(); while (reader.ready()) { line = reader.readLine().replaceAll(" ", ""); if(!line.contains("#")) { urllist.add(line); } } line = null; reader.close(); return urllist; } // url rewrite public String rewriteUrl(String target) { String[] rexlines; Pattern p; Matcher m; for (int i = 0; i < urllist.size(); i++) { rexlines = String.valueOf(urllist.get(i)).split("@@"); if (rexlines.length == 2) { p = Pattern.compile(rexlines[0].trim()); m = p.matcher(target); if (m.find()) { target = rexlines[1].trim(); //存在只有$2 没有$1的情况,所以不做FOR或者WHILE循环,最多4个参数 if (rexlines[1].contains("$1")) { target = target.replaceAll("\\$1", m.group(1)); } if (rexlines[1].contains("$2")) { target = target.replaceAll("\\$2", m.group(2)); } if (rexlines[1].contains("$3")) { target = target.replaceAll("\\$3", m.group(3)); } if (rexlines[1].contains("$4")) { target = target.replaceAll("\\$4", m.group(4)); } return target; } } } return target; } }
配置文件 @@分隔符, 语法类似urlrewrite xml 配置文件 (*******) = $1 格式
# ---URL Rewrite--- # @@ 分离符 # # ===========ticket=============== #/aucn/ = /ticket/1 ^/aucn/$ @@ /ticket/1 #/cnau/ = /ticket/2 ^/cnau/$ @@ /ticket/2 #/syd/ = /ticket/city/syd ^/([A-Za-z]{3})/$ @@ /ticket/city/$1 #/syd/pek = /ticket/citytocity/syd-pek ^/([A-Za-z]{3})/([A-Za-z]{3})$ @@ /ticket/citytocity/$1-$2