springmvc: Common Annotations

A, RequestParam comment

effect:

  The parameters specified in the request to the name of the controller parameter assignment.

Attributes:

   value: name of the parameter in the request.

  required: if the request parameter must provide this parameter. Default value: true. Representation must be provided if you do not provide an error.

jsp code: 

1 <-! RequestParams annotations using -> 
 2 <a href="springmvc/useRequestParam?name=test"> requestParam comment </a>
1     @RequestMapping("/testRequestParam")
2     public String testRequestParam(@RequestParam(name = "name") String username){
3         System.out.println("执行了。。。");
4         System.out.println(username);
5         return "success";
6     }

Two, RequestBody comment

effect:

  Request for acquiring the content thereof. Are obtained directly using the data key = value & key = value ... structure.

  get request method is not applicable.

Attributes:

  required: Must have a request body. The default value is: true. When the value is true, get request method error. If the value is false, get a request is null.

1  / * 
2       * acquiring content request body
 . 3       * @param body
 . 4       * @return 
. 5       * / 
. 6      @RequestMapping ( "/ testRequestBody" )
 . 7      public String testRequestBody (@RequestBody String body) {
 . 8          System.out.println ( "I performed ..." );
 . 9          System.out.println (body);
 10          return "Success" ;
 . 11      }

 

Three, PathVaribale comment

effect:

  For binding url placeholder. For example: in a request url / delete / {id}, the {id} is url placeholder.

  url support placeholder is added after spring3.0. Springmvc support is an important sign of rest style URL.

Attributes:

  value: specifies a placeholder name url.

  required: Must provide a placeholder.

 

Four, HiddentHttpMethodFilter filter

effect:  

  Because the browser form forms only support GET and POST requests, and DELETE, PUT, etc. method does not support, Spring3.0 add a filter, the browser can request changed the way specified in the request sent to our controller method that supports GET, POST, PUT and DELETE requests.

Instructions:

  The first step: the filter is disposed in web.xml.

  Step two: request method must be used post request.

  The third step: in accordance with the requirements provided _method request parameter value is the way we need to request the parameter.

 

Five, RequestHeader comment

effect:

  Request for acquiring the message header.

Attributes:

  value: providing a message header name

  required: Do you have to have this header

Note: generally do not use in the actual development.

 

Six, CookieValue comment

effect:

  For the value of the specified cookie name passed to the method parameter controller.

Attributes:

  value: Specifies the name of the cookie.

  required: Do I have to have this cookie.

 

七, ModelAttribute

effect:

  The annotation is SpringMVC4.3 new version later added. It can be used to modify the methods and parameters.

  In the method appears on representing the current method will be executed before the method of the controller, it performs. The method does not return values ​​which may be modified, specific methods return values ​​may also be modified.

  Appears on the parameters, gets the specified data to the parameter assignment.

Attributes:

  value: used to obtain key data.

  POJO can be a key attribute name can also be key map structure.

Scenario:

  When the form is submitted the data is not complete entity class data, to ensure that the field does not submit data using database objects original data.

  For example: When we edit a user, a user has created an information field, the field's value is allowed to be modified. In submitting the form data is certainly not the contents of this field, once the update content of the field will be set to null, then you can use this annotation to solve the problem.

 

八、SessionAttribute

effect:

  Sharing among multiple parameters for performing the method of the controller.

Attributes:

  value: for property specifies the name of deposit

  type: used to specify the type of data stored.

@SessionAttributes (value = { "MSG"})      // The msg = flypig stored in the session in domain
 1     /**
 2      * SessionAttributes注解
 3      * @return
 4      */
 5     @RequestMapping(value = "/testSessionAttributes")
 6     public String testSessionAttributes(Model model){
 7         System.out.println("testSessionAttributes。。。");
 8         //底层会存储到request域对象中
 9         model.addAttribute("msg","flypig");
10         return "success";
11 
12     }
13 
14     @RequestMapping(value = "/getSessionAttributes")
15     public String getSessionAttributes(ModelMap modelMap){
16         System.out.println("getSessionAttributes。。。");
17         //底层会存储到request域对象中
18         String msg = (String) modelMap.get("msg");
19         System.out.println(msg);
20         return "success";
21     }
22 
23     /**
24      * 清除
25      * @param status
26      * @return
27      */
28     @RequestMapping(value = "/delSessionAttributes")
29     public String delSessionAttributes(SessionStatus status){
30         System.out.println("delSessionAttributes。。。");
31         status.setComplete();
32         return "success";
33     }

 

Guess you like

Origin www.cnblogs.com/flypig666/p/11517497.html