当前位置: 移动技术网 > IT编程>开发语言>Java > SpringMVC 中,当前台传入多个参数时,可将参数封装成一个bean类

SpringMVC 中,当前台传入多个参数时,可将参数封装成一个bean类

2018年10月24日  | 移动技术网IT编程  | 我要评论

在实际业务场景中,当前台通过 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();
    }

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网