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";
}
}