SpringMVC and springmvc

Source: Internet
Author: User
Tags i18n

SpringMVC and springmvc

Preface:

This document is an extension of SpringMVC-1. It mainly introduces the practical application of SpringMVC, including SpringMVC data formatting, using JSR 303 verification standards for data validation in SpringMVC, SpringMVC and Ajax return JSON in the request processor, SpringMVC International and International error message display, use springMVC uploads and downloads files, customizes interceptor in SpringMVC, exception handling in SpringMVC, and integrates Spring MVC.

  A large number of code examples are provided in the notes. It should be noted that most of the code examples are displayed in the form of images, and all the images are from the code I typed, please correct me ~

If you have any questions or need to share the data tools in this series, please contact qingqing_crawl@163.com

12. data formatting

1. format the string of the time Date ---> Date type

1) Configure in the SpringMVC configuration file:<Mvc: annotation-driven/>

2) Add a Date attribute for JavaBean.@ DateTimeFormat (pattern = "yyyy-MM-dd ")Annotation

2. Formatting numeric values

1) Configure in the SpringMVC configuration file:<Mvc: annotation-driven>

2) Add a value type attribute for JavaBean.@ NumberFormat (pattern = "#,###,###.#")# Representing numbers

3. Get data formatting error information: UseBindingResult

XIII. Use the JSR 303 verification standard to verify data in SpringMVC

1. Add the jar package of Hibernate Validate: Under the path of hibernate-validator-5.0.0.CR2 \ dist

Add the third jar package of el under the hibernate-validator-5.0.0.CR2 \ dist \ lib \ required directory

2. Configure in spring MVC configuration file<Mvc: annotation-driven/>

3. Add the required annotation to the JavaBean attribute.

4. Add JavaBean in the method corresponding to the request Controller@ Valid annotation

5. Page whereabouts after an error occurs

6. Display Error information on the jsp page

7. Custom and international error messages

1) Create an international resource fileI18n. properties

 

2) use the SpringMVC configuration file with the international resource file

14. SpringMVC's request processor returns JSON

1. Import Three jar packages: \ jackson \ SpringMVC_JSON directory (this directory is the LZ directory to facilitate customization ~~, You only need to pay attention to the jar package)

 

2. Create a test connection on the jsp page and send Ajax

3. Request the target method of the processor class to directly return the required array or set, and add the target method@ ResponseBodyNote:

 

Supplement:HttpMessageConverterUsage:

1. @ RequestBody modifier input parameter@ ResponseBodyModifierBoth do not need to be used at the same time

1) simulate file upload:

① Form preparation:

② Target method

2.HttpEntity <T>Modify input parametersAndResponseEntity <T> modifier returned value

1) UseResponseEntity <T>SimulationFile DownloadOperation

15. SpringMVC Internationalization

Configuration is required before internationalization.3 International resource files

 

Then in the SpringMVC configuration fileConfigure international resource files

 

1. The text (not content), time, and numeric values can be localized on the page based on the browser language settings:Use the fmt tag of JSTL

1) i18n. jsp

2. HowObtain the message corresponding to Locale in the target method of the request processor class.Solution:Inject the ResourceBundleMessageSource instance into the request processor class and use the corresponding getMessage method.

3. PassLink Switching LocaleInstead of relying on the browser's language settings

1) jsp page configuration hyperlink

 

2)Configure SessionLocaleResolver and LocaleChangeInterceptor interceptor in the SpringMVC configuration file.

 

16. upload and download SpringMVC files

1. File Upload in SpringMVCDependent on apache-commons-fileupload ComponentsSo import the first jar package under the jar package apache-commons \ commons-fileupload-1.2.1 \ lib

2.Apache-commons-fileuploadDependent on commons-ioSo import the io package under apache-commons \ commons-io-2.0

3.SpringMVCConfigure MultipartResolver in the configuration file

4. Prepare the File Upload form

 

5. Compile the target method in the request processor

 

Supplement: For file downloads, see section 2nd in section 15.

17. Custom interceptor

1. How to define it?

1) Create an interceptor classImplement the HandlerInterceptor InterfaceImplementation Method

2) Configure in the SpringMVC configuration file

2. Three Methods in the Custom interceptor

3. Further configurations of the interceptor

18. Exception Handling in SpringMVC

1. Configure in spring MVC configuration file<Mvc: annotation-driven/>

2. Simulated exceptions

3.@ ExceptionHandlerAnnotation Method for Exception Handling

4.@ ControllerAdviceAnnotation used to mark the exception handling class

5.@ ResponseStatusIt can be used to label custom exception classes or to label methods.

1) custom exception class

3)@ ResponseStatusModifier

6. Use spring MVCSimpleMappingExceptionResolveR ing exception

1) An error occurred while simulating array subscript out of bounds.

2) In SpringMVCAn error occurred while performing ing in the configuration file.

19. Spring integrated Spring MVC

1. Is integration required?

1) required: Usually, similar to data sources, transactions, and integration of other frameworks are stored in the Spring configuration file (rather than in the SpringMVC configuration file ). in fact, there are services and Dao in the IOC container corresponding to the Spring configuration file.

2) No: they are all stored in the SpringMVC configuration file. You can also split Multiple Spring configuration files and use the import node to import other configuration files.

2. Common Problems and Solutions for Integration

Problem: If Spring's IOC container and SpringMVC's IOC container scan overlap, some beans will be created twice.

Solution:

1) make the Spring IOC container scan package and SpringMVC IOC container scan package do not overlap. (inconvenient to implement)

2.) YesUse exclude-filter and include-filter subnodes to specify annotations that can only be scanned

3. Relationship between SpringIOC container and SpringMVC IOC container

SpringMVCThe bean in the IOC container of Spring IOC can reference the bean in the Spring IOC container. Otherwise, the bean in the Spring IOC container cannot reference the bean in the Spring mvc ioc container!

 

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.