包装类型pojo参数绑定
需求
实现方法
第一种方法:在形参中添加HttpServletRequest request参数,通过request接受查询条件参数。第二种方法:在形参中让包装类型的pojo接受查询条件参数。
分析:
页面传参数的特点:复杂,多样性。条件包括:用户账号、商品编号、订单信息。。。
如果将用户账号、商品编号、订单信息等放在简单pojo(属性是简单类型)中,pojo类属性比较多,比较乱。
建议使用包装类型的pojo,pojo中属性是pojo。
页面参数和controller方法形参定义
页面参数:商品名称:<input name="itemsCustom.name" />
注意:itemsCustom和包装pojo中的属性一致即可。
controller方法中的形参:
public ModelAndView queryItems(HttpServletRequest request,ItemsQueryVo itemsQueryVo){
通过继承和扩展就能够找到name的属性。继而将name属性传入到ItemsQueryVo中。
集合类型绑定
数组绑定
public String deleteItems(Integer[] items_id) throws Exception{
传入相同的参数
list绑定
通常在需要批量提交数据时,将提交的数据绑定到list<pojo>中,比如:成绩录入(录入多门课成绩,批量提交),
本例子需求:批量商品修改,在页面输入多个商品信息,将多个商品信息提交到controller方法中。
定义函数的参数为pojo的类型,上面是ItemsQueryVo的参数
@RequestMapping("editItemsSuccess")
public String editItemsSuccess(ItemsQueryVo itemsQueryVo) throws Exception{
页面传递参数
<c:forEach items="${itemsList }" var="item" varStatus="status">
<tr>
<td><input name="itemsList[${ status.index}].name" value="${item.name }"/></td>
<td><input name="itemsList[${ status.index}].price" value="${item.price }"/></td>
<td><input name="itemsList[${ status.index}].createtime" value="<fmt:formatDate value="${item.createtime}"pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
<td><input name="itemsList[${ status.index}].detail" value="${item.detail }"/></td>
</tr>
springmvc校验
项目中,通常使用较多是前端的校验,比如页面中js校验。对于安全要求较高点建议在服务端进行校验。
服务端校验:
控制层conroller:校验页面请求的参数的合法性。在服务端控制层conroller校验,不区分客户端类型(浏览器、手机客户端、远程调用)
业务层service(使用较多):主要校验关键业务参数,仅限于service接口中使用的参数。
持久层dao:一般是不校验的。
springmvc校验需求
springmvc使用hibernate的校验框架validation(和hibernate没有任何关系)。
校验思路:
页面提交请求的参数,请求到controller方法中,使用validation进行校验。如果校验出错,将错误信息展示到页面。
具体需求:
商品修改,添加校验(校验商品名称长度,生产日期的非空校验),如果校验出错,在商品修改页面显示错误信息。
hibernate的校验框架validation所需要jar包:
配置校验器(springmvc.xml)
<!--校验器 -->
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<!--校验器-->
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<!--指定校验使用的资源文件,如果不指定则默认使用classpath下的ValidationMessages.properties -->
<property name="validationMessageSource" ref="messageSource" />
</bean>
<!--校验错误信息配置文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!--资源文件名-->
<property name="basenames">
<list>
<value>classpath:CustomValidationMessages</value>
</list>
</property>
<!--资源文件编码格式 -->
<property name="fileEncodings" value="utf-8" />
<!--对资源文件内容缓存时间,单位秒 -->
<property name="cacheSeconds" value="120" />
</bean>
将validator加到处理器适配器
<mvc:annotation-driven conversion-service="conversionService" validator="validator"></mvc:annotation-driven>
方式2:
<!-- 自定义webBinder -->
<bean id="customBinder"
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator" ref="validator" />
</bean>
<!-- 注解适配器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="customBinder"></property>
</bean>
添加验证规则
public class Items {
private Integer id;
@Size(min=1,max=30,message="{item.name.length.error}")
private String name;
@NotEmpty(message="{pic.is.null}")
private String pic;
添加错误信息classpath:CustomValidationMessages.properties
item.name.length.error=商品名称在1到30个字符之间
pic.is.null=请上传图片
在需要验证的controller中添加:
@RequestMapping("editItemsSubmit")
public String editItemsSubmit(Integer id,@Validated ItemsCustom itemsCustom,BindingResult bindingResult) throws Exception{
if(bindingResult.hasErrors()){
List<ObjectError> listObject = bindingResult.getAllErrors();
for (ObjectError objectError : listObject) {
System.out.println(objectError.getDefaultMessage());
}
}
定义分组
使用分组:
@Size(min=1,max=20,message="{item.name.length.error}",groups={ValidatorGroup1.class})
private String name;
在页面显示相应的validation内容:
<!-- 输出错误信息 -->
<c:if test="${listObject!=null}">
<c:forEach items="${listObject}" var="error">
${error.defaultMessage}
</c:forEach>
</c:if>