configRoute(Routes me)


This method is used to configure JFinal access routing. The following code configures the mapping of "/hello" to the HelloController controller. Through the following configuration, http://localhost/hello will access HelloController.index() method, and http://localhost/hello/methodName will access the HelloController.methodName() method.

public void configRoute(Routes me)
{ me.add("/hello",
HelloController.class);

Routes class mainly includes The following two methods:


public Routes add(String controllerKey, Class<? extends Controller>
controllerClass, String viewPath)


public Routes add(String controllerKey, Class<? extends Controller>

##controllerClass)


The first parameter controllerKey refers to a string required to access a Controller. This string uniquely corresponds to one Controller. The controllerKey can only locate the Controller. The second parameter controllerClass is the Controller corresponding to the controllerKey. The third parameter viewPath refers to the relative path of the view returned by the Controller (the specific details of this parameter will be given in the relevant chapters of the Controller). The default value is controllerKey when viewPath is not specified.


JFinal routing rules are as follows:


QQ截图20170206095411.png

##From the table It can be seen that JFinal accesses an exact Action (see Section 3.2 for Action definition) and needs to use controllerKey and method to accurately locate it. When method is omitted, the default value is index. urlPara is to carry parameter values ​​in the url. urlPara can carry multiple values ​​in one request at the same time. JFinal uses the minus sign "-" to separate multiple values ​​by default (the separator can be set through constants.setUrlParaSeparator(String)), In Controller, these values ​​can be retrieved separately through getPara(intindex). The three parts of controllerKey, method, and urlPara must be separated by forward slashes "/". Note that the controllerKey itself can also contain a forward slash "/", such as "/admin/article", which essentially implements the namespace function of struts2.



JFinal also provides ActionKey annotations in addition to the above routing rules, which can break the original rules. The following is a code example:

public class UserController extends Controller {
@ActionKey("/login")
public void login()
{ render("login.html");
}


Assume that the controllerKey value of UserController is "/user". After using the @ActionKey("/login") annotation, the actionKey changes from the original "/user/login" to " /login". This annotation can also allow characters such as minus signs or numbers to be used in actionKey, such as "/user/123-456".


If JFinal's default routing rules cannot meet the needs, developers can also use Handler to customize more personalized routing as needed. The general idea is to change the first parameter String target in Handler. value.

JFinal routing can also be split and configured, which is particularly useful for large-scale team development. The following is a code example:

public class FrontRoutes extends Routes {



public void config(){



add("/",IndexController.class);

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



}



}

public class AdminRoutesextends Routes{



public void config(){



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

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



}



}

public class MyJFinalConfigextends JFinalConfig{



publicvoid configRoute(Routesme)

{ me.add(new FrontRoutes()); // Front-end routing

me.add(new AdminRoutes()); / / Backend routing



}


##public void configConstant(Constantsme) {}



public void configPlugin(Pluginsme) {}



public void configInterceptor(Interceptorsme) {}


##public void configHandler(Handlersme) {}



}


As shown in the above three pieces of code, the system front-end routing is configured in the FrontRoutes class, and the AdminRoutes configuration The system back-end route is installed, and the MyJFinalConfig.configRoute(...) method merges the two split routes. Using this split configuration not only makes the MyJFinalConfig file more concise, but also facilitates large-scale team development and avoids version conflicts when multiple people modify MyJFinalConfig at the same time.