How to create pretty URLs (permalinks) for website on Java?
I would like to make pretty URLs for my web projects on Java.
For example, I have URLs like these:
- www.mysite.com/web/controller?command=showNews&newsId=1
- www.mysite.com/web/controller?command=showNews&newsId=2
- www.mysite.com/web/controller?command=showNews&newsId=3
or
- www.mysite.com/web/user.do?action=start
- www.mysite.com/web/user.do?action=showCategory&category=videoGames§ion=AboutGames
But it isn't so pretty and userfriendly...
I want to make links like these:
- www.mysite.com/web/2011/10/04/St开发者_StackOverflow中文版eve-Jobs-iPhone-5/
- www.mysite.com/web/2011/10/23/Facebook-Timeline/
- www.mysite.com/web/2012/05/25/Vladimir-Putin-Russian-President/
Сan you help me with this? How can I get it?
It's possible to use any Java frameworks or libs if it's help.
Thank you!
Update: I found solution - Spring MVC with Controller
's @RequestMapping("/Putin")
annotation for example.
Context Framework allows you to do just that. For instance the examples you gave could be mapped like this in a view:
@View(url="regex:/web/<year:\\d{4}>/<month:\\d{2}>/<day:\\d{2}>/<specifier>")
@PageScoped
public class ArticleView extends Component implements ViewComponent {
@PathParam
private long year;
@PathParam
private long month;
@PathParam
private long day;
@PathParam
private String specifier;
@Override
public void initialize(ViewContext context) {
System.out.println(year+"/"+month+"/"+day+"/"+specifier);
// Then do something
}
}
精彩评论