标签:des style http java color os
一、处理器
1、事件处理器
添加类WebEventHandler.java并继承IWebEventHandler

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) {
}
}
2、异常处理器
添加类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;
}
}
3、修改ymp-conf.properties,添加相关配置
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(模块列表)配置项如下
ymp.module_list=configuration|logger|webmvc
二、控制器
1、修改ymp-conf.properties,添加控制器包扫描路径
ymp.configs.webmvc.base.controller_packages=com.demo.controller
新建控制器包和类对象com.demo.controller.DemoController

DemoController.java
@Controller
public class DemoController {
@RequestMapping("hello")
@RequestMethod
public IView hello(){
return new TextView("Hello,world!");
}
}
2、发布到tomcat并启动,通过浏览器访问:
http://localhost:8080/ympWeb/hello

3、控制器参数绑定。
在控制器中添加如下代码:
@RequestMapping("/hello/sayHi")
@RequestMethod
public IView sayHi(@RequestParam String name) {
return new TextView("Hi, " + name);
}
部署到tomcat并启动,通过浏览器访问:
http://localhost:8080/ympWeb/hello/sayHi?name=xiaoming

4、绑定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

5、控制器实现参数验证。
在控制器中添加如下代码:
@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());
}
再次通过浏览器访问:

三、拦截器
1、创建拦截器类,用于拦截年龄大于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;
}
}
工程视图如下:
2、将@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 + "岁了!");
}
3、通过浏览器访问:


打完,收工!
YMP框架学习笔记(三)------处理器、控制器、拦截器,布布扣,bubuko.com
标签:des style http java color os
原文地址:http://my.oschina.net/u/1864314/blog/290650