Java Url Rewrite Tool : UrlRewriteFilter
Url rewrite is a common requirement in the web development. Apache Web Server uses its module mod_rewrite to offere a powerful Url rewrite function. In this post, i make some introduction to UrlRewriteFilter which is a very powerful tool just like Apache's mod_rewrite.Here is the detailed manul UrlRewriteFilter. It is east to use in your Java web project following the steps. It is also apparently that UrlRewriteFilter takes great advantage of Java Servlet's filter. In the following recipe, i want to highligh the java annotation implementation.UrlRewriteFilter provides three kinds of annotations: @HttpUrl , @HttpParam @HttpJson[java]@Retention(RetentionPolicy.SOURCE)@Target(ElementType.METHOD)public @inte易做图ce HttpUrl {String value();/*** A weighting to be applied to the url, if higher it will move this url to the "top" if lower more towards the* "bottom".** @return weight*/int weight() default 0;}[java]@Retention(RetentionPolicy.SOURCE)@Target(ElementType.PARAMETER)public @inte易做图ce HttpParam {/*** If not set will use the name of the parameter (case insensitive).* can be a expression ie, $1 (the first group of @HttpUrl regexp), %{header:user-agent} (the user agent header).** @return value*/String value() default "[ unassigned ]";}[java]@Retention(RetentionPolicy.SOURCE)@Target(ElementType.METHOD)public @inte易做图ce HttpJson {String value() default "[ unassigned ]";/*** A weighting to be applied to the url, if higher it will move this url to the "top" if lower more towards the* "bottom".** @return weight*/int weight() default 0;}For those annotions, UrlRewriteFilter project creates a annotation processor named UrlRewriteAnnotationProcessor to handle them.[java]public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {if (isBlank(dest)) {if (roundEnv.processingOver())infoMsg(getClass().getSimpleName() + ": -AurlrewriteDest not specified, annotations ignored");return true;}debugMsg("process");Set<? extends Element> urlDeclarations = roundEnv.getElementsAnnotatedWith(HttpUrl.class);for (Element element : urlDeclarations) {processedAnnotations.add(new ProcessedHttpUrlAnnotation(element));}Set<? extends Element> jsonDeclarations = roundEnv.getElementsAnnotatedWith(HttpJson.class);for (Element element : jsonDeclarations) {processedJsonAnnotations.add(new ProcessedHttpJsonAnnotation(element));}Set<? extends Element> exceptionDeclarations = roundEnv.getElementsAnnotatedWith(HttpExceptionHandler.class);for (Element element : exceptionDeclarations) {httpExceptionHandlers.add(new ProcessedHttpExceptionAnnotation(element));}if (roundEnv.processingOver()) {if (processedAnnotations.size() > 0) {infoMsg("Got " + processedAnnotations.size() + " @HttpUrl annotations");}if (processedJsonAnnotations.size() > 0) {infoMsg("Got " + processedJsonAnnotations.size() + " @HttpJson annotations");processedAnnotations.addAll(processedJsonAnnotations);}Collections.sort(processedAnnotations);if (httpExceptionHandlers.size() > 0) {infoMsg("Got " + httpExceptionHandlers.size() + " @HttpExceptionHandler annotations");Collections.sort(httpExceptionHandlers);}try {File destFile = new File(dest);if (!destFile.exists()) {checkDirsExistMkdir(destFile.getParentFile());destFile.createNewFile();}if (!destFile.canWrite()) {throw new IOException("cannot write to " + destFile.getName());}if (errorDuringProcessing) {errorMsg("Error occured during processing deleting generated file.");destFile.delete();&nb补充:软件开发 , Java ,
上一个:实时操作系统主流调度方法RMS
下一个:Java中的classpath
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值