Spring MVC binding parameter According to default, whether it must be passed, (Requestparam (value= "id", defaultvalue= "1", required=true))

Source: Internet
Author: User

@RequestMapping (value = "/detail", method = Requestmethod.get)
Public String Newdetail (@RequestParam (value= "id", defaultvalue= "1", required=true) int id, @RequestParam (value= " TypeId ", defaultvalue=" 2 ", required=true) int typeId) {
Newsinfoentity newsinfoentity = Newsinfoservice.findbyid (ID);
Newstypeentity newstypeentity = Newstypeservice.findbyid (typeId);
This.request.setAttribute ("Newsinfoentity", newsinfoentity);
This.request.setAttribute ("Newstypeentity", newstypeentity);
return "News_detail";
}

First of all, SPRINGMVC. The default supported binding types are:

Httpservletreequest object: Parameter information can be obtained from the request object

HttpServletResponse object: The response information can be processed by the response object

HttpSession object: Gets the object stored in the session

Model/modelmap:model is an interface, MODELMAP is an implementation of an interface. The role is to populate the model data into the request domain.

Parameter bindings for simple types:

1. Define a variable directly in the controller, but there is a restriction that the parameter names and parameters in the request must be consistent, otherwise the data will not be received.

For example:
controller:public void Controllertest (Integer id) {}
request:http://localhost:8080/springmvctest/controllertest?id=2; It must be written as "id=2" and not as "id". This property name is immutable.

2, using @requestparam for parameter binding, when using this annotation to bind, the parameter name does not need to be consistent with the parameter name in the request.

For example:
controller:public void Controllertest (@RequestParam (value= "id") Integer goods_id) {}
The @RequestParam (value= "id") Integer goods_id indicates that the id attribute in the request is bound to the goods_id parameter
request:http://localhost:8080/springdemo/controllertest?id=2; The parameter passed in here is named ID

In @requestparam, there is a property called Required @RequestParam (Required=true) that indicates that the current parameter must be passed in
@RequestParam also has a property called DefaultValue to indicate the default value @RequestParam (defaultvalue= "AAA"), you know not to do more introduction

3, the Pojo binding

Parameters of the Pojo type can be directly defined in the controller to receive data from the request.

The condition of this usage is that the value of the name attribute of input in the page must correspond to the Pojo property one by one!

Spring MVC binding parameter According to default, whether it must be passed, (Requestparam (value= "id", defaultvalue= "1", required=true))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.