让Spring MVC支持JSON

首先需要导入两个包

com.springsource.org.codehaus.jackson-1.4.2.jar

com.springsource.org.codehaus.jackson.mapper-1.4.2.jar

如果不使用MAVEN可在官网上下载,包含了所有Spring需要的Jar包:

spring-framework-3.0.5.CI-834-dependencies.zip

在配置文件中加入如下配置:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven/>

</beans>

 Action

@ResponseBody
@RequestMapping("/tree/xmlTreeAction")
public List<Node> getTreeInfo(String type,HttpSession session) throws IOException{
	SecurityContext sc = (SecurityContext)session.getAttribute("SPRING_SECURITY_CONTEXT");
	Node es = null;
	Authentication auth = sc.getAuthentication();
	UserDetails user = (UserDetails)auth.getPrincipal();
	es = bizTree.loadAllnode(user.getUsername());
	List<Node> list = new ArrayList<Node>();
	list.add(es);
	return list;
}

 

注意必须添加ResponseBody元注解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值