springmvc显示json数据 :<mvc:annotation-driven>

在springmvc.xml中配置,核心代码:

<mvc:default-servlet-handler/>

   <mvc:annotation-driven>

</mvc:annotation-driven>

 

  <mvc:annotation-driven>会自动注册RequestMappingHandlerMapping、RequestMappingHandlerAdapter 与ExceptionHandlerExceptionResolver 三个bean。

• 还将提供以下支持:

支持使用 ConversionService 实例对表单参数进行类型转换– 支持使用 @NumberFormat annotation、@DateTimeFormat注解完成数据类型的格式化– 支持使用 @Valid 注解对 JavaBean 实例进行 JSR 303 验证

 支持使用 @RequestBody 和 @ResponseBody 注解


同时,使用jQuery可以提取springmvc的数据。

例子如下:

package com.hh.springmvc.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hh.springmvc.entity.Department;
import com.hh.springmvc.entity.Employee;

@Repository
public class EmployeeDao {

	private static Map
   
   
    
     employees = null;
	
	@Autowired
	private DepartmentDao departmentDao;
	
	static{
		employees = new HashMap
    
    
     
     ();

		employees.put(1001, new Employee(1001, "E-AA", "aa@163.com",1,  new Department(101, "D-AA")));
		employees.put(1002, new Employee(1002, "E-BB", "bb@163.com",1, new Department(102, "D-BB")));
		employees.put(1003, new Employee(1003, "E-CC", "cc@163.com",0,new Department(103, "D-CC")));
		employees.put(1004, new Employee(1004, "E-DD", "dd@163.com",0,new Department(104, "D-DD")));
		employees.put(1005, new Employee(1005, "E-EE", "ee@163.com",1,new Department(105, "D-EE")));
	}
	
	private static Integer initId = 1006;
	
	public void save(Employee employee){
		if(employee.getId() == null){
			employee.setId(initId++);
		}
		
		employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
		employees.put(employee.getId(), employee);
	}
	
	public Collection
     
     
      
       getAll(){
		return employees.values();
	}
	
	public Employee get(Integer id){
		return employees.get(id);
	}
	
	public void delete(Integer id){
		employees.remove(id);
	}
}
package com.hh.springmvc.comtroller;

import java.util.Collection;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hh.springmvc.dao.DepartmentDao;
import com.hh.springmvc.dao.EmployeeDao;
/**
 * 
 * @author HH
 * @date 2017-6-27  
 * @version jdk 1.7.0 
 * @function   没完成 delete  put  get功能
 */
@Controller
@RequestMapping("/Handeler0627")
public class Handeler0627{
	@Autowired
	private DepartmentDao departmentDao;
	//1.查找员工,jsonxingshifanhui
	@ResponseBody
	@RequestMapping("/testJson")
    public Collection testJson(){
		System.out.println("Collection的testJson");
    	return employeeDao.getAll();
   }
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>




  
    
      
      
    
    
      
      My JSP 'index.jsp' starting page
	
      
      
	
      
      
	
      
          
	
      
      
	
      
      
	
      
      
	<script type="text/javascript" src="js/jquery-1.8.2.min.js">	</script>
  
  <script type="text/javascript">
	$(function(){
		$("#testJson").click(function(){
			var url = this.href;
			var args = {};
			$.post(url, args, function(data){
				for(var i = 0; i < data.length; i++){
					var id = data[i].id;
					var lastName = data[i].lastName;
					
					alert(id + ": " + lastName);
				}
			});
			return false;
		});
	})
</script>
  
  
  
 
  
  
      
      json测试
      
      


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值