Spring3 MVC Restful 多请求类型(json,xml,k-v),多视图配置(Json,Xml,JSP,Freemarker,Volacity等)
<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-/>
<context:annotation-config />
<import resource="classes/DataSource.xml"/>
<import resource="classes/applicationContext.xml"/>
<mvc:interceptors>
<bean />
</mvc:interceptors>
<bean id="defaultAnnotationHandlerMapping" ></bean>
<!-- 配置多请求数据类型,如json xml-->
<bean id="annotationMethodHandlerAdapter" >
<property name="messageConverters">
<list>
<!-- 解析json请求数据,将json转换为java对象-->
<bean />
<!-- 解析xml请求数据,将xml转换为java对象-->
<bean >
<bean ><value>true</value></property>
<!--可以与xml互换的对象,需要使用XStream的注解,注解的使用方法请参XStream官网-->
<property name="annotatedClasses">
<list>
<value>com.xxx.XxxxDTO</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
<bean value="/WEB-INF/ftl/"/>
<property name="freemarkerSettings">
<props>
<prop key="defaultEncoding">UTF-8</prop><!--解决汉字编码问题-->
</props>
</property>
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape"/>
</map>
</property>
<!--可以增加其他freemarker的配置,详情请参阅FreeMarkerConfigurerAPI-->
</bean>
<bean id="fmXmlEscape" 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 value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
<bean value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 可加入其他视图技术如 volacity jstl等-->
</list>
</property>
<property name="defaultViews">
<list>
<!-- 输出为JSON数据-->
<bean >
<property name="autodetectAnnotations" value="true"/>
</bean>
</property>
<property name="contentType" value="application/xml" />
</bean>
</list>
</property>
</bean>
<bean id="exceptionResolver" 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://localhost:8080/MyProject/register/person
前台代码:
ajax:
jQuery.ajax({
url: '/register/person',
contentType: "application/json",//application/xml
processData: true,//contentType为xml时,些值为false
dataType: "html",//json--返回json数据类型;xml--返回xml
data: {},
success: function(data) {
},
error:function(e){
}
});