Introduction to Spring MVC annotation Development

Source: Internet
Author: User

Initial development of annotated type

Two commonly used annotations:

@Controller: Is the most commonly used annotation in SPRINGMVC, which can help define the current class as a spring-managed bean, specifying that the class is a controller that can be used to accept requests. Identifying the current class is a concrete implementation of the control layer

@requestMapping: Put on a method to specify the path to a method, when it is placed on the class equivalent to the namespace needs to requestmapping on the combination method to access

Expand:

Note Name

Role

@Controller

Annotations indicate that the class requires the spring container to be loaded automatically, and a class to be the Bean of the spring container.

@RequestMapping

It can be labeled at the class definition, associating a Controller with a specific request, or it can be labeled at the method signature. So the @RequestMapping labeled at the class declaration is equivalent to having POJO implement the Controller interface, and the @RequestMapping at the method definition is equivalent to having the POJO extend the Spring predefined controller (such as Simp Leformcontroller, etc.).

@Resource

The setter method parameter that is used to annotate the attribute is derived from the spring Bean

@ModelAttribute

① bind request parameter to command object: When placed on the parameter of the function processing method, it is used to bind multiple request parameters to a command object, thus simplifying the binding process and automatically exposing the model data for use in view page presentation;

② exposes a form reference object as model data: when placed on a processor's general method (non-functional processing method), it is the form reference object that prepares the form for presentation, such as the city that needs to be selected when registering, and before performing the function processing method (@RequestMapping annotation method), Automatically added to the model object for use when the view page is displayed;

③ Exposure @requestmapping method returns a value of model data: when placed on the return value of a function processing method, the return value of the exposed function processing method is the model data that is used when the view page is displayed.

@SessionAttributes

Represents the annotated object that is stored in the httpsession scope

@PathVariable

Used to map template variables in the request URL to parameters of a functional processing method

@requestParam

Used to map the request parameter area data to the parameters of a functional processing method

The premise is: Configure a package scanner

Case: Using @controller and @requestmapping () to implement the Welcome program

To configure the package scanner:

Firstcontroller:

 PackageCn.controller; import org.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.stereotype.Controller;/** *  * @authorJingpeppe **/@Controller Public classfirstcontroller{@RequestMapping ("/dofirst.do")          PublicString Dofirst () {return"/web-inf/index.jsp"; }                   }    

N processor methods defined in a processing class

 PackageCn.controller; import org.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.stereotype.Controller;/** *  * @authorJingpeppe **/@Controller Public classfirstcontroller{@RequestMapping ("/dofirst.do")          PublicString Dofirst () {return"/web-inf/index.jsp"; } @RequestMapping ("/dosecond.do")       PublicString Dosecond () {return"/web-inf/index.jsp"; }                   }

Namespaces:

Wildcard usage in a request

 PackageCn.controller;ImportJava.util.HashMap;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;/** *  * @authorJingpeppe **/@Controller//name Space@RequestMapping ("/hr")     Public classfirstcontroller{@RequestMapping ("/dofirst.do")          PublicString Dofirst () {return"/web-inf/index.jsp"; } @RequestMapping ("/dosecond.do")          PublicString Dosecond () {return"/web-inf/index.jsp"; } @RequestMapping ("/*third.do")//* represents 0 or n characters, matching all of the formats on the third end              PublicString Dothird () {return"/web-inf/index.jsp"; } @RequestMapping ("/fourth*.do")//* represents 0 or n characters, matching all address formats beginning with fourth              PublicString Dofour () {return"/web-inf/index.jsp"; } @RequestMapping ("/**/fiveth.do")//must end with Fiveth, preceded by N-level directories, or level 0              PublicString dofive () {return"/web-inf/index.jsp"; } @RequestMapping ("/*/sixth*.do")//there must be a first-level path before HR and sixth. And can only be one level              PublicString Dosix () {return"/web-inf/index.jsp"; }           }    

Definition of the mode in the request

For @requestmapping, there is an attribute method that restricts the way a request is submitted by the annotated method, and only the method specified by the methods property is satisfied to execute the annotated methods. The value of the method property is Requestmethod, which is an enumeration constant. Common values are requestmethod.get and requestmethod.post

 PackageCn.controller;ImportJava.util.HashMap;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;/** *  * @authorJingpeppe **/@Controller//name Space@RequestMapping ("/hr")     Public classfirstcontroller{@RequestMapping (value= "/dofirst.do", method=requestmethod.get) PublicString Dofirst () {return"/web-inf/index.jsp"; }                       }        

Default is Get,post words will error 405

Parameters of the Processor method

There are five types of parameters commonly used in processor methods, which are automatically assigned by the system when the system is called, that is, the programmer can use it directly within the method
1) httpservletrequest
2) HttpServletResponse
3) HttpSession
4) model for carrying data
5) Request parameters that are carried in the request

Let's start with the first three kinds.

 PackageCn.controller;ImportJava.util.HashMap;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;/** *  * @authorJingpeppe **/@Controller//name Space@RequestMapping ("/hr")     Public classfirstcontroller{@RequestMapping (value= "/dofirst.do", method=requestmethod.get) PublicString Dofirst (httpservletrequest request,httpservletresponse response,httpsession Session,model Model) {S Ystem.out.println ("Request\t" +request); System.out.println ("Response\t" +response); System.out.println ("Session\t" +session); System.out.println ("Model\t" +model); return"/web-inf/index.jsp"; }            }    

Request parameters that are carried in the request--scattered parameters

@RequestMapping (value= "/dofirst.do", method=requestmethod.get)   public String Dofirst (Model        Model) {Map<String,Object> datas=new hashmap<string, object>();        Datas.put ("uname", "Firefox");        Model.addallattributes (datas);                 SYSTEM.OUT.PRINTLN (model);     return ""/web-inf/index.jsp "; }

Object parameters: Assembling into entities

Custom entity classes: UserInfo

 Package cn.entity;  Public class UserInfo {    private  String uname;      Public String Getuname () {        return  uname;    }      Public void setuname (String uname) {        this. uname = uname;    }} 

Fi the Rstcontroller controller class method parameter is an entity class object:

 PackageCn.controller;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportCn.entity.UserInfo; @Controller//name Space@RequestMapping ("/hr")     Public classfirstcontroller{@RequestMapping (value= "/dofirst.do")          PublicString Dofirst (UserInfo info) {System.out.println (Info.getuname ()); return"/web-inf/index.jsp"; }                }    

Appliactiobcontext.xml

JSP page Form form submission:

Solve garbled problem: Web. XML CONFIG encoding filter---characterencodingfilter

Introduction to Spring MVC annotation Development

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.