简单的参数绑定
要求:请求的参数名要与响应请求的方法的参数名相同
@RequestMapping("/testParam")
public String testRequestParamsBinding(String username, String password) {
System.out.println("用户名:" + username);
System.out.println("密码:" + password);
return "success";
}
<a href="params/testParam?username=dudu&password=dudu123">请求参数绑定</a>
将数据封装到JavaBean
要求:请求的参数名与JavaBean中的属性名相同,且属性一定有对应的set方法
@RequestMapping("/saveAccount")
public String testSaveAccount(Account account) {
System.out.println(account);
return "success";
}
/*
Account中的属性:
private String userName;
private String password;
private double money;
*/
<form action="params/saveAccount" method="post">
用户名:<input type="text" name="username"/> <br>
密码:<input type="text" name="password"/> <br>
金额:<input type="text" name="money"/> <br>
<input type="submit" value="提交">
</form>
JavaBean中含有引用类型
要求:参数名要写成【引用类型名.属性名】
@RequestMapping("/saveAccount")
public String testSaveAccount(Account account) {
System.out.println(account);
return "success";
}
/*
Account中的属性:
private String userName;
private String password;
private double money;
private User user;
User中的属性:
private String name;
private Integer age;
*/
<form action="params/saveAccount" method="post">
用户名:<input type="text" name="username"/> <br>
密码:<input type="text" name="password"/> <br>
金额:<input type="text" name="money"/> <br>
姓名:<input type="text" name="user.name"/> <br>
年龄:<input type="text" name="user.age"/> <br>
<input type="submit" value="提交">
</form>
JavaBean中含有集合类型
要求:参数名要写成【引用类型名[xx].属性名】
@RequestMapping("/saveAccount")
public String testSaveAccount(Account account) {
System.out.println(account);
return "success";
}
/*
Account中的属性:
private String userName;
private String password;
private double money;
private List<User> list;
private Map<String, User> map;
User中的属性:
private String name;
private Integer age;
*/
<form action="params/saveAccount" method="post">
用户名:<input type="text" name="username"/> <br>
密码:<input type="text" name="password"/> <br>
金额:<input type="text" name="money"/> <br>
<%-- 存到list中 --%>
姓名:<input type="text" name="list[0].name"/> <br>
年龄:<input type="text" name="list[0].age"/> <br>
<%-- 存到map中 --%>
姓名:<input type="text" name="map['1'].name"/> <br>
年龄:<input type="text" name="map['1'].age"/> <br>
<input type="submit" value="提交">
</form>
获取Servlet原生的API
@RequestMapping("/servletApi")
public String testServletAPI(HttpServletRequest request, HttpServletResponse response) {
System.out.println("request: " + request);
System.out.println("response: " + response);
return "success";
}
<a href="params/servletApi">获取Servlet原生的API</a>