在实际业务场景中,当前台通过 url 向后台传送多个参数时,可以将参数封装成一个bean类,在bean类中对各个参数进行非空,默认值等的设置。
前台 url ,想后台传送两个参数,userName 和 password:
http://localhost:8082/web/baseAction.do?pathVar=app/task/fetchItemDetail.do?userName=123&password=123
将参数封装成bean 类,并在bean类中对参数进行控制:
import org.hibernate.validator.constraints.NotEmpty;
/**
* Created by thinkpad on 2017/10/23.
*/
public class QueryCondition {
@NotEmpty(message = "validator.userName")
private String userName ;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
if (null == password || password.length() == 0){
password = "123456";
}
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
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();
}