java中的前台传入参数检验_SpringMVC 中,当前台传入多个参数时,可将参数封装成一个bean类...

本文介绍了如何在Java中通过QueryCondition类封装HTTP请求参数,使用Hibernate Validator进行非空校验,并在Controller中接收并处理这些参数。实例演示了如何构造URL、创建Bean和参数校验的过程。
摘要由CSDN通过智能技术生成

在实际业务场景中,当前台通过 url 向后台传送多个参数时,可以将参数封装成一个bean类,在bean类中对各个参数进行非空,默认值等的设置。

前台 url ,想后台传送两个参数,username 和 password:

1 http://localhost:8082/web/baseaction.do?pathvar=app/task/fetchitemdetail.do?username=123&password=123

将参数封装成bean 类,并在bean类中对参数进行控制:

1

2

3 import org.hibernate.validator.constraints.notempty;

4

5 /**

6 * created by thinkpad on 2017/10/23.

7 */

8 public class querycondition {

9

10 @notempty(message = "validator.username")

11 private string username ;

12 private string password;

13

14 public string getusername() {

15 return username;

16 }

17

18 public void setusername(string username) {

19 this.username = username;

20 }

21

22 public string getpassword() {

23 if (null == password || password.length() == 0){

24 password = "123456";

25 }

26 return password;

27 }

28

29 public void setpassword(string password) {

30 this.password = password;

31 }

32 }

controller 中的接收、打印及去除当前传入的参数:

@requestmapping("/fetchitemdetail.do")

@responsebody

public string fetchitemdetail(querycondition condition) {

jsonobject json = new jsonobject();

system.out.println(reflectiontostringbuilder.tostring(condition, tostringstyle.multi_line_style));

system.out.println("conditionusername = [" + condition.getusername() + "], " +

"conditionpassword = [" + condition.getpassword() + "]");

return json.tojsonstring();

}

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值