@RequestParam--SpringMVC 注解系列文章(一)
- 概述
RequestParam 注解是使用 SpringMVC 开发过程中,比较常用的一个注解,用于映射请求参数。
- 代码
package rex.springmvc.handlers; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @RequestMapping(value="/requestParamTest") @Controller public class RequestParamTest { private static final Logger logger = Logger.getLogger(RequestParamTest.class); private static final String SUCCESS = "success"; /** * @RequestParam 注解可以用来映射请求参数 * value 值即为请求参数名称 * required 表示请求参数是否允许为空,默认值是true * defaultValue 值为请求参数的默认值 * * @param userNm * @param age * @return */ @RequestMapping(value="/testRequestParam", method=RequestMethod.GET) public String testRequestParam(@RequestParam(value="userNm") String userNm, @RequestParam(value="age", required=false, defaultValue="0") int age){ logger.debug("userNm:" + userNm); logger.debug("age:" + age); return SUCCESS; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>ReqeustParam Test</title> </head> <body> <a href="requestParamTest/testRequestParam?userNm=RexFang&age=18">ReqeustParam Test</a> </body> </html>