Spring3 MVC Restful multi-request type (json, xml, k-v), multi-view configuration (Json, Xml, JSP, Freemarker, Volacity, etc)

Source: Internet
Author: User

Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: p = "http://www.springframework.org/schema/p"
Xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: context = "http://www.springframework.org/schema/context"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.xsd>

<Context: component-scan base-package = "com. xxx."/>
<Aop: aspectj-autoproxy proxy-target-class = "true"/>
<Context: annotation-config/>
<Import resource = "classes/DataSource. xml"/>
<Import resource = "classes/applicationContext. xml"/>
<Mvc: interceptors>
<Bean class = "com. globeway. web. servlet. intercept. JsonGetInterceptor"/>
</MVC: interceptors>


<Bean id = "defaultannotationhandlermapping" class = "org. springframework. Web. servlet. MVC. annotation. defaultannotationhandlermapping"> </bean>
<! -- Configure multiple request data types, such as json xml -->

<Bean id = "annotationmethodhandleradapter" class = "org. springframework. Web. servlet. MVC. annotation. annotationmethodhandleradapter">
<Property name = "messageconverters">
<List>

<! -- Parse JSON request data and convert JSON to Java object -->
<Bean class = "org. springframework. http. converter. json. MappingJacksonHttpMessageConverter"/>
<! -- Parse xml request data and convert xml to java object -->

<Bean class = "org. springframework. http. converter. xml. alallinghttpmessageconverter">
<Constructor-arg>
<Bean class = "org. springframework. oxm. xstream. xstreampolicaller">
<Property name = "streamDriver">
<Bean class = "com. thoughtworks. xstream. io. xml. DomDriver"/>
</Property>

<Property name = "autodetectAnnotations"> <value> true </value> </property>
<! -- Objects that can be exchanged with xml must be annotated with XStream. For the usage of annotations, see the XStream official website. -->

<Property name = "annotatedClasses">
<List>
<Value> com. xxx. XxxxDTO </value>
</List>
</Property>

</Bean>
</Constructor-arg>
</Bean>
<Bean class = "org. springframework. http. converter. FormHttpMessageConverter"/>
<Bean class = "org. springframework. http. converter. BufferedImageHttpMessageConverter"/>
<Bean class = "org. springframework. http. converter. ByteArrayHttpMessageConverter"/>
<Bean class = "org. springframework. http. converter. StringHttpMessageConverter"/>
<Bean class = "org. springframework. http. converter. resourcehttpmessageconverter"/>

<! -- You can add other data types. For more information, see spring API -->
</List>
</Property>
</Bean>

</Bean>
<Bean id = "freemarkerconfig" class = "org. springframework. Web. servlet. View. freemarker. freemarkerconfigurer">

<! -- The root directory of the FTL file -->

<Property name = "templateloaderpath" value = "/WEB-INF/FTL/"/>
<Property name = "freemarkerVariables">
<Map>
<Entry key = "xml_escape" value-ref = "fmXmlEscape"/>
</Map>
</Property>

<! -- You can add other freemarker configurations. For more information, see freemarkerjavaserapi -->
</Bean>

<Bean id = "fmXmlEscape" class = "freemarker. template. utility. XmlEscape"/>

<Bean
Class = "org. springframework. web. servlet. view. ContentNegotiatingViewResolver">
<Property name = "defaultContentType" value = "application/json"/>
<Property name = "mediaTypes">
<Map>
<Entry key = "html" value = "text/html"/>
<Entry key = "json" value = "application/json"/>
<Entry key = "xml" value = "application/xml"/>
</Map>
</Property>
<Property name = "viewResolvers">
<List>
<Bean class = "org. springframework. web. servlet. view. freemarker. FreeMarkerViewResolver">
<Property name = "cache" value = "true"/>
<Property name = "prefix" value = ""/>
<Property name = "suffix" value = ". ftl"/>
<Property name = "exposeSpringMacroHelpers" value = "true"/>
</Bean>

<Bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "viewClass"
Value = "org. springframework. web. servlet. view. JstlView"/>
<Property name = "prefix" value = "/WEB-INF/jsp/"/>
<Property name = "suffix" value = ". jsp"/>
</Bean>

<! -- Other view technologies such as volacity jstl can be added -->
</List>
</Property>
<Property name = "defaultViews">
<List>

<! -- JSON data output -->
<Bean
Class = "org. springframework. Web. servlet. View. JSON. mappingjacksonjsonview">
</Bean>

<! -- Output as XML data -->
<Bean id = "marshallingview" class = "org. springframework. Web. servlet. View. xml. marshallingview">
<Property name = "marshaller">
<Bean id = "xstreampolicaller" class = "org. springframework. oxm. xstream. xstreampolicaller">
<Property name = "autodetectAnnotations" value = "true"/>
</Bean>
</Property>
<Property name = "contenttype" value = "application/XML"/>
</Bean>
</List>
</Property>
</Bean>

<Bean id = "exceptionresolver"
Class = "org. springframework. Web. servlet. handler. simplemappingexceptionresolver">

<Property name = "defaultErrorView" value = "/commons/error"/>
<Property name = "exceptionMappings">
<Props>
</Props>
</Property>
</Bean>
</Beans>

 

Controller:

@ Controller
@ RequestMapping ("/register/person ")
Public class PersonController {


@ RequestMapping (method = RequestMethod. GET)
Public ModelAndView list (){

HashMap <String, Object> model = new HashMap <String, Object> ();
Model. put ("pager", pager );
Return new ModelAndView ("/register/person/personList", model );
}
}

 

Http: // xxxx: pp/register/person

 

Ajax:
JQuery. ajax ({
Url: '/register/person ',
ContentType: "application/json", // application/xml
ProcessData: true, // when contentType is xml, some values are false
DataType: "html", // json -- Return json data type; xml -- Return xml
Data :{},
Success: function (data ){

},
Error: function (e ){

}
});

Related Article

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.