springmvc处理提交数据

原始的提交方式:
需要在链接上写入跟我们的变量一样的名字:
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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值