1、使用HttpServletResponse来处理---不需要配置解析器
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller()
public class AjaxController {
@RequestMapping("ajax")
public void ajax(String name, HttpServletResponse resp) throws IOException {
if ("sign".equals(name)) {
resp.getWriter().print("true");
} else
resp.getWriter().print("false");
}
}
导入js:
2、spring mvc 处理json数据:
2.1、导入jar包:
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
jackson-annotations-2.5.4.jar
jackson-core-2.5.4.jar
jackson-databind-2.5.4.jar
spring-aop-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
jackson-annotations-2.5.4.jar
jackson-core-2.5.4.jar
jackson-databind-2.5.4.jar
spring-aop-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
2.2、配置json转换器:
<?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:p="http://www.springframework.org/schema/p"
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">
<!-- json配置 -->
<!-- 用于将对象转换为 JSON -->
<bean id="stringConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<!-- spring容器扫描指定包下的所有类,如果类上有注解 那么根据注解产生相应bean对象已经映射信息 -->
<context:component-scan base-package="cn.sxt.controller"/>
</beans>
2.3、控制器:
package cn.sxt.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.sxt.vo.User;
@Controller
public class AjaxController {
@RequestMapping("/ajax.do")
public void ajax(HttpServletRequest req,HttpServletResponse resp) throws IOException{
resp.getWriter().print("ajax data");
}
@RequestMapping("/json.do")
@ResponseBody
//将会把返回值 转换为json对象
public List<User> json(){
List<User> list = new ArrayList<User>();
list.add(new User(1,"zhansan",22));
list.add(new User(2,"wangwu",21));
list.add(new User(3,"zhaosi",33));
list.add(new User(4,"wangdana",14));
return list;
}
}