One, @RequestMapping request mapping
Requestmapping is an annotation that handles request address mappings and can be used on classes or methods. On a class, the method that represents all response requests in a class is the parent path of the address.
The requestmapping annotation has six properties, and we'll describe her in three categories below.
1, value, method;
Value: Specifies the actual address of the request, the specified address can be the URI Template mode (which will be explained later);
Method: Specifies the type of method requested, GET, POST, PUT, delete, and so on;
2, Consumes,produces;
Consumes: Specifies the type of submission to process the request (Content-type), such as Application/json, text/html;
Produces: Specifies the type of content returned, only if the specified type is included in the (Accept) type in the request header;
3, Params,headers;
Params: Specifies that some parameter values must be included in the request before the method is processed.
Headers: Specifies that certain header values must be included in the request in order for the method to process requests.
Second, @RequestParam request parameters
Iii. Modelandview return models and views
PackageWeb.controller;ImportJava.util.ArrayList;ImportJava.util.List;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.servlet.ModelAndView;ImportWeb.model.Student;@Controller@RequestMapping("/student")//class level, can not be required, if necessary, all of the following request paths need to join/student Public class studentcontroller { Private StaticList<student> sl=NewArraylist<student> ();Static{Sl.add (NewStudent (1,"Zhang Shan", One)); Sl.add (NewStudent (2,"Reese", A)); Sl.add (NewStudent (3,"Chen Wu", -)); }@RequestMapping("/list")//method level, must have, decide which request this method handles, if there is class level/student/list PublicModelandviewList() {Modelandview mav=NewModelandview (); Mav.addobject ("Studentlist", SL); Mav.setviewname ("Student/list");returnMav }@RequestMapping("/presave") PublicModelandviewPresave(@Requestparam(value="id", required=false) (String ID) {Modelandview mav=NewModelandview ();if(id!=NULL) {Mav.addobject ("Student", Sl.get (Integer.parseint (ID)-1)); Mav.setviewname ("Student/update"); }Else{Mav.setviewname ("Student/add"); }returnMav } }
package web.model;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublicclass Student { privateint id; private String name; privateint age;}
Introduction to Spring MVC (2)