Ajax与SpringMvc交互

前端之家收集整理的这篇文章主要介绍了Ajax与SpringMvc交互前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

ajax是一种比较常用的页面技术,以其良好的交互效果已经被广泛应用,为了方便的使用ajax,市面上也出现的不少的ajax框架如dwr。为此springmvc借鉴了dwr的思想,可以使用ajax和springmvc交互,同时提供了java对象转换成json的机制。还是先一睹为快吧

1 如果需要springmvc进行java对象和json的转换需要增加如下配置

  1. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  2. <property name="cacheSeconds" value="0" />
  3. <property name="messageConverters">
  4. <list>
  5. <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
  6. </list>
  7. </property>
  8. </bean>

如果json数据不需要springmvc进行转换messageConverters的配置可以忽略


2 在控制器中的方法加上注解@ResponseBody,此时该方法就可以通过ajax直接访问了

  1. package com.sxt.action;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12.  
  13. import com.sxt.po.User;
  14.  
  15. @Controller
  16. @RequestMapping("myajax.do")
  17. public class MyAjaxController {
  18. @RequestMapping(params="method=test1",method=RequestMethod.GET)
  19. public @ResponseBody List<User> test1(String uname) throws Exception{
  20. String uname2 = new String(uname.getBytes("iso8859-1"),"UTF-8");
  21. System.out.println(uname2);
  22. System.out.println("MyAjaxController.test1()");
  23. List<User> list = new ArrayList<User>();
  24. list.add(new User("潘玮柏","123"));
  25. list.add(new User("蔡依林","456"));
  26. return list;
  27. }
  28. }

3 使用ajax请求springmvc并取得返回值
  1. <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>My JSP 'index.jsp' starting page</title>
  12. <Meta http-equiv="pragma" content="no-cache">
  13. <Meta http-equiv="cache-control" content="no-cache">
  14. <Meta http-equiv="expires" content="0">
  15. <Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <Meta http-equiv="description" content="This is my page">
  17. <script>
  18. function createAjaxObj(){
  19. var req;
  20. if(window.XMLHttpRequest){
  21. req = new XMLHttpRequest();
  22. }else{
  23. req = new ActiveXObject("Msxml2.XMLHTTP"); //ie
  24. }
  25. return req;
  26. }
  27. function sendAjaxReq(){
  28. var req = createAjaxObj();
  29. req.open("get","myajax.do?method=test1&a="+Math.random());
  30. req.setRequestHeader("accept","application/json");
  31. req.onreadystatechange = function(){
  32. alert(req.responseText);
  33. //eval("var result="+req.responseText);
  34. //document.getElementById("div1").innerHTML=result[0].uname;
  35. }
  36. req.send(null);
  37. }
  38. </script>
  39. </head>
  40. <body>
  41. <a href="javascript:void(0);" onclick="sendAjaxReq();">测试</a>
  42. <div id="div1"></div>
  43. </body>
  44. </html>
  45.  

最后来看看运行效果

是不是也特别简单呢,其中最要注意的是使用这种方式需要jackson的支持,因此下面的这两个jar包不能少

这次先到这里,下次分享的是springmvc的拦截器。

猜你在找的SpringMVC相关文章