在实际业务场景中,当前台通过 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(); }
原文地址:https://www.cnblogs.com/huigee/p/9842391.html
时间: 2024-11-05 16:34:54