springmvc 獲取前台請求數據方式


springmvc 獲取前台請求數據方式

1)基本數據類型或String,在方法參數中定義參數,參數名與請求傳遞數據名一致即可自動封裝;

    // RequestMapping:指定方法對應的請求地址
    //return:頁面地址,表示方法執行完成之后跳轉到對應的頁面(轉發)
    //springmvc:接收請求參數,直接在方法的參數中定義名稱與傳遞參數名一致的形參即可
    //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
即可查看結果;

2)對象類型的,在方法參數中定義對象,與對象屬性名一致的數據,會自動封裝進對象;

 
         
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中測試結果;

        @RequestMapping("/saveUser")
        public String saveUser(User user){
              System.out.println("User:"+user);
return "index.jsp";
}

3)接收數組的情況。在方法中定義數組;

4)對象中的數組和集合可以接收與集合屬性同名的多個請求數據;

    //通過對象來接收前台的多個同名數據,兩種方式,一種是數組,一種是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中測試結果;

    @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中:
  //用來接收前台的數據
    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)將數據封裝到map中。在方法中定義HashMap參數,並在前面添加@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文件中:
    <%--將數據封裝到對象中的map,map屬性名[key]--%>
    查詢條件:<input type="text" name="conditions[age]"/><br/>
    查詢條件:<input type="text" name="conditions[sex]"/><br/>
    <input type="submit" value="注冊"/>

測試:

//map :將所有請求數據封裝到map中
    // @RequestParam("name"):指定向請求中獲取數據  ==>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";
}

 


免責聲明!

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



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