原始的提交方式:
需要在链接上写入跟我们的变量一样的名字:
http://localhost:8080/test1?name=zsp
public class ControllerTest3 {
@GetMapping("/test1")
public String test1(String name, Model model){
model.addAttribute("msg",name);
return "test";
}
使用requestParam可以使用我们想要用的传入参数:
http://localhost:8080/test2?name=zsp
// 用@RequestParam对其进行修饰,传参只能用username
@GetMapping("/test2")
public String test2(@RequestParam("username") String name, Model model){
model.addAttribute("msg",name);
return "test";
}
使用requestParam可以使用我们想要用的传入参数:
传输一个对象,对应的对象数据类型必须保持一致
// 传输一个对象,对应的对象数据类型必须保持一致
// http://localhost:8080/test3?id=1&name=zsp&age=21
@GetMapping("/test3")
public String test3(User user, Model model){
model.addAttribute("msg",user);
return "test";
}
对应的全部代码:
package com.zsp.controller;
import com.zsp.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class ControllerTest3 {
@GetMapping("/test1")
public String test1(String name, Model model){
model.addAttribute("msg",name);
return "test";
}
// 用@RequestParam对其进行修饰,传参只能用username
@GetMapping("/test2")
public String test2(@RequestParam("username") String name, Model model){
model.addAttribute("msg",name);
return "test";
}
// 传输一个对象,对应的对象数据类型必须保持一致
// http://localhost:8080/test3?id=1&name=zsp&age=21
@GetMapping("/test3")
public String test3(User user, Model model){
model.addAttribute("msg",user);
return "test";
}
}
以及jsp页面:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/9/5
Time: 0:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>