Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ RequestParam annotations

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the @ RequestParam annotation. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

@ RequestParam annotations use 1, function

@ RequestParam: bind the request parameter to the method parameter of your controller (is the note to receive the normal parameter in springmvc)

2. Grammar

Syntax: @ RequestParam (value= "parameter name", required= "true/false", defaultValue= "")

Value: parameter name

Required: whether to include this parameter. The default is true, which means that this parameter must be included in the request path. If not, an error is reported.

DefaultValue: default parameter value. If this value is set, required=true will be invalid and automatically false. If this parameter is not passed, the default value will be used.

3. Test environment

Environment: jdk1.8 Tomcat8.5 idea2018 manven parent project child module

Steps:

1. Create a web project and introduce dependencies

2. Configure the SpringMvc entry file-DispatcherServlet-- is the master schedule and configured in web.xml

3. Create a Springmvc.xml file-- understood as: adapter (you don't need to specify your own adaptation here, but springmvc will specify it automatically)-- View parser

4. Create a business processor Controller class

5. Test

4. Engineering structure

Steps 1, 2, 3, reference: getting started with SpringMvc

5. Business processor HelloController.javapackage com.day01springmvc.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import org.springframework.web.servlet.ModelAndView; / * * @ Author: ShaoWei Sun. * @ Date: Created in 20:58 2018-11-16 * / @ Controller@RequestMapping ("hello") public class HelloController2 {/ * receive normal request parameter * http://localhost:8080/hello/show16?name=linuxsir * url parameter name must be consistent with @ RequestParam ("name") * @ return * / @ RequestMapping ("show16") public ModelAndView test16 (@ RequestParam () "name") String name) {ModelAndView mv = new ModelAndView () Mv.setViewName ("hello2"); mv.addObject ("msg", "receive normal request parameters:" + name); return mv } / * receive normal request parameter * http://localhost:8080/hello/show17 * url without name parameter will not report an error. If there is, it will be displayed * @ return * / @ RequestMapping ("show17") public ModelAndView test17 (@ RequestParam (value= "name", required=false) String name) {ModelAndView mv = new ModelAndView (); mv.setViewName ("hello2") Mv.addObject ("msg", "receive normal request parameters:" + name); return mv } / * receive normal request parameters * http://localhost:8080/hello/show18?name=998 display 998 * http://localhost:8080/hello/show18?name display as hello * @ return * / @ RequestMapping ("show18") public ModelAndView test18 (@ RequestParam (value= "name", required=true,defaultValue= "hello") String name) {ModelAndView mv = new ModelAndView (); mv.setViewName ("hello2") Mv.addObject ("msg", "receive common request parameters:" + name); return mv;}} 6, test

The difference between @ RequestParam and @ Param

@ RequestParam is used in the controller layer and is an annotation for Spring

Solves the problem that the foreground parameter name is inconsistent with the backend parameter variable name, which is equivalent to request.getParam

Value: parameter name, that is, the request parameter name of the input parameter. For example, username indicates that the value of the parameter whose name is username in the parameter area of the request will be passed.

Required: whether it is required. The default is true, which means there must be a corresponding parameter in the request, otherwise an error code of 404 will be reported.

DefaultValue: default value, which means that if there is no parameter with the same name in the request, the default value can be a SpEL expression, such as "# {systemProperties ['java.vm.version']}".

ResponseBody @ RequestMapping ("login") public String login (@ RequestParam (value = "username") final String username, @ RequestParam (value = "password", required = false) final String password, @ RequestParam (value = "valcode", required = false) final String valcode) {}

* * @ Param** is used in the dao layer and is an annotation in mybatis

Make the parameters in mapper.xml correspond to the parameters in the background, and enhance the readability.

If the two parameter names are the same, spring will automatically encapsulate them, and if they are inconsistent, you will need to manually match them.

That is, when using annotations to simplify xml configuration, the @ Param annotation is used to name the parameters. After naming the parameters, you can get the parameter values according to their names, and correctly pass the parameters into the sql statement.

Public interface Mapper {@ Select ("select s_id id,s_name name,class_id classid" + "from student where soundname = # {aaaa} and class_id = # {bbbb}") public Student select (@ Param ("aaaa") String name,@Param ("bbbb") int class_id); @ Delete. @ Insert. }

In the dao layer, it is used to name the parameter and add this comment to the mapper of Mybatis. The passed parameter is the same as the field name in Sql.

List getAllEmployeeByPage (@ Param ("page") Integer page, @ Param ("size") Integer size); that's all about how to use the @ RequestParam annotation. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report