04-SpringMVC_请求数据传入

1. 不使用注解

直接给方法入参上写一个和请求参数名相同的变量, 这个变量就来接收请求参数的值, 没值,入参就是null

package com.lz.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @ClassName Demo01_Default
 * @Description: TODO
 * @Author MAlone
 * @Date 2020/2/10
 * @Version V1.0
 **/
@Controller
public class Demo01_Default {

    @RequestMapping("/test01")
    public String test01(String username,Integer age) {
        System.out.println("test01 param:" + username + "," +age);
        return "success";
    }
}

<a href="test01?username=zhangsan&age=10">test01</a>

2. @RequestParam注解

package com.lz.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @ClassName Demo02_RequestParam
 * @Description: TODO
 * @Author MAlone
 * @Date 2020/2/10
 * @Version V1.0
 **/

@Controller
public class Demo02_RequestParam {

    @RequestMapping("/test02")
    public String test02(@RequestParam(value = "username") String username,
                         @RequestParam(value = "age", required = false, defaultValue = "0") Integer age) {

        System.out.println("username:" + username +"age:" + age);
        return "success";
    }
}

3. @RequestHeader注解

package com.lz.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @ClassName Demo03_RequestHeader
 * @Description: TODO
 * @Author MAlone
 * @Date 2020/2/10
 * @Version V1.0
 **/

@Controller
public class Demo03_RequestHeader {
    @RequestMapping("/test03")
    public String test03(@RequestHeader(value = "Accept-Language", required = false, defaultValue = "") String al) {
        System.out.println(al);
        return "success";
    }
}

4. @CookieValue注解

package com.lz.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @ClassName Demo04_CookieValue
 * @Description: TODO
 * @Author MAlone
 * @Date 2020/2/10
 * @Version V1.0
 **/

@Controller
public class Demo04_CookieValue {
    @RequestMapping("/test04")
    public String test04(@CookieValue(value = "JSESSION", required = false, defaultValue = "") String sessionID) {
        System.out.println(sessionID);
        return "success";
    }
}

5. 使用POJO

package com.lz.controller;

import com.lz.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @ClassName Demo05_POJO
 * @Description: TODO
 * @Author MAlone
 * @Date 2020/2/10
 * @Version V1.0
 **/

@Controller
public class Demo05_POJO {

    @RequestMapping("/test05")
    public String test05(User user) {
        System.out.println(user);
        return "success";
    }
}

<!-- 测试 POJO 对象传参,支持级联属性 -->
<form action="test05" method="POST">
    username: <input type="text" name="username"/><br>
    password: <input type="password" name="password"/><br>
    email: <input type="text" name="email"/><br>
    age: <input type="text" name="age"/><br>
    city: <input type="text" name="address.city"/><br>
    province: <input type="text" name="address.province"/>
    <input type="submit" value="Submit"/>
</form>

6. 使用Servlet原生API

package com.lz.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName Demo06_ServletAPI
 * @Description: TODO
 * @Author MAlone
 * @Date 2020/2/10
 * @Version V1.0
 **/

@Controller
public class Demo06_ServletAPI {

    @RequestMapping("/test06")
    public String test06(HttpSession session, HttpServletRequest request) {
        session.setAttribute("msg","success");
        return "success";
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值