Introduction to Spring MVC (2)

Source: Internet
Author: User

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)

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.