Spring MVC extension and SSM integration

Source: Internet
Author: User

Processing of JSON objects

Brief introduction to the usage of @responsebody annotations

The function of the @responseBody annotation is to write to the body of the response object after the object returned by the controller is converted to the specified format by the appropriate converter, which is usually used to return JSON data or XML data. Instead of going to the processor after using this annotation, the data is written directly to the input stream, and his effect is equivalent to outputting the data in the specified format through the response object. The @ResponseBody is used when fetching data asynchronously, and the data returned by the processing method to which it is labeled is output to the corresponding stream, and the client obtains and displays the data.

Troubleshoot JSON data transfer in Chinese: the Message converter (stringhttpmessageconverter) fixed a conversion character encoded as "iso-8859-1" solution One: @RequestMapping (value= "/view ", Method=requestmethod.get, produces={" Application/json;charset=utf-8 "}) simple and practical, and flexible to handle

Solution two assemble the message converter stringhttpmessageconverter, set the character encoding to UTF-8 Supportedmediatypes property to specify the media type: Application/json character encoding: UTF-8 one configuration, Permanent fix

Resolving date formatting problems with JSON data delivery

Date format: Timestamp "YYYY-MM-DD" solution One: Annotation method: @JSONField (format= "YYYY-MM-DD")

Solution II

Configuring the Fastjson message converter-fastjsonhttpmessageconverter Setting features property: Specifying the output date Converter is Writedateusedateformat FASTJSON Specifies the default return date type DEFFAULT_DATE_FORMAT:YYYY-MM-DD HH:mm:ss for special type fields, you can use @jsonfield to control

For date format processing in JSON data (Fastjson), the default output timestamp conversion output YYYY-MM-DD HH:MM:SS format is configured in the message converter <value>writedateusedateformat</ value> annotation Precedence (YYYY-MM-DD) configures the <value>WriteDateUseDateFormat</value> add attribute annotation @jsonfield (format= ") in the message converter Yyyy-mm-dd ")

Configuring the Multi-View parser 2

<property name= "MediaTypes" >

<map> <entry key= "html" value= "Text/html;charset=utf-8"/>

<entry key= "JSON" value= "Application/json;charset=utf-8"/>

<entry key= "xml" value= "Application/xml;charset=utf-8"/>

</map>

</property>

Contentnegotiatingviewresolver favorparameter default is true, which means that parameter matching is supported, and the MIME type can be determined based on the value of the request parameter, the default request parameter is format, such as:/user/view?id =12&format=json MediaTypes based on the mapping list of request parameter values and MIME types viewresolvers Page view Parser

By Contentnegotiatingviewresolver, where the same resource is accessed according to the same URL address, and by setting the data format returned by the controller side of the MIME format, the return content of different forms is obtained. In fact, this restful programming style.

Data conversion and formatting

Time data cannot be implemented in spring MVC the binding solution must be manually configured to bind custom data types to enable data conversion and formatting of the function

Data binding Process

Core components of DataBinder data binding core dispatch Conversionservice the core interface of spring type conversion system to solve the conversion problem of the time string to the background date data type in the Foreground form form configuration <MVC: Annotation-driven/> tags, not configured with Conversionservice, can also resolve date conversion issues <mvc:annotation-driven/> labels by formatting annotations Defaultannotationhandlermapping Annotationmethodhandleradapter Formattingconversionservicefactorybean

Note: The method that labels the @initbinder annotation is called when the controller is initialized

SSM Framework Integration

New Web Project, importing related jar files

Configure Web. xml

Configuration file (/resources) database.properties

Log4j.properties

Mybatis-config.xml

Configure Typealiases

Set Global lazy Load

Applicationcontext-mybatis.xml

Springmvc-servlet.xml

Applicationcontext-mybatis.xml

Data source related configuration

Transaction management

Configure Sqlsessionfactorybean

Configure Mapperscannerconfigurer

Springmvc-servlet.xml

Configuration <mvc:annotation-driven/> tags (including message converter configuration)

Configure static file access via <mvc:resources/> tags

Configuring support file Uploads-multipartresolver

Configuring the Multi-View parser-contentnegotiatingviewresolver

Configuring Interceptors-interceptors

  

Intercept requests based on handlermapping, define multiple interceptors based on different handlermapping according to business needs

Handlerinterceptor Interface Prehandle () Posthandle () aftercompletion ()

Customizing the Interceptor Configuration-sysinterceptor

Spring MVC extension and SSM integration

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.