1、處理器java
一、事件處理器
web
添加類WebEventHandler.java並繼承IWebEventHandlershell
public class WebEventHandler implements IWebEventHandler { public void onInitialized() { } public void onRequestReceived(IRequestContext context) { } public void onRequestCompleted(IRequestContext context) { } public void onDestroyed() { } public void onStartup(ServletContextEvent event) { } public void onShutdown(ServletContextEvent event) { } public void onSessionCreated(HttpSessionEvent event) { } public void onSessionDestroyed(HttpSessionEvent event) { } public void onRequestInitialized(ServletRequestEvent event) { } public void onRequestDestroyed(ServletRequestEvent event) { } }
二、異常處理器
apache
添加類ErrorHandler.java並繼承IWebErrorHandler瀏覽器
public class WebErrorHandler implements IWebErrorHandler { public void onError(Throwable e) { } public IView onConvention(String requestMapping) { return null; } public IView onValidation(Set<ValidateResult> results) { return null; } }
三、修改ymp-conf.properties,添加相關配置tomcat
ymp.configs.webmvc.base.event_handler_class=com.demo.WebEventHandler ymp.configs.webmvc.base.error_handler_class=com.demo.WebErrorHandler
注:ymp-conf.properties中,ymp.module_list(模塊列表)配置項以下mvc
ymp.module_list=configuration|logger|webmvc
2、控制器app
一、修改ymp-conf.properties,添加控制器包掃描路徑ui
ymp.configs.webmvc.base.controller_packages=com.demo.controller
新建控制器包和類對象com.demo.controller.DemoControllerspa
DemoController.java
@Controller public class DemoController { @RequestMapping("hello") @RequestMethod public IView hello(){ return new TextView("Hello,world!"); } }
二、發佈到tomcat並啓動,經過瀏覽器訪問:
http://localhost:8080/ympWeb/hello
三、控制器參數綁定。
在控制器中添加以下代碼:
@RequestMapping("/hello/sayHi") @RequestMethod public IView sayHi(@RequestParam String name) { return new TextView("Hi, " + name); }
部署到tomcat並啓動,經過瀏覽器訪問:
http://localhost:8080/ympWeb/hello/sayHi?name=xiaoming
四、綁定URL參數
在控制器中添加以下代碼:
@RequestMapping("/hello/sayHi/{name}") @RequestMethod public IView sayHi2(@PathVariable String name) { return new TextView("Hi, " + name); }
部署到tomcat並啓動,經過瀏覽器訪問:
http://localhost:8080/ympWeb/hello/sayHi/xiaoming
五、控制器實現參數驗證。
在控制器中添加以下代碼:
@RequestMapping("/valid/{name}/{age}") @RequestMethod @Validation(fullMode = true) public IView paramValidator( @Validate({ @ValidateRule(RequriedValidator.NAME), @ValidateRule(value = LengthValidator.NAME, params = { "min=3", "max=6" }) }) @PathVariable String name, @Validate({ @ValidateRule(RequriedValidator.NAME), @ValidateRule(value = NumericValidator.NAME, params = { "min=18", "max=20" }) }) @PathVariable String age) { return new TextView(""); }
經過瀏覽器訪問http://localhost:8080/ympWeb/hello/sayHi/xiaoming/13,控制檯拋出以下異常:
2014-7-14 16:21:39 org.apache.catalina.core.StandardWrapperValve invoke 嚴重: Servlet.service() for servlet default threw exception net.ymate.platform.validation.ValidationException: [{ fieldName : 'age', message : '長度必須介於18和20之間' }, { fieldName : 'name', message : '長度必須介於3和6之間' }] at net.ymate.platform.mvc.support.RequestExecutor.execute(RequestExecutor.java:151) at net.ymate.platform.mvc.web.support.DispatchHelper.doRequestProcess(DispatchHelper.java:119) at net.ymate.platform.mvc.web.DispatcherFilter.doFilter(DispatcherFilter.java:92) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source)
若但願將驗證信息輸出到瀏覽器,能夠經過WebErrorHandler類中onValidation方法實現,代碼以下:
public IView onValidation(Set<ValidateResult> results) { StringBuilder _sb = new StringBuilder(); for (ValidateResult _result : results) { _sb.append(_result.getMessage()).append("<br/>"); } return new TextView(_sb.toString()); }
再次經過瀏覽器訪問:
3、攔截器
一、建立攔截器類,用於攔截年齡大於19歲,代碼以下:
@Bean public class DemoFilter implements IFilter { public IView doFilter(RequestMeta meta, String params) throws Exception { Integer _age = Integer.parseInt(StringUtils.defaultIfEmpty((String) WebContext.getContext().get("age"), "0")); if (_age > 19) { return new TextView("年齡大於19歲,被攔截"); } return null; } }
工程視圖以下:
二、將@Filter添加到DemoControl類中的paramValidator方法中,修改後代碼以下:
@RequestMapping("/valid/{name}/{age}") @RequestMethod @Validation(fullMode = true) @Filter(DemoFilter.class) public IView paramValidator( @Validate({ @ValidateRule(RequriedValidator.NAME), @ValidateRule(value = LengthValidator.NAME, params = { "min=3", "max=6" }) }) @PathVariable String name, @Validate({ @ValidateRule(RequriedValidator.NAME), @ValidateRule(value = NumericValidator.NAME, params = { "min=18", "max=20" }) }) @PathVariable String age) { return new TextView("你好," + name + ", 你今年" + age + "歲了!"); }
三、經過瀏覽器訪問:
打完,收工!