Skip to content
This repository was archived by the owner on Aug 16, 2025. It is now read-only.

Routing

laxika edited this page Mar 8, 2014 · 2 revisions

Routing variables

The routing in Torch is pretty complex. You can define routes as you saw in the previous section but not only that, you can also add variables to your routes. For example:

torch.getRouteManager().defineRoute("/hello/@variable1/@variable2/", HelloWorldWithTwoVar.class);

The variables in the uris starts with @ so you declare two variables in this method. The first variable is called @variable1 and the second one is @variable2.

You can also mix non variables and variables in any order in a given route, for example:

torch.getRouteManager().defineRoute("/hello/@variable1/lol/", HelloWorldWithOneVar.class);

Okey, so you can declare variables in the routes, but how can you access them? Accessing them is really easy:

request.getRouteData().getValue("variable1");

This will return you a RouteVariable object. You can call getName() and getValue() on it to get it's name/value.

You can use routing variables to create SEO links and so on. Imagine that you plan to do an item database for roleplaying games. You can define a route like this: /@gamename/@itemtype/@itemname and match them to something like /supermmorpg/weapon/blade-of-the-programmers.

The routing order is easy too:

torch.getRouteManager().defineRoute("/hello/@variable1/@variable2/", HelloWorldWithTwoVar.class);
torch.getRouteManager().defineRoute("/hello/@variable1/lol/", HelloWorldWithOneVar.class);
torch.getRouteManager().defineRoute("/hello/exact/route", HelloWorldExactRoute.class);

The /hello/smthing/lol will route to the HelloWorldWithOneVar class while the /hello/thisis/stupid will route to the HelloWorldWithTwoVar class.

You can also iterate over the routing variables this way:

for (RouteVariable routeVar : request.getRouteData()) {
    response.appendContent("Route var: <b>" + routeVar.getName() + "</b> = '" + routeVar.getValue()+"'<br>");
}

Passing depedencies to WebPages

You can pass down depedencies to WebPage classes via routing.

torch.getRouteManager().defineRoute("/", PageWithDependency.class, new Object[]{"This is a string dependency", "And another one"});

Then get the passed down depedencies in the constructor of the target WebPage.

public PageWithDependency(String str1, String str2) {
    this.str1 = str1;
    this.str2 = str2;
}

You need to create the correct constructor, otherwise you will get NoSuchConstructorException exception.

Passing down null as dependency is not allowed, use an empty Object array or the DefaultRouteTarget.NO_DEPENDENCY constant instead.

Routing by request method

You can also route GET/POST/PUT/DELETE request to different classes (the defines without RequestMethod will handle the GET requests):

torch.getRouteManager().defineRoute("/hello/exact/route", HelloWorldExactRoute.class, DefaultRouteTarget.NO_DEPENDENCY, RequestMethod.POST);

The routing engine will route all POST requests for the /hello/exact/route uri to the HelloWorldExactRoute class, while the GET requests for the same uri will return a 404 Not Found error.

You can also pass an EnumSet as method type to route different request methods to the same class:

torch.getRouteManager().defineRoute("/login", Login.class, DefaultRouteTarget.NO_DEPENDENCY, EnumSet.of(RequestMethod.POST, RequestMethod.GET));

Clone this wiki locally