@InitBinder
在controller中注册一个customer protperty editor以解析request中的参数并通过date bind机制与handler method中的参数做绑定。
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, false));
}
Handler method代码如下
@RequestMapping("/databind1")
public ModelAndView databind1(Date date) {
…
}
访问url http://localhost:8080/springmvc/databind1.action?date=2000-01-02
通过initbinder中注册的customeDateEditor类型,自动将2000-01-02转换为日期类型
@ResponseStatus
返回一个指定的http response状态码。
例如
@ResponseStatus(reason="no reason",value=HttpStatus.BAD_REQUEST)
@RequestMapping("/responsestatus")
public void responseStatusTest(){
}
将返回 Http error 400.
@SessionAttributes
相当于以前的requiresSession属性,当配置此属性时,handler method中对应的参数将从session中获取,如果不存在则抛出SessionRequiredException,与之前提到的SessionStatus. setComplete ()一起使用,可起到避免提及成功后按刷新键重复提交的情况。
@ExceptionHandler
@RequestMapping("/exception")
public void ExceptionTest() throws Exception{
throw new Exception("i don't know");
}
@ExceptionHandler
public String handleException(Exception e,HttpServletRequest request){
System.out.println(e.getMessage());
return "helloworld";
}
其中/exception抛出一个异常,而handleException则抓到这个异常并进行处理
分享到:
相关推荐
工程文件 博文链接:https://starscream.iteye.com/blog/1063966
spring mvc的学习笔记,主要讲解了基于xml的配置,和如何增加spring mvc拦截器,如何返回json报文
主要给大家介绍了关于Spring MVC学习笔记之Controller查找(基于Spring4.0.3)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
springboot学习笔记 spring基础 Spring概述 Spring的简史 xml配置 注解配置 java配置 Spring概述 Spring的模块 核心容器CoreContainer Spring-Core Spring-Beans ...
到现在,我们已经基本搭建起了项目的框架,但是项目中还存在一个问题,就是尽管层与层之间使用了接口进行隔离,但实例化接口的时候,还是引入了接口实现类的依赖,如下面的代码: private IUserService _...
达内云笔记,是达内在线教育平台(tmooc)中的一个子项目, 用于在学员学习时,随时的记录笔记、收藏笔记、分享笔记 以及参加笔记相关的活动。期望达到一个无时不学习,不处 不笔记的目标。 2、技术架构 1...
达内云笔记,是达内在线教育平台(tmooc)中的一个子项目, 用于在学员学习时,随时的记录笔记、收藏笔记、分享笔记 以及参加笔记相关的活动。期望达到一个无时不学习,不处 不笔记的目标。 2、技术架构 1...
SpringMVC1,回顾MVC1.1,什么是MVC MVC是模型(Model),视图(View),控制器(Controller)的简写,是一种软件设计规范。是将数据,显示,业务逻辑分离的方法来组织代码。 MVC主要作用是降低了视图与业务逻辑间的...
系统搭建:采用当前软件行业比较流行的JavaWeb技术,运用MVC分层设计思想,使用Hibernate+Spring+SpringMVC来搭建环境,设计实体类(entity)、数据库访问类(dao)、服务类(service)、控制层类(web/controller)...