spring mvc json ajax jquery

来源:互联网 发布:js when done 编辑:程序博客网 时间:2024/06/02 18:57

1.前台页面

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>论坛版块</title><style type="text/css">   h1{    color:red;    text-align: center;   }</style><script type="text/javascript" src="../resources/js/jquery.js"></script></head><% cn.springmvc.model.User user = (cn.springmvc.model.User)session.getAttribute("user");<strong>String path = this.getServletContext().getContextPath();</strong>%><script type="text/javascript">$(document).ready(function(){  $("a[name='showTopic']").click(function(){var boardId = this.id;boardId = boardId.substring(10,boardId.length);<strong>$.ajax({</strong>type:'GET',<strong>contentType:'application/json',</strong>url:'../topics/mainTopic.html',<strong>dataType:'json',</strong>data:{ id: boardId },success:function(data){<strong>if (data&&data.success=="true"){</strong>            $('#topic_ls_'+boardId).html("共"+data.total+"条数据。<br/>");           <strong> $.each(data.data,function(i, item){</strong>              $('#topic_ls_'+boardId).append(                "<a href='../posts/mainPost.html?id="+boardId+"'>名称:" + item.topicTitle+"</a> | 创建者:"+item.userId+" | 创建时间:"+item.createTime+"<br/>");            });          }        },error:function(){alert("error");}})  })});</script><body><h1>版块列表</h1><div id="showTopic12" ></div>  <c:forEach items="${boards }"  var="board">     ${board.boardId }--<a  id="showTopic_${board.boardId }"  name="showTopic"  href="javascript:void(0);">${board.boardName }</a>--${board.boardDesc }--${board.topicNum }<br/>     <div id="topic_ls_${board.boardId }">${board.boardId }</div>  </c:forEach></body></html>

2.java文件

@Controller@RequestMapping("/posts")public class PostController {@Autowiredprivate PostService postService;@RequestMapping(value = "/mainPost", method = RequestMethod.GET)public String getPostList(String id, Model model) {List<Post> mainPosts = postService.getMainPostListById(id);model.addAttribute("mainPosts", mainPosts);return "post/post_list";}@RequestMapping(value = "/Posts", method = RequestMethod.GET)<strong>@ResponseBody</strong>public <strong>Map<String, Object></strong> getPostList(String id) {List<Post> posts = postService.getPostListById(id);Map<String, Object> modelMap = new HashMap<String, Object>();<strong>modelMap.put("total", posts.size());modelMap.put("data", posts);modelMap.put("success", "true");</strong>return modelMap;}}


3.jar文件

    jackson-core-asl-1.9.12.jar

   jackson-mapper-asl-1.9.12.jar


4.配置文件

<mvc:annotation-driven />  <!-- 支持spring3.0新的mvc注解 -->
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

0 0