Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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&section=AboutGames

But it isn't so pretty and userfriendly...

I want to make links like these:

  • www.mysite.com/web/2011/10/04/Steve-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.

like image 268
Alex Nevsky Avatar asked Nov 13 '22 16:11

Alex Nevsky


1 Answers

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
  }
}
like image 149
M.L. Avatar answered Dec 18 '22 23:12

M.L.