Linux 拨号vps windows公众号手机端

怎么使用springmvc注解隐藏字段值

lewis 8年前 (2017-10-27) 阅读数 6 #程序编程
文章标签 springmvc

在Spring MVC中,可以使用注解`@ModelAttribute`和`@InitBinder`来隐藏字段值。下面是一种常见的方法:

1. 在控制器类中创建一个`initBinder()`方法,并用`@InitBinder`注解标记该方法,用于初始化绑定器。

@Controller
publicclassMyController{

@InitBinder
protectedvoidinitBinder(WebDataBinderbinder){
//隐藏指定字段值
binder.setDisallowedFields("fieldName");
}
//其他处理方法...
}

2. 在需要隐藏字段值的表单对象中,添加`@ModelAttribute`注解。

publicclassMyForm{
privateStringfieldName;
//getter和setter方法
@ModelAttribute("fieldName")
publicStringgetFieldName(){
return"hiddenvalue";
}
}

在上述代码中,`getFieldName()`方法被`@ModelAttribute`注解标记,并返回了需要隐藏的字段的值。当表单提交时,这个字段的值将被隐藏。

请注意,在使用`@ModelAttribute`注解时,要确保命名与表单对象的字段名称相匹配,以便正确隐藏字段的值。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门