SpringMVC learning notes (3) and springmvc learning notes

Source: Internet
Author: User

SpringMVC learning notes (3) and springmvc learning notes

I. SpringMVC complete with annotations

1. First, import the jar package required by SpringMVC.

2. Add SpringMVC configuration in the Web. xml configuration file

<! -- Configure the setting of springmvcDispatcherServlet and configure the mapping --> <servlet-name> springmvc </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: springmvc-servlet.xml </param-value> </init-param> <! -- Indicates that the configuration file <load-on-startup> 1 </load-on-startup> --> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern> *. do </url-pattern> </servlet-mapping>

3. Add the springmvc-servlet.xml configuration file under src

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context h Ttp: // www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd "> <! -- Scan package and sub-package --> <context: component-scan base-package = "test. SpringMVC"/> <! -- Do not process static resources --> <mvc: default-servlet-handler/> <! -- If annotations are used, they must follow the following configuration --> <mvc: annotation-driven/> <! -- View parser --> <bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver" id = "internalResourceViewResolver"> <! -- Prefix --> <property name = "prefix" value = "/WEB-INF/jsp/"/> <! -- Suffix --> <property name = "suffix" value = ". jsp"/> </bean> </beans>

4. Create a folder named jsp under the WEB-INF folder to store the jsp view. Create a hello. jsp file and add "Hello World" to the body ".

5. Create a package and Controller, as shown below:

6. Write Controller code

@Controller@RequestMapping("/mvc")public class mvcController {     @RequestMapping("/hello")    public String hello(){                return "hello";    }}

7. Start the server and type http: // localhost: 8080/project name/mvc/hello

 

Ii. Configuration Parsing

1. Dispatcherservlet

DispatcherServlet is a front-end controller configured in the web. xml file. To intercept matching requests, the Servlet intercept matching rules must be defined by yourself. HandleMapping distributes the intercepted requests to the target Controller based on the corresponding rules, which is the first step to configure spring MVC.

2. InternalResourceViewResolver

View name parser

3. The preceding annotations

@ Controller registers a bean to the spring Context

@ RequestMapping annotation specifies which URL requests can be processed by the Controller

Iii. SpringMVC common annotations

@ Controller

Registers a bean to the spring context.

@ RequestMapping

The annotation specifies which URL requests can be processed by the Controller.

@ RequestBody

This annotation is used to read part of the body data of the Request. It is parsed using the HttpMessageConverter configured by default by the system, and then the corresponding data is bound to the object to be returned, then, bind the object data returned by HttpMessageConverter to the method parameters in the controller.

@ ResponseBody

This annotation is used to convert an object returned by the Controller method to a specified format through an appropriate HttpMessageConverter and write it to the body data area of the Response object.

@ ModelAttribute

Use @ ModelAttribute annotation in method definition: Spring MVC will call the @ ModelAttribute method at the method level one by one before calling the target processing method.

Use the @ ModelAttribute annotation before the input parameter of the method: You can obtain the object from the implicit model data from the implicit object, and then bind the request parameter to the object, then input the input parameter to add the method input parameter object to the model.

@ RequestParam

@ RequestParam can be used to pass the request parameter data transmission to the request method.

@ PathVariable

Bind a URL placeholder to an input parameter

@ ExceptionHandler

Annotation to the method. This method is executed when an exception occurs.

@ ControllerAdvice

Make a Contoller a global exception handling class, and use the @ ExceptionHandler method annotation in the class to handle all Controller exceptions.

 

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.