In layman SpringMVC-1

Source: Internet
Author: User
Tags representational state transfer

Objective:

This note is the LZ in the previous study SPRINGMVC the framework of the record, divided into two share, this is the basis of this article, including SPRINGMVC environment built in Eclipse , , common annotations: @RequestMapping, @PathVariable, @ Requestparam, @SessionAttributes, @ModelAttribute, etc. , rest-style URL usage , , using native Servlet API , processing model data , @ModelAttribute source code Analysis , SPRINGMVC view parser and its flow analysis , and finally a small case.

The notes provide a lot of code examples, it should be noted that most of the code samples are shown in the form of pictures, all the pictures are from the code I hit, the shortcomings, please correct me ~

first, the HelloWorld of Springmvc

1. Join the spring base jar package under the JAR package Spring-framework-4.0.0.release\libs and the log package under apache-commons\commons-logging-1.1.1

2. Configure the Web. xml file Alt +/ Select dispatcherservlet

Note: You can actually configure the SPRINGMVC configuration file without contextconfiglocation, but instead use the default, default profile path and name:/web-inf/<servlet-name>- Servlet.xml The <servlet-name> property value of the SPRINGMVC that is configured in the Web. xml file <servlet-name>

3. New Spring configuration file under class path springmvc.xml Select beans context MVC constraint

4. Write the request processor Class , configure the automatically scanned package in Springmvc.xml, and add the processor flag annotation to the processor class

4. Write the view: index.jsp; Map the requested URL by @RequestMapping (XXX) in the request processor class; Configure the View resolver in the Springmvc.xml file

Second, @RequestMapping mapping request

[email protected] can both modify the class and modify the method

If @RequestMapping ("/springmvc") is used on the class, the request corresponding to the view layer is:

2. @RequestMapping the value and method properties

① value Property : Specifies the URL of the map

② Method Property : Specify how mappings are requested

3. @RequestMapping Params and Headers properties (understand, not used)

①params property: Specify request parameters for mappings

②headers Property: Specifies the mapped request header

4. @RequestMapping Ant-style path, that is, use wildcards (understand)

5. Supplement: @PathVariable (XXX) annotations can be used to map placeholders in the URL to the parameters of the target method

Third, REST

1. Introduction: Representational State Transfer. (Resource) Presentation Layer State transformation. is currently one of the most popular Internet software architectures. It is well-structured, standards-compliant, easy to understand, easy to expand, and is being used by more and more websites

In the 2.HTTP protocol, four verbs representing the mode of operation:GET, POST, PUT, DELETE. They correspond to four basic operations:get is used to get resources, post is used to create new resources, put is used to update resources, delete deletes resources

3. Take CRUD as an example to demonstrate the use of REST-style URLs

1) Configure the hiddenhttpmethodfilter filter in Web. XML to convert the POST request to a DELETE or PUT request

2) Simulate send GET POST DELETE PUT request in JSP page

3) Write the Get POST DELETE PUT request corresponding method in the request processor class, get the requested parameter by @PathVariable annotation, i.e. ID

iv. @RequestParam Annotations (important, common)

1.@RequestParam annotations You can get the requested parameters in the request method

2. Specific access:

V. @RequestHeader annotations (Learn)

1. Map (get) The request header information in the request method

2. Use

Vi. @CookieValue Annotations (Learn)

1. Obtain the value of a cookie based on the key of the cookie

1. Role

Vii. parameters for binding requests using the POJO object (POJO: Normal Java Class) (Common)

1. Build the user class and address class, where the user class contains the Address object

2. Build the form in the View layer (index.jsp)

3. Write the request method in the request controller class

Viii. using native Servlet APIs in the processor's method (common)

IX. Processing of model data

1. Modelandview as the return value of the processor method

2. Add a Map type parameter to the target method

3. @SessionAttributes Annotations (Common) Note: This annotation can only be placed on a class and cannot be placed on a method

1) Function: ① Specifies the attributes that need to be placed into the session by the property name (using the Value property of the @SessionAttributes annotation) ② specifies which model properties need to be placed into the session through the object type of the Model property (the types Of

2) Presentation;

4. @ModelAttribute Annotations ( similar to preparable in Struts2)

1) Application scenario: modify only a subset of an object's properties, and the properties that do not need to be modified must remain as-is and cannot be null

2) Specific use and precautions:

3) Source code Analysis using @ModelAttribute annotations in SPRINGMVC (learn)

4) conclusion, it is necessary to remember

5) Supplement

X. Analysis of the view parser and its flow (by default, the Internalresourceview view parser is used)

1. internalresourceviewresolver View parsing process

2., jstlview View parser ( If JSTL is used in the project, SPRINGMVC will automatically convert the view from Internalresourceview to Jstlview)

Use JSTL's fmt tags to simulate internationalization:

1) New three internationalized resource files

2) Introduction of JSTL in JSP pages, using FMT tags

3) Configure the Internationalized resource file in the SPRINGMVC configuration file

3,mvc:view-controller Tag: Configure the direct forwarding page, the configured path can directly ring the forwarded page, without having to go through the Handler target method

Test: Enter in URL: http://localhost:8888/springmvc-1/success (LZ modifies the port number of Tomcat in order to 8888)

Note: You must configure another annotation to use this annotation:mvc:annotation-driven

4. Custom View

1) Create the view class, implement the View interface , implement two methods

2) Configure beannameviewresolver view resolver in SPRINGMVC configuration file

3) Test:

5. Redirect to the target page

Xi. the CRUD case of SPRINGMVC

1. Follow the previous steps to build the SPRINGMVC environment

2. Note the point:

1) Use the Springmvc Form tab in the JSP page:

JSP page:

Correction: should be form:radiobuttons instead of form:checkboxes

In the Request processor class:

2) SPRINGMVC processing static resource files

Solve:

3) How to convert a GET request to a POST request and then to a DELETE request

GET Request:

GET---> POST:

4) About _method, convert the POST request to a PUT or DELETE request

In layman SpringMVC-1

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.