Springmvc from introductory to mastery of the fourth chapter

Source: Internet
Author: User
Tags class definition

The first point of knowledge: @Controller annotations that identify the class as a back-end controller (like action in struts).
The main function is to accept the parameters of the page, forward the page.
The middle of the business logic is called the Business class processing this is the idea of MVC design pattern.
Let's look at the source code for this note:

 PackageOrg.springframework.stereotype;Importjava.lang.annotation.Annotation;Importjava.lang.annotation.Documented;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;//indicates that only the class above can be defined@Target ({Java.lang.annotation.ElementType.TYPE})//The retention policy is runtime, which loads annotations into the JVM memory when the JVM loads the class (it is the only policy that can read annotations with reflection)@Retention (retentionpolicy.runtime)//@Documented used to describe other types of annotation that should be used as public APIs for annotated program members, so they can be documented by tools such as Javadoc. Documented is a markup annotation with no members. @Documented//The Spring Framework provides the ability to use this annotation when a class is poorly categorized (service, DAO, Controller), so even if the @component annotation is used internally or in a good collation@Component Public@Interfacecontroller{ Public AbstractString value ();//The default value can be a component name, usually not written (see English explanation above)}

Second point of Knowledge: @RequestMapping annotations, like the above, the purpose of this annotation is not the same as @controller, this annotation can be defined on the class can also be defined above the method.

For example:

ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping, @Controller @requestmapping ("/springmvc") Public classtest01_requestmapping {/*** [email protected]: In addition to the modification method, you can also modify the class * 2. Class definition: Provides a preliminary request information mapping. Relative to the Web application's root directory (narrowing request) * 3. Method: Provides further subdivision mapping information. Relative to the URL at the class definition. * * If the class definition is labeled @requestmapping, the URL of the method is relative to the Web app's root directory *@return     */@RequestMapping ("/testrequestmapping")     PublicString testrequestmapping () {System.out.println ("Purpose of @RequestMapping annotations"); return"Success"; }}

Take a look at the source notes for the annotations:

 public @Interface  requestmapping {     default {};  Default {};       Default {};             ..... }

Example code:

/** @Title: Testrequestmappingattrmethodpost * @Description: TODO (Limit request can only be POST request) * @return*/@RequestMapping (Value= "/testrequestmappingattrmethodpost", method ={requestmethod.post}) PublicString Testrequestmappingattrmethodpost () {System.out.println ("Properties of the @RequestMaping annotations: Method Requestmethod[]{requestmethod.post}"); return"Success"; }    /*** @Title: Testrequestmappingattrmethodget * @Description: TODO (limit request only as GET request) *@return *     */@RequestMapping (Value= "/testrequestmappingattrmethodget", method ={requestmethod.get}) PublicString Testrequestmappingattrmethodget () {//Request URL//"${pagecontext.request.contextpath}/springmvc/testrequestmappingattrparams?username=hanson&age=11" SYSTEM.OUT.PRINTLN ("Attributes of @RequestMaping Annotations: Method Requestmethod[]{requestmethod.get}"); return"Success"; }

This annotation also supports Ant-style requests:

?  ***:**/user/*/createuser: Matching –/user/aaa/createuser,/user/bbb/createuser, etc. url/user/*/createUser: Matching –/user/aaa/createuser,/user/bbb//user/**/ CreateUser: Match –/user/createuser,/user/aaa/bbb/createuser and so on Url/user/createuser?? : Match URLs such as –/user/createuseraa,/USER/CREATEUSERBB, etc.

For example: The following request:
href= "${PAGECONTEXT.REQUEST.CONTEXTPATH}/SPRINGMVC/TESTANTSTYLEURL/CCCCC/ABC"
Example code:

@RequestMapping ("Testantstyleurl/*/abc")      public  String testantstyleurl () {          System.out.println ("ANTSTYLEURL:TESTANTSTYLEURL/*/ABC");           return "Success";     }
By the end of this chapter you should know the scope of the @controller annotation, and the class represented is a back-end controller.
@RequestMapping the scope of the annotation, the difference and function between the class and the target method, and the ability to limit the request mode, and so on.
Hansonq
Links: http://www.imooc.com/article/4299
Source: MU-Class Network

By the end of this chapter you should know the scope of the @controller annotation, and the class represented is a back-end controller.
@RequestMapping the scope of the annotation, the difference and function between the class and the target method, and the ability to limit the request mode, and so on.

Springmvc from Getting started to mastering chapter three


Hansonq
Links: http://www.imooc.com/article/4299
Source: MU-Class Network

Springmvc from introductory to mastery of the fourth chapter

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.