Springmvc獲取前臺請求數(shù)據(jù)過程解析
1)基本數(shù)據(jù)類型或String,在方法參數(shù)中定義參數(shù),參數(shù)名與請求傳遞數(shù)據(jù)名一致即可自動封裝;
// RequestMapping:指定方法對應的請求地址
//return:頁面地址,表示方法執(zhí)行完成之后跳轉(zhuǎn)到對應的頁面(轉(zhuǎn)發(fā))
//springmvc:接收請求參數(shù),直接在方法的參數(shù)中定義名稱與傳遞參數(shù)名一致的形參即可
//name:會自動接收請求傳遞的name值
@RequestMapping("/hello")
public String hello(String name,Integer age){
System.out.println("name:"+name+",age:"+age);
return "index.jsp";
}
啟動tomcat,在瀏覽器地址欄輸入
http://localhost:8086/hello?name=tom&age=18
即可查看結(jié)果;
2)對象類型的,在方法參數(shù)中定義對象,與對象屬性名一致的數(shù)據(jù),會自動封裝進對象;
public class User {
private Integer id;
private String name;
private Integer age;
private String sex;
private String addr;
}
并對其get和set,再寫一個form表單提交信息的jsp頁面;
<form action="/saveUser" method="post">
用戶名:<input type="text" name="name"/><br/>
年齡:<input type="text" name="age"/><br/>
性別:<input type="radio" name="sex" value="男"/>男
<input type="radio" name="sex" value="女">女<br/>
地址:<input type="text" name="addr"><br/>
<input type="submit" value="注冊"/>
</form>
再在controller中測試結(jié)果;
@RequestMapping("/saveUser")
public String saveUser(User user){
System.out.println("User:"+user);
return "index.jsp";
}
3)接收數(shù)組的情況。在方法中定義數(shù)組;
4)對象中的數(shù)組和集合可以接收與集合屬性同名的多個請求數(shù)據(jù);
//通過對象來接收前臺的多個同名數(shù)據(jù),兩種方式,一種是數(shù)組,一種是List集合;
//private String[] hobbies;
private List<String> hobbies;
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
jsp頁面中添加愛好屬性:
愛好: <input type="checkbox" name="hobbies" value="打籃球"/>打籃球
<input type="checkbox" name="hobbies" value="打游戲"/>打游戲
<input type="checkbox" name="hobbies" value="敲代碼"/>敲代碼
<input type="checkbox" name="hobbies" value="學習"/>學習<br/>
再在controller中測試結(jié)果;
@RequestMapping("/saveUser")
public String saveAdmin(User user, String[] hobbies){
System.out.println("user:"+user);
System.out.println(Arrays.toString(hobbies));
return "index.jsp";
}
5)對象中的對象。 前臺指定name 時,屬性.屬性;
要測試對象中的對象,那我們要另外再建立一個對象,然后把這個對象當成屬性添加到User.java中去;
public class Role {
private String name;
private Integer id;
@Override
public String toString() {
return "Role{" +
"name='" + name + '\'' +
"id='" + id + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
User.java中:
//用來接收前臺的數(shù)據(jù)
private Role role;
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
jsp文件中:
<%-- 將屬性封裝到對象的對象屬性中 屬性名.屬性--%>
角色:<select name="role.name" id="role">
<option value="管理員">管理員</option>
<option value="超級管理員">超級管理員</option>
<option value="測試賬號">測試賬號</option>
</select><br/>
<input type="hidden" name="role.id" value="100"/><br/>
測試:
public String saveUser(User user, String[] hobbies){
System.out.println("User:"+user);
System.out.println(Arrays.toString(hobbies));
System.out.println("role:"+user.getRole());
return "index.jsp";
}
6)將數(shù)據(jù)封裝到map中。在方法中定義HashMap參數(shù),并在前面添加@RequestParam注解;
7)封裝到對象中的map,前臺指定name為 map屬性名[key值];
User.java中:
//通過map接收前臺的值
private Map<String,String> conditions;
public Map<String, String> getConditions() {
return conditions;
}
public void setConditions(Map<String, String> conditions) {
this.conditions = conditions;
}
jsp文件中:
<%--將數(shù)據(jù)封裝到對象中的map,map屬性名[key]--%>
查詢條件:<input type="text" name="conditions[age]"/><br/>
查詢條件:<input type="text" name="conditions[sex]"/><br/>
<input type="submit" value="注冊"/>
測試:
//map :將所有請求數(shù)據(jù)封裝到map中
// @RequestParam("name"):指定向請求中獲取數(shù)據(jù) ==>request.getParameter
//required = false:是否必傳 defaultValue:默認值
@RequestMapping("/saveUser")
public String saveUser(User user, String[] hobbies,
@RequestParam(name="name",required = false,defaultValue = "username") String username,
@RequestParam HashMap map){
System.out.println("User:"+user);
System.out.println("role:"+user.getRole());
System.out.println(Arrays.toString(hobbies));
System.out.println("map:"+map);
System.out.println("username:"+username); System.out.println("conditions:"+user.getConditions()); return "index.jsp"; }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- SpringMVC詳解如何映射請求數(shù)據(jù)
- Java超詳細講解SpringMVC如何獲取請求數(shù)據(jù)
- 關于SpringMVC請求域?qū)ο蟮臄?shù)據(jù)共享問題
- SpringMVC 重新定向redirect請求中攜帶數(shù)據(jù)方式
- 使用springmvc的controller層獲取到請求的數(shù)據(jù)方式
- Springmvc處理ajax請求并返回json數(shù)據(jù)
- SpringMVC 跨重定向請求傳遞數(shù)據(jù)的方法實現(xiàn)
- SpringMVC解析JSON請求數(shù)據(jù)問題解析
- SpringMVC請求數(shù)據(jù)詳解講解
相關文章
java基于servlet使用組件smartUpload實現(xiàn)文件上傳
這篇文章主要介紹了java基于servlet使用組件smartUpload實現(xiàn)文件上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

