SpringMVC---RequestParam注解

一:请求处理方法签名

  • SpringMVC通过分析处理方法的签名,将HTTP请求信息绑定到处理方法的入参中
  • Spring MVC对控制器处理方法的签名限制是很宽松的,几乎可以按照喜欢的任何方式对方法进行签名
  • 必要时可以对方法及方法的入参标注相应的注解(@RequestParam,@PathVariable,@RequestHeader等),SpringMVC框架会将HTTP请求信息绑定到相应方法的入参中,并根据方法的返回值类型做出相应的后续处理。

二:代码实现

1.测试方法

package com.dhx.handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import jdk.nashorn.internal.ir.RuntimeNode.Request;


@RequestMapping("spring")
@Controller
public class SpringTest {
	
	private final String SUCCESS="success";
	
	/*
	 * @RequestParam  来映射请求参数
	 * value  值即对应请求参数的参数名
	 * required   该参数是否必须,默认为true
	 * defaultValue  请求参数默认值
	 */
	@RequestMapping("RequestParam")
	public String testRequestParam(@RequestParam(value="username") String username,
			@RequestParam(value="age",required=false,defaultValue="0") int age) {
		System.out.println("testRequestParam username= "+username+",age="+age);
		return SUCCESS;
		
	}

}

2.测试页面

<a href="spring/RequestParam?username=dhx&age=12">test RequestParam</a>
发布了64 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39093474/article/details/103698954
今日推荐