1、引入下面兩個jar包,我用的是1.8.10,好像1.4.2版本以上都可以,下載地址:http://wiki.fasterxml.com/JacksonDownload
jackson-core-asl-1.8.10.jar
jackson-mapper-asl-1.8.10.jar
同時引入Sping 本身的jar包:
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
2、Sping配置文件
web.xml
- <servlet>
- <servlet-name>dispatchServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/servlet-config.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatchServlet</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
servlet-config.xml:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!-- 自動掃描bean,把作了注解的類轉換為bean -->
- <context:component-scanbase-package="com.report.controller"/>
- <!-- 默認的注解映射的支持 -->
- <mvc:annotation-driven/>
- <!-- 支持JSON數據格式 -->
- <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
- <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <propertyname="messageConverters">
- <list>
- <beanclass="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
- <propertyname="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- </list>
- </property>
- </bean>
- <!-- 聲明viewResolver -->
- <beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <propertyname="prefix"value="/"/>
- <propertyname="suffix"value=".html"/>
- </bean>
- </beans>
接著,我們再定義一個java類:
- import java.util.HashMap;
- import java.util.Map;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- @Controller
- publicclass KeywordInfoController {
- @RequestMapping(value="getKeywordExportPVData.do", method=RequestMethod.POST)
- @ResponseBody
- public Object getKeywordExportPVInfo(){
- String s = "hellowold";
- return s;
- }
- }
然後我們在頁面調用url即可。