SpringMVC 中,當前台傳入多個參數時,可將參數封裝成一個bean類


在實際業務場景中,當前台通過 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();
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM