Spring MVC参数的接收方式!!!

参考入门案例:Spring MVC入门案例!!!-CSDN博客

1.接收基本数据类型:通常用于参数较少的情况。

在index.jsp中定义超链接:

html 复制代码
<a href="/account/findAccount7?name=mike&age=18">接收基本数据类型</a>

编写AccountController:

java 复制代码
@Controller
@RequestMapping("/account")
public class AccountController {
   
 @GetMapping("/findAccount7")
    public String findAccount7(String name, Integer age, Model model) throws UnsupportedEncodingException {
        //get请求解决乱码问题
        byte[] bytes = name.getBytes(StandardCharsets.ISO_8859_1);
        name = new String(bytes, StandardCharsets.UTF_8);
        model.addAttribute("msg", "通过url传参:" + name + "---" + age);
        return "success";
    }
}

2.通过pojo实体类进行接收:通常用于参数较多的情况。

实体类:Account

java 复制代码
package com.by.pojo;

import java.io.Serializable;
import java.util.Date;

public class Account implements Serializable {
    private Integer id;
    private String name;
    private Float money;
    private Address address;
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
//省略get set toString方法


    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                ", address=" + address +
                ", date=" + date +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    public Address getAddress() {
        return address;
    }

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

实体类:Address

java 复制代码
package com.by.pojo;

import java.io.Serializable;

public class Address implements Serializable {
    private String provinceName;
    private String cityName;
     //省略get set toString方法

    @Override
    public String toString() {
        return "Address{" +
                "provinceName='" + provinceName + '\'' +
                ", cityName='" + cityName + '\'' +
                '}';
    }

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }
}

编写AccountController

java 复制代码
package com.by.controller;

import com.by.pojo.Account;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/account")
public class AccountController {

    @RequestMapping("/saveAccount")
    public String saveAccount(Account account, Model model){
        model.addAttribute("msg", account);
        return "success";
    }
}

在index.jsp中添加表单:

html 复制代码
<form action="account/saveAccount" method="post">
    账户名称:<input type="text" name="name"><br/>
    账户金额:<input type="text" name="money"><br/>
    账户省份:<input type="text" name="address.provinceName"><br/>
    账户城市:<input type="text" name="address.cityName"><br/>
    <input type="submit" value="保存">
</form>

3.采用restful风格的方式进行接收

在index.jsp中添加超链接

html 复制代码
<br><a href="/account/findAccount10/小美/18">restful</a><br>

在AccountController中编写方法:通过在参数列表中使用@PathVariable

java 复制代码
/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.controller;

import com.by.pojo.Account;
import com.by.pojo.JsonStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * <p>Project: SpringMVC - AccountController</p>
 * <p>Powered by scl On 2024-01-09 10:07:21</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [[email protected]]
 * @version 1.0
 * @since 17
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @GetMapping("/findAccount10/{name}/{age}")
    public String findAccount10(@PathVariable String name, @PathVariable Integer age, Model model) {
        byte[] bytes = name.getBytes(StandardCharsets.ISO_8859_1);
        name = new String(bytes, StandardCharsets.UTF_8);
        model.addAttribute("msg", "restful:" + name + ":" + age);
        return "success";
    }
}

4.使用ServletAPI接收参数

编写controller

java 复制代码
@Controller
@RequestMapping("/account")
public class AccountController {

    @RequestMapping("/findAccount8")
    public String findAccount8(HttpServletRequest request, 
                               HttpServletResponse response){
        String username = request.getParameter("name");
        String age = request.getParameter("age");
        request.setAttribute("msg",username+" "+age);
        return "success";
    }
}

在index.jsp里面定义超链接

html 复制代码
<a href="/account/findAccount8?username=eric&age=19">Servlet接收参数</a>
相关推荐
mghio4 小时前
Dubbo 中的集群容错
java·微服务·dubbo
咖啡教室9 小时前
java日常开发笔记和开发问题记录
java
咖啡教室9 小时前
java练习项目记录笔记
java
鱼樱前端10 小时前
maven的基础安装和使用--mac/window版本
java·后端
RainbowSea10 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea10 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
李少兄12 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
此木|西贝12 小时前
【设计模式】原型模式
java·设计模式·原型模式
可乐加.糖12 小时前
一篇关于Netty相关的梳理总结
java·后端·网络协议·netty·信息与通信