码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc 注解 RequestParam/RequestHeader/CookieValue/Pojo

时间:2016-12-14 01:20:22      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:head   override   address   nbsp   实体   lang   code   require   sub   

RequestParam注解:

示例:

@RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(name="username") String un,@RequestParam(name="age",required=false,defaultValue="0") int age) {
        System.out.println("testRequestParam: username="+un+" age="+age);
        return SUCCESS;
    }

RequestHeader注解:

示例:

@RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader(name="Accept-Language") String al) {
        System.out.println("testRequestHeader: al=" + al);
        return SUCCESS;
    }

CookieValue注解:

示例:

@RequestMapping("/testCookieValue")
    public String testCookieValue(@CookieValue(name="JSESSIONID") String sid) {
        System.out.println("testCookieValue:sid="+sid);
        return SUCCESS;
    }

 

Pojo作为参数:

实体:

package com.hy.springmvc.entities;

public class User {
    
    private String username;
    
    private String password;
    
    private String email;
    
    private Address address;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password
                + ", email=" + email + ", address=" + address + "]";
    }
    
}
package com.hy.springmvc.entities;

public class Address {
    
    private String province;
    
    private String city;

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Address [province=" + province + ", city=" + city + "]";
    }
    
}

 

注解方法:

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

页面调用:

<form action="test/testPojo">
    username:<input name="username" type="text"><br>
    password:<input name="password" type="password"><br>
    email:<input name="email" type="text"><br>
    province:<input name="address.province" type="text"><br>
    city:<input name="address.city" type="text"><br>
    <input type="submit" value="submit">
</form>

支持级联属性!!

springmvc 注解 RequestParam/RequestHeader/CookieValue/Pojo

标签:head   override   address   nbsp   实体   lang   code   require   sub   

原文地址:http://www.cnblogs.com/hy87/p/6174167.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!