开发者

how to rewrite or generate url like twitter and facebook in java

how to rewrite or generate url like twitter and facebook in java....

Example : https://www.facebook.com/username, https://www.twitter.com/username

is there any java framework for this?

If anyone's got any ideas on how I could do this, or exam开发者_如何学编程ples to help me that would be great!


There are quite a lot of options. Almost every framework can do that. Here are a few options:

  • use the UrlRewriteFilter
  • spring-mvc allows you to map a method to /${username}
  • map a servlet to / and handle the request.getRequestURI() or request.getPathInfo()


I would use OCPsoft PrettyFaces or OCPsoft Rewrite for this:

With PrettyFaces:

create WEB-INF/pretty-config.xml

<url-mapping>
   <pattern value="/#{username}" />
   <view-id value="/profile.jsp" />
</url-mapping>

This will automatically put the value of the URL "username" into the request parameter named "username"

With Rewrite:

Here is the same thing using Rewrite, which is a bit more explicit, but also more powerful.

ConfigurationBuilder.begin()
   .addRule(Join.path("/{username}").to("/profile.jsp")
                .where("username").bindsTo(Request.parameter("username")));

I hope this helps.

~Lincoln

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜