在Spring验证器接口中调用request.setattribute

时间:2017-07-11 19:52:58

标签: spring jsp spring-validator

如何在spring custom request.setattribute类中访问validator。我需要在jsp方设置这些值,我正在尝试下面的内容

@Component
public class ProductSearchValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return Product.class.isAssignableFrom(clazz);  
    }

    @Override
    public void validate(Object target, Errors errors) {
        Product product = (Product) target;
        String name = product.getName();
        String cod="Validated";

        request.setAttribute("isVal",cod);

    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过这种方式访问​​请求

RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
if (attrs instanceof ServletRequestAttributes) {
    HttpServletRequest request = ((ServletRequestAttributes)attrs).getRequest();
}