开发者学堂课程【SpringMVC框架入门:springmvc中 ajax.的处理】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/22
springmvc中ajax.的处理
目录
一.使用HttpservletResponse来处理―不需要配置解析器
二.springmvs处理json数据
1.使用HttpservletResponse来处理―不需要配置解析器
@RequestMapping("/ajax")
public void ajax(String name,HttpServletResponseresp) throws IOEexception{
if("siggy".equals(name)){
resp.getWriter(). print("true");
else{
resp.getWriter().print("false");}
}
2.springmvs处理json数据
A. 导入jar包
jackson-annotations-2.5.4.jar
jackson-core-2.5.4.jar
jackson-databind-2.5.4.jarb)
B. 配置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 MappingJackson2Ht tpMessageConverter"></bean<bean
class="org springframework.web.servlet.mvc.annotation AnnotationMethodHandlerAdapter ">
<property name= "messageConverters ">
<list>
<ref bean= "stringConverter" />
<ref bean= "jsonConverter" />
</list>
</property>
</bean>
C. Controller 代码
@Controller
public class JsonController {
@ReguestMapping("/json")
@ResponseBody
public List<User> json(){
List<User> list= new ArrayList<User>();
list,add(new User(1,"zhangsan","男"));
list.add(new User(2,"nico","female"));
list.add(new User(3,"jackson","男"));
return list;
}