让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元注解

你可能感兴趣的:(spring,maven,mvc,json,Security)