Convention Mode
Explanation of terms: Convention Over Configuration, also known as programming by convention, is a software design paradigm that uses constraints such as naming rules to reduce program complexity. configuration, designed to reduce the number of decisions software developers need to make, gaining the benefits of simplicity without sacrificing flexibility.
Sometimes we have to write a controller method just to access a view file. When there is a lot of repetitive work, it becomes a disaster. Therefore, in the WebMVC module , by turning on the convention mode, you can support direct access to the view files under the base_view_path
path without writing any code;
The convention mode of the WebMVC module is turned off by default and needs to be turned on through configuration parameters. :
ymp.configs.webmvc.convention_mode=true
Access permission rule configuration
In the convention mode, it supports setting access permissions for different paths. The rules are: -
means access is prohibited,
or no string means access is allowed, multiple paths are separated by |
;
access Permission example: Access to the admin directory and index.jsp file is prohibited. The directory structure is as follows:
WEB-INF\ | |--templates\ | | | +--admin\ | | | +--users\ | | | +--reports\ | | | +--index.jsp | | | <...>
Example parameter configuration:
ymp.configs.webmvc.convention_view_paths=admin-|index-|users|reports+
Interceptor rule configuration
Since in convention mode, no controller is required to access the view file, the interceptor configuration cannot be added through the controller method, so , the WebMVC module provides an extended function of interceptor rule configuration for the convention mode, which is mainly used through @InterceptorRule in conjunction with the IInterceptorRule interface;
The interceptor rule setting is turned off by default and needs to be turned on through configuration parameters:
ymp.configs.webmvc.convention_interceptor_mode=true
Interception rule configuration example:
@InterceptorRule("/demo") @Before(WebUserSessionCheck.class) public class InterceptRuleDemo implements IInterceptorRule { @InterceptorRule("/admin/*") @Before(AdminTypeCheckFilter.class) public void adminAll() { } @Clean @InterceptorRule("/admin/login") public void adminLogin() { } @InterceptorRule("/user/*") public void userAll() { } @InterceptorRule("/mobile/person/*") public void mobilePersonAll() { } }
Description:
@InterceptorRule: Interceptor rule annotation;
- Declared on the class that implements the IInterceptorRule interface, indicating that the class is an interception rule configuration;
- Declared on the class method, indicating the configuration of rules for a specific request path, similar to @RequestMapping;
Supported annotations in rule configuration:
- @Before: The interceptor in the agreed mode only supports @Before pre-interception;
- @Clean: Clean up the interceptor specified by the upper layer;
- @ContextParam: Context parameter;
- @ResponseCache: Declare that the execution result of the view object returned by the controller method will be cached;
Note: The method of configuring the rule class can be arbitrary, and the method itself has no In any sense, it only uses annotations through methods;
URL pseudo-static
The WebMVC module can integrate parameters into the URL through the convention mode, and no longer passes ?
Pass parameters to make the URL look better;
The pseudo-static mode is off by default and needs to be turned on through configuration parameters:
ymp.configs.webmvc.convention_urlrewrite_mode=true
Parameter passing rules:
- Pass multiple request parameters through the separator
_
in the URL;- pass
UrlParams[index]
refers to parameter values;
Pseudo-static example:
URL原始格式: http://localhost:8080/user/info/list?type=all&page=2&page_size=15 URL伪静态格式: http://localhost:8080/user/info/list_all_2_15
Request parameters Reference:
// 通过EL表达式获取参数 ${UrlParams[0]}:all ${UrlParams[1]}:2 ${UrlParams[2]}:15
Note: Pseudo-static parameters must be continuous, and the UrlParams parameter set is stored in the Request scope;
Complete configuration parameters of agreed mode
#------------------------------------- # 约定模式配置参数 #------------------------------------- # 是否开启视图自动渲染(约定优于配置,无需编写控制器代码,直接匹配并执行视图)模式,可选参数,默认值为false ymp.configs.webmvc.convention_mode= # Convention模式开启时视图文件路径(基于base_view_path的相对路径,'-'号代表禁止访问,'+'或无符串代表允许访问),可选参数,默认值为空(即不限制访问路径),多个路径间用'|'分隔 ymp.configs.webmvc.convention_view_paths= # Convention模式开启时是否采用URL伪静态(URL中通过分隔符'_'传递多个请求参数,通过UrlParams[index]方式引用参数值)模式,可选参数,默认值为false ymp.configs.webmvc.convention_urlrewrite_mode= # Convention模式开启时是否采用拦截器规则设置,可选参数,默认值为false ymp.configs.webmvc.convention_interceptor_mode=