Spring3 MVC Restful 多請求類型(json,xml,k-v),多視圖配置(Json,Xml,JSP,Freemarker,Volacity等)

來源:互聯網
上載者: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> 
    <!-- 配置多請求資料類型,如json xml-->

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

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

                <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官網-->

                           <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的API-->
            </list>
        </property>
    </bean>     
   
    </bean>    
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

      <!-- .ftl檔案存放的根目錄-->

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

      <!--可以增加其他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>

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

                <!-- 輸出為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){
                
                }
});

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.