WebMVC
WebMVC 模块在 YMP 框架中是除了 JDBC 持久化模块以外的另一个非常重要的模块,集成了 YMP 框架的诸多特性,在功能结构的设计和使用方法上依然保持一贯的简单风格,同时也继承了主流 MVC 框架的基因,对于了解和熟悉 SSH 或 SSM 等框架技术的开发人员来说,上手极其容易,毫无学习成本。
其主要功能特性如下:
- 标准 MVC 实现,结构清晰,完全基于注解方式配置简单;
- 支持约定模式,无需编写控制器代码,直接匹配并执行视图渲染;
- 支持多种视图技术(Binary、Forward、Freemarker、HTML、HttpStatus、JSON、JSP、Redirect、Text、Velocity 等);
- 支持 RESTful 模式及 URL 风格;
- 支持请求参数与控制器方法参数的自动绑定;
- 支持参数有效性验证;
- 支持控制器方法的拦截;
- 支持注解配置控制器请求路由映射;
- 支持自动扫描控制器类并注册;
- 支持事件和异常的自定义处理;
- 支持I18N资源国际化;
- 支持控制器方法和视图缓存;
- 支持插件扩展;
Maven包依赖
<dependency>
<groupId>net.ymate.platform</groupId>
<artifactId>ymate-platform-webmvc</artifactId>
<version>2.1.4-dev</version>
</dependency>
模块初始化
在 Web 程序中监听器(Listener)是最先被容器初始化的,所以 WebMVC 模块是由监听器负责对 YMP 框架进行初始化:
监听器(Listener):net.ymate.platform.webmvc.support.WebAppEventListener
处理浏览器请求并与模块中控制器匹配、路由的过程可分别由过滤器(Filter)和服务端程序(Servlet)完成:
过滤器(Filter):net.ymate.platform.webmvc.support.DispatchFilter
服务端程序(Servlet):net.ymate.platform.webmvc.support.DispatchServlet
以下为完整的 web.xml 配置文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="ymate-cms-webapp" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<listener>
<listener-class>net.ymate.platform.webmvc.support.WebAppEventListener</listener-class>
</listener>
<filter>
<filter-name>GeneralWebFilter</filter-name>
<filter-class>net.ymate.platform.webmvc.support.GeneralWebFilter</filter-class>
<init-param>
<param-name>responseHeaders</param-name>
<!--
HTTP 响应头信息中的 X-Frame-Options,可以指示浏览器是否应该加载一个 iframe 中的页面。
如果服务器响应头信息中没有 X-Frame-Options,则该网站存在 ClickJacking 攻击风险。
网站可以通过设置 X-Frame-Options 阻止站点内的页面被其他页面嵌入从而防止点击劫持。
添加 X-Frame-Options 响应头,赋值有如下三种:
1、DENY: 不能被嵌入到任何iframe或者frame中。
2、SAMEORIGIN: 页面只能被本站页面嵌入到iframe或者frame中。
3、ALLOW-FROM uri: 只能被嵌入到指定域名的框架中。
-->
<param-value>X-Frame-Options=SAMEORIGIN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>GeneralWebFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>DispatchFilter</filter-name>
<filter-class>net.ymate.platform.webmvc.support.DispatchFilter</filter-class>
<!--
参数项 "requestIgnoreUrls" 用于配置需要排除的请求地址前缀,多个前缀之间用 "|" 分隔。
-->
<!--
<init-param>
<param-name>requestIgnoreUrls</param-name>
<param-value>/service1|/serviceN</param-value>
</init-param>
-->
</filter>
<filter-mapping>
<filter-name>DispatchFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!--
<servlet>
<servlet-name>DispatchServlet</servlet-name>
<servlet-class>net.ymate.platform.webmvc.support.DispatchServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatchServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
-->
<!--
OPTIONS 方法是用于请求获得由 Request-URI 标识的资源在请求/响应的通信过程中可以使用的功能选项。
通过这个方法,客户端可以在采取具体资源请求之前,决定对该资源采取何种必要措施,或者了解服务器的性能。
OPTIONS 方法可能会暴露一些敏感信息,这些信息将帮助攻击者准备更进一步的攻击。
-->
<!--
<security-constraint>
<web-resource-collection>
<web-resource-name>RequestMethodAllowed</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method-omission>GET</http-method-omission>
<http-method-omission>POST</http-method-omission>
<http-method-omission>OPTIONS</http-method-omission>
<http-method-omission>PUT</http-method-omission>
<http-method-omission>HEAD</http-method-omission>
<http-method-omission>TRACE</http-method-omission>
<http-method-omission>DELETE</http-method-omission>
<http-method-omission>SEARCH</http-method-omission>
<http-method-omission>COPY</http-method-omission>
<http-method-omission>MOVE</http-method-omission>
<http-method-omission>PROPFIND</http-method-omission>
<http-method-omission>PROPPATCH</http-method-omission>
<http-method-omission>MKCOL</http-method-omission>
<http-method-omission>LOCK</http-method-omission>
<http-method-omission>UNLOCK</http-method-omission>
</web-resource-collection>
<auth-constraint/>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>400</error-code>
<location>/WEB-INF/templates/error.jsp?status=400</location>
</error-page>
<error-page>
<error-code>401</error-code>
<location>/WEB-INF/templates/error.jsp?status=401</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/templates/error.jsp?status=404</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/WEB-INF/templates/error.jsp?status=405</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/templates/error.jsp?status=500</location>
</error-page>
</web-app>
模块配置
配置文件参数说明
基本参数配置
#-------------------------------------
# WebMVC模块初始化参数
#-------------------------------------
# 控制器请求映射路径分析器, 可选值为已知分析器名称或自定义分析器类名称, 默认值: default, 目前支持已知分析 器[default|...]
ymp.configs.webmvc.request_mapping_parser_class=
# 控制器请求处理器, 可选值为已知处理器名称或自定义处理器类名称, 自定义类需实现net.ymate.platform.webmvc.IRequestProcessor接口, 默认值: default, 目前支持已知处理器[default|json|xml|...]
ymp.configs.webmvc.request_processor_class=
# 异常错误处理器, 可选参数, 默认值: net.ymate.platform.webmvc.impl.DefaultWebErrorProcessor
ymp.configs.webmvc.error_processor_class=
# 缓存处理器, 可选参数, 此类需实现net.ymate.platform.webmvc.IWebCacheProcessor接口, 默认值: net.ymate.platform.webmvc.support.WebCacheProcessor
ymp.configs.webmvc.cache_processor_class=
# 默认字符编码集设置, 可选参数, 默认值: UTF-8
ymp.configs.webmvc.default_charset_encoding=
# 默认Content-Type设置, 可选参数, 默认值: text/html
ymp.configs.webmvc.default_content_type=
# 国际化资源文件存放路径, 可选参数, 默认值: ${root}/i18n/
ymp.configs.webmvc.resources_home=
# 国际化资源文件名称, 可选参数, 默认值: messages
ymp.configs.webmvc.resource_name=
# 国际化语言设置参数名称, 可选参数, 默认值: _lang
ymp.configs.webmvc.language_param_name=
# 请求忽略后缀集合, 可选参数, 默认值: jsp|jspx|png|gif|jpg|jpeg|js|css|swf|ico|htm|html|eot|woff|woff2|ttf|svg|map
ymp.configs.webmvc.request_ignore_regex=
# 请求方法参数名称, 可选参数, 默认值: _method
ymp.configs.webmvc.request_method_param=
# 请求路径前缀, 可选参数, 默认值: 空
ymp.configs.webmvc.request_prefix=
# 请求路径匹配是否启用严格模式,默认值: false
ymp.configs.webmvc.request_strict_mode_enabled=
# 控制器视图文件基础路径(必须是以 '/' 开始和结尾), 默认值: /WEB-INF/templates/
ymp.configs.webmvc.base_view_path=
#-------------------------------------
# Cookie相关参数配置
#-------------------------------------
# Cookie键前缀, 可选参数, 默认值: 空
ymp.configs.webmvc.cookie_prefix=
# Cookie作用域, 可选参数, 默认值: 空
ymp.configs.webmvc.cookie_domain=
# Cookie作用路径, 可选参数, 默认值: /
ymp.configs.webmvc.cookie_path=
# Cookie密钥, 可选参数, 默认值: 空
ymp.configs.webmvc.cookie_auth_key=
# Cookie密钥验证是否默认开启, 默认值: false
ymp.configs.webmvc.cookie_auth_enabled=
# Cookie是否默认使用HttpOnly, 默认值: false
ymp.configs.webmvc.cookie_use_http_only=
#-------------------------------------
# 文件上传相关参数配置
#-------------------------------------
# 文件上传临时目录, 为空则默认使用:System.getProperty("java.io.tmpdir")
ymp.configs.webmvc.upload_temp_dir=
# 上传文件数量最大值,默认值:-1
ymp.configs.webmvc.upload_file_count_max=
# 上传文件大小最大值(字节), 默认值: -1(注: 10485760 = 10M)
ymp.configs.webmvc.upload_file_size_max=
# 上传文件总量大小最大值(字节), 默认值: -1(注:10485760 = 10M)
ymp.configs.webmvc.upload_total_size_max=
# 内存缓冲区的大小, 默认值: 10240字节(=10K), 即如果文件大于10K, 将使用临时文件缓存上传文件
ymp.configs.webmvc.upload_size_threshold=
# 文件上传状态监听器, 可选参数, 此类需实现org.apache.commons.fileupload.ProgressListener接口, 默认值: 空
ymp.configs.webmvc.upload_listener_class=
说明:
-
在服务端程序(Servlet)方式的请求处理中,请求忽略正则表达式
request_ignore_regex参数无效。 -
在配置
request_ignore_regex参数时,可以通过首元素为~符号实现继承,即在默认值的基础上追加。例如:为默认请求忽略后缀集合增加
xml和json后缀。ymp.configs.webmvc.request_ignore_regex=~|xml|json