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 孙臣龙 [1846080280@qq.com]
 * @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>
相关推荐
Bunny021220 分钟前
SpringMVC笔记
java·redis·笔记
blammmp22 分钟前
Java EE 进阶:Spring MVC(1)
spring·java-ee·mvc
feng_blog66881 小时前
【docker-1】快速入门docker
java·docker·eureka
枫叶落雨2222 小时前
04JavaWeb——Maven-SpringBootWeb入门
java·maven
m0_748232392 小时前
SpringMVC新版本踩坑[已解决]
java
多则惑少则明3 小时前
SSM开发(一)JAVA,javaEE,spring,springmvc,springboot,SSM,SSH等几个概念区别
spring boot·spring·ssh
码农小灰3 小时前
Spring MVC中HandlerInterceptor和Filter的区别
java·spring·mvc
乔木剑衣3 小时前
Java集合学习:HashMap的原理
java·学习·哈希算法·集合
专职4 小时前
spring boot中实现手动分页
java·spring boot·后端
神探阿航4 小时前
第十五届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组
java·算法·蓝桥杯