Spring MVC @RequestParam注解

版权声明:最终解释权归属Hern、HernSong(hernsong)、苍鹭、www.hernsong.com所有! https://blog.csdn.net/qq_36761831/article/details/88887592

简介

@RequestParam注解用于绑定请求参数值,在处理方法入参处使用@RequestParam注解可以把请求参数传递给请求方法。

@RequestParam 的属性

(1)value:请求参数名(必须配置)。

(2)required:是否必需,默认为 true,即 请求中必须包含该参数,如果没有包含,将会抛出异常(可选配置)。

(3)defaultValue:默认值,如果设置了该值,required 将自动设为 false,无论你是否配置了required,配置了什么值,都是 false(可选配置)。

例子:

jsp页面

<%--
  Created by IntelliJ IDEA.
  User: 23369
  Date: 2019/3/24
  Time: 18:29
  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>
    <a href="helloworld_mvc/requestparm?username=Hern&pass=123456">requestparm</a>
    
  </body>
</html>

Java类

package com.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.filter.HiddenHttpMethodFilter;

import javax.swing.text.html.HTMLDocument;

@Controller
@RequestMapping("/helloworld_mvc")
public class HelloWorld {

    @RequestMapping("requestparm")
    public String requestparm(@RequestParam("username") String username, @RequestParam(value = "age", required = false) Integer pass){
        System.out.println(username + "    " + pass);
        return "success";
    }

}

猜你喜欢

转载自blog.csdn.net/qq_36761831/article/details/88887592