Spring3 MVC Restful 多請求類型(json,xml,k-v),多視圖配置(J...

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>
    <!-- 配置多請求數據類型,如json xml-->html

    <bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
        <property name="messageConverters">
            <list>java

                <!-- 解析json請求數據,將json轉換爲java對象-->
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
                <!-- 解析xml請求數據,將xml轉換爲java對象-->web

                <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                    <constructor-arg>
                        <bean class="org.springframework.oxm.xstream.XStreamMarshaller">
                           <property name="streamDriver" >
                               <bean class="com.thoughtworks.xstream.io.xml.DomDriver"/>
                           </property>
                           
                           <property name="autodetectAnnotations" ><value>true</value></property>
                           <!--能夠與xml互換的對象,須要使用XStream的註解,註解的使用方法請參XStream官網-->ajax

                           <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"/>spring

                <!--能夠增長其餘數據類型,請參考spring的API-->
            </list>
        </property>
    </bean>    
  
    </bean>   
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">json

      <!-- .ftl文件存放的根目錄-->spring-mvc

      <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
    <property name="freemarkerVariables">
        <map>
          <entry key="xml_escape" value-ref="fmXmlEscape"/>
        </map>
    </property>mvc

      <!--能夠增長其餘freemarker的配置,詳情請參閱FreeMarkerConfigurerAPI-->
    </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>app

<!-- 可加入其餘視圖技術如 volacity jstl等-->                       
            </list>
        </property>
        <property name="defaultViews">
            <list>jsp

                <!-- 輸出爲JSON數據-->
                <bean
                    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                </bean>

                <!-- 輸出爲xml數據-->
                <bean id="marshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <property name="marshaller">
                        <bean id="xStreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller" >                         
                            <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,//contentType爲xml時,些值爲false                 dataType: "html",//json--返回json數據類型;xml--返回xml                 data: {},                 success: function(data) {                                   },                             error:function(e){                                 } });

相關文章
相關標籤/搜索