Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

2.3 configRoute(Routes me)


May 14, 2021 JFinal manual



This method is used to configure the JFinal access route, and the following code is configured to map "/hello" to the HelloController controller, and through the provisioning below, http://localhost/hello will access the HelloController.index() method, and http://localhost/hello/methodName will access the HelloController.methodName().


public void configRoute(Routes me)

{ me.add("/hello",

HelloController.class);


The Routes class has two main methods:


public R outes add(String controllerKey, Class<? ends Controller>

controllerClass, String viewPath)

public R outes add(String controllerKey, Class<? ends Controller>

controllerClass)


The first argument, controllerKey, refers to a string that is required to access a Controller, and the string is unique to a Controller, which can only be located to Controller. T he second parameter, controllerClass, is the Controller to which the controllerKey corresponds. T he third parameter, viewPath, refers to the relative path of the view returned by the Controller ,the details of which are given in the Controller section. The default value is controllerKey when viewPath is not specified.

JFinal routing rules are as follows:

url composition

Access the target

controllerKey

YourController.index()

controllerKey/method

YourController.method()

controllerKey/method/v0-v1

YourController.method(), with url parameter value: v0-v1

controllerKey/v0-v1

YourController.index(), with url parameter value: v0-v1

As can be seen from the table, JFinal's access to an exact Action (action definition see section 3.2) requires controllerKey and method to be precisely positioned, with the default value index when the method is omitted. u rlPara is to be able to carry parameter values in url, urlPara can carry multiple values at the same time in a single request, JFinal defaults to using the minus sign "-" to separate multiple values (can be separated by constants. S etUrlParaSeparator (String) sets separators, which can be taken out separately in Controller through getPara (intindex). C ontrollerKey, method, urlPara, these three parts must be separated by a forward slash "/" Note that controllerKey itself can also contain positive slashes "/", such as "/admin/article", which essentially implements the namespace functionality of struts2.


In addition to the above routing rules, JFinal provides ActionKey annotations that break the old rules, and here's a code example:

public class UserController extends Controller {

@ActionKey("/login")

public void login()

{ render("login.html");

}


Assuming that UserController's controllerKey value is "/user," actionKey changed from "/user/login" to "/login" after using a @ActionKey ("/login") annotation. The annotation also allows actionKey to use characters such as minus sign or number, such as "/user/123-456."


If JFinal's default routing rules don't meet the requirements, developers can also customize more sexual routes with Handler as needed, with the general idea being to change the value of the first parameter, String target, in Handler.

JFinal routing can also be split, which is especially useful for large-scale team development, and here's an example of code:

public class FrontRoutes extends Routes {

public void config(){

add("/",IndexController. class );

add("/blog", BlogController. class );

}

}


public class AdminRoutes extends Routes{

public void config(){

add("/admin",AdminController. class );

add("/admin/user", UserController. class );

}

}


public class MyJFinalConfig extends JFinalConfig{

publicvoid configRoute(Routesme)

{ me.add( new FrontRoutes()); F ront-end routing

me.add( new AdminRoutes()); Back-end routing

}

public void configConstant(Constantsme) {}

public void configPlugin(Pluginsme) {}

public void configInterceptor(Interceptorsme) {}

public void configHandler(Handlersme) {}

}

As in the last three pieces of code, system front-end routing is configured in the FrontRoutes class, AdminRoutes is configured system back-end routing, MyJFinalConfig.configRoute (...) M ethod merges the split two routes. Using this split configuration not only makes MyJFinalConfig files cleaner, but also facilitates large-scale team development, avoiding version conflicts when multiple people modify MyJFinalConfig at the same time.