歡迎關注公衆号:
----------------------------------------------正文----------------------------------------------------
方式一:使用JSON工具将對象序列化成json,常用工具Jackson,fastjson,gson。
利用HttpServletResponse,然後擷取response.getOutputStream()或response.getWriter(),直接輸出。
如下:
這種方式最為直接,但是在既然已經用了SpingMVC架構的情況下,再用這種方式,有點不合時宜,out啦。
方式二:非注解形式,配置JsonView視圖
[html] view plain copy print?
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/context http://www.springframework.org/schema/context/spring-context.xsd">
- <description>SpringMVC公共配置</description>
- <!-- 視圖解析器 -->
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
- <property name="mediaTypes">
- <map>
- <entry key="html" value="text/html" />
- <entry key="json" value="application/json" />
- </map>
- </property>
- <property name="viewResolvers">
- <list>
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/views/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </list>
- </property>
- <property name="defaultViews">
- <list>
- <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" >
- <property name="extractValueFromSingleKeyModel" value="true" />
- </bean>
- </list>
- </property>
- </bean>
- </beans>
那麼我們的通路方式應該為:http://localhost:8080/SpringMVC/account/viewResolver.json
如果我們想以xml的形式傳回,當然還要配xml視圖,那相應的通路路勁為:http://localhost:8080/SpringMVC/account/viewResolver.xml
方式三:注解形式
[html] view plain copy print?
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd">
- <!-- 啟動注解驅動的Spring MVC功能,注冊請求url和注解POJO類方法的映射-->
- <mvc:annotation-driven />
- <!-- use-default-filters="false" 隻掃描指定的注解 -->
- <context:component-scan base-package="com.somnus.controller" use-default-filters="false">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
- <!-- 視圖解析器 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
- <property name="contentType" value="text/html"/>
- <property name="prefix" value="/WEB-INF/views/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- </beans>
由于配置了<mvc:annotation-driven />,SpringMVC會幫我們做很多事情,那也意味着需要我們自己來配置的越來越少,至于做了哪些事情可以看我的這篇博文《 SpringMVC 解讀——<mvc:annotation-driven/>》。
是不是使用方式越來越簡單了呢,程式員越來越傻,不知道是好事,還是壞事……