Slim3 Controller‎ > ‎

URL mapping

Slim3 maps a request path to a controller automatically, so you don't need to define configurations. The mapping rule is as follows:

 a request path
 controller
 /  <slim3.rootPackage>.controller.IndexController
 /xxx  <slim3.rootPackage>.controller.XxxController
 /xxx/  <slim3.rootPackage>.controller.xxx.IndexController
 /xxx/yyy  <slim3.rootPackage>.controller.xxx.YyyController
 /xxx/yyy/zzz  <slim3.rootPackage>.controller.xxx.yyy.ZzzController

<slim3.rootPackage> is defined in web.xml.

If you use GWT, the mapping rule is as follows:

 a request path
 controller
 /  <slim3.rootPackage>.server.controller.IndexController
 /xxx  <slim3.rootPackage>.server.controller.XxxController
 /xxx/  <slim3.rootPackage>.server.controller.xxx.IndexController
 /xxx/yyy  <slim3.rootPackage>.server.controller.xxx.YyyController
 /xxx/yyy/zzz  <slim3.rootPackage>.server.controller.xxx.yyy.ZzzController


You can also rewrite a request path before the request gets to a controller. To use URL rewriting, you need to define <slim3.rootPackage>.controller.AppRouter class first. Due to HOT reloading, even when you add AppRouter, you don't need to restart your application.

For example:
package slim3.demo.controller;

import org.slim3.controller.router.RouterImpl;

public class AppRouter extends RouterImpl {

    public AppRouter() {
        addRouting("/_ah/mail/{address}", "/mail/receive?address={address}");
        addRouting(
            "/{app}/edit/{key}/{version}",
            "/{app}/edit?key={key}&version={version}");
        addRouting(
            "/{app}/delete/{key}/{version}",
            "/{app}/delete?key={key}&version={version}");
                addRouting(
            "/{app}/find/*path",
            "/{app}/find?path={path}");
    }
}


The request for /_ah/mail/xxx@gmail.com is routed to /mail/receive?address=xxx@gmail.com.
The request for /blog/edit/xxx/1 is routed to /blog/edit?key=xxx&version=1.
The request for /blog/delete/xxx/1 is routed to /blog/delete?key=xxx&version=1.
The request for /blog/find/xxx/yyy/zzz is routed to /blog/find?path=xxx%2Fyyy%2Fzzz, (xxx/yyy/zzz gets URL-encoded).

A place holder({xxx}) does not match "/", because "/" is a path separator.
A catch-all place holder(*xxx) must be at the end of the match (From) string and matches everything including "/" up to the end. 

Next...

Continue to Request, Response and ServletContext.


Comments