怎样才能制做免费网站,创新的企业网站建设,wordpress短代码显示,北京做兼职从哪个网站好SpringMVC 第四篇【参数绑定详讲、默认支持参数类型、自定义参数绑定、RequestParam 注解】
参数绑定
我们在 Controller 使用方法参数接收值#xff0c;就是把 web 端的值给接收到 Controller 中处理#xff0c;这个过程就叫做参数绑定…
默认支持的参数类型
从上面的用…SpringMVC 第四篇【参数绑定详讲、默认支持参数类型、自定义参数绑定、RequestParam 注解】
参数绑定
我们在 Controller 使用方法参数接收值就是把 web 端的值给接收到 Controller 中处理这个过程就叫做参数绑定…
默认支持的参数类型
从上面的用法我们可以发现我们可以使用 request 对象、Model 对象等等其实是不是可以随便把参数写上去都行其实并不是的…
Controller 方法默认支持的参数类型有 4 个这 4 个足以支撑我们的日常开发了
HttpServletRequestHttpServletResponseHttpSessionModel
参数的绑定过程
一般地我们要用到自定义的参数绑定就是上面所讲的日期类型转换以及一些特殊的需求…. 对于平常的参数绑定我们是无需使用转换器的SpringMVC 就已经帮我们干了这个活了… 自定义绑定参数【版本一】
在上一篇我们已经简单介绍了怎么把字符串转换成日期类型了【使用的是 WebDataBinder 方式】… 其实那是一个比较老的方法我们可以使用 SpringMVC 更推荐的方式…
在上次把字符串转换成日期类型如果使用的是 WebDataBinder 方式的话那么该转换仅仅只能在当前 Controller 使用… 如果想要全部的 Controller 都能够使用那么我们可以使用 WebBindingInitializer 方式
如果想多个 controller 需要共同注册相同的属性编辑器可以实现 PropertyEditorRegistrar 接口并注入 webBindingInitializer 中。
实现接口
public class CustomPropertyEditor implements PropertyEditorRegistrar {Overridepublic void registerCustomEditors(PropertyEditorRegistry binder) {binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(yyyy-MM-dd HH-mm-ss), true));}}配置转换器
注入到 webBindingInitializer 中 !-- 注册属性编辑器 --bean idcustomPropertyEditor classcn.itcast.ssm.controller.propertyeditor.CustomPropertyEditor/bean!-- 自定义webBinder --bean idcustomBinder classorg.springframework.web.bind.support.ConfigurableWebBindingInitializer!-- propertyEditorRegistrars用于属性编辑器 --property namepropertyEditorRegistrarslistref beancustomPropertyEditor //list/property/bean!-- 注解适配器 --bean classorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter!-- 在webBindingInitializer中注入自定义属性编辑器、自定义转换器 --property namewebBindingInitializer refcustomBinder/property/bean自定义参数转换器【版本二】
上面的方式是对象较老的现在我们一般都是实现 Converter 接口来实现自定义参数转换… 我们就来看看实现 Converter 比上面有什么好
配置日期转换器
public class CustomDateConverter implements ConverterString, Date {Overridepublic Date convert(String source) {try {//进行日期转换return new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).parse(source);} catch (Exception e) {e.printStackTrace();}return null;}}配置去除字符串转换器
public class StringTrimConverter implements ConverterString, String {Overridepublic String convert(String source) {try {//去掉字符串两边空格如果去除后为空设置为nullif(source!null){source source.trim();if(source.equals()){return null;}}} catch (Exception e) {e.printStackTrace();}return source;}
}从上面可以得出我们想要转换什么内容就直接实现接口该接口又是支持泛型的阅读起来就非常方便了…
配置转换器 !-- 转换器 -- bean idconversionServiceclassorg.springframework.format.support.FormattingConversionServiceFactoryBean
property nameconverterslistbean classcn.itcast.ssm.controller.converter.CustomDateConverter/bean classcn.itcast.ssm.controller.converter.StringTrimConverter//list
/property
/bean !-- 自定义webBinder -- bean idcustomBinderclassorg.springframework.web.bind.support.ConfigurableWebBindingInitializer !-- 使用converter进行参数转 --
property nameconversionService refconversionService/
/bean !-- 注解适配器 -- bean
classorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter !-- 在webBindingInitializer中注入自定义属性编辑器、自定义转换器 --
property namewebBindingInitializer refcustomBinder/property
/bean 如果是基于 的话我们是这样配置的
mvc:annotation-driven conversion-serviceconversionService
/mvc:annotation-driven
!-- conversionService --bean idconversionService classorg.springframework.format.support.FormattingConversionServiceFactoryBean!-- 转换器 --property nameconverterslistbean classcn.itcast.ssm.controller.converter.CustomDateConverter/bean classcn.itcast.ssm.controller.converter.StringTrimConverter//list/property/beanRequestParam 注解
我们一般使用的参数绑定都有遵循的规则方法参数名要与传递过来的 name 属性名相同。
在默认的情况下只有名字相同SpringMVC 才会帮我们进行参数绑定…
如果我们使用 RequestParam注解的话我们就可以使方法参数名与传递过来的 name 属性名不同…
该注解有三个变量
value【指定 name 属性的名称是什么】required【是否必须要有该参数】defaultvalue 设置默认值
例子我们的方法参数叫 id而页面带过来的 name 属性名字叫 item_id一定需要该参数
public String editItem(RequestParam(valueitem_id,requiredtrue) String id) {}