1 使用HttpServletResponse来处理--不需要配置解析器
@Controller public class AjaxController { @RequestMapping("/ajax") public void ajax(String name,HttpServletResponse resp) throws IOException{ if ("sgcc".equals(name)) { resp.getWriter().print("true"); System.out.println("true"); }else { resp.getWriter().print("false"); } } }
前台
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript" src="jquery-1.8.3-min.js"></script> </head> <body> <input type="button" value="发送" id="btn"> 用户名:<input type="text" id="textName"/> <script type="text/javascript"> $(function(){ $('#btn').click(function(){ $.ajax({url:'ajax.do', type:'POST', data:{ name:$('#textName').val() }, dataType:'text', success:function(data,textStatus){ alert(data); console.log(textStatus) }, error:function(textStatus){ console.log(textStatus) } }) }) }) </script> </body> </html>
2 springmvc处理json数据
a 导入jar包
jackson-annotations-2.7.0.jar
jackson-core-2.7.7.jar
jackson-databind-2.7.8.jar
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.MappingJackson2HttpMessageConverter"> </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 { @RequestMapping("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(1,"jackson","男")); return list; }
前台代码(不对。。)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript" src="jquery-1.8.3-min.js"></script> </head> <body> <input type="button" value="获取数据" id="btn"> <table width="80%" align="center"> <tr> <td>编号</td> <td>姓名</td> <td>性别</td> </tr> <tbody id="content"> </tbody> </table> <script type="text/javascript"> $(function(){ $('#btn').click(function(){ $.post("json.do",function(data){ var html=""; for(var i=0;i<data.length;i++){ html+="<tr><td>"+data[i].id+"</td><td>"+data[i].name+"</td><td>"+data[i].sex+"</td></tr>"; } $("content").html(html); }); }); }); </script> </body> </html>