标签:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.ll.controller" />
<!-- bean视图解析器 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
p:order="10" />
<!-- XMl及JSON视图解析器配置 -->
<!-- <bean id="userListJson"
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"
p:renderedAttributes="userList" /> -->
<bean id="userListJson"
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="renderedAttributes">
<set>
<value>userList</value>
<value>School</value>
<value>Work</value>
</set>
</property>
</bean>
</beans>
package com.ll.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ll.model.Person;
import com.ll.model.Shop;
@Controller
@RequestMapping("/kfc/brands")
public class JSONController {
@RequestMapping(value="/getShopInJson", method = RequestMethod.GET)
public @ResponseBody Shop getShopInJSON() {
System.out.println("-----请求json数据--------");
Shop shop = new Shop();
shop.setName("zhangsan");
shop.setStaffName(new String[]{"mkyong1", "mkyong2"});
return shop;
}
@RequestMapping(value = "/showUserListByJson")
public String showUserListInJson(ModelMap mm) {
List<Person> userList = new ArrayList<Person>();
Person user1 = new Person();
user1.setUsername("tom");
user1.setPasswd("汤姆");
Person user2 = new Person();
user2.setUsername("john");
user2.setPasswd("约翰");
userList.add(user1);
userList.add(user2);
mm.addAttribute("userList", userList);
mm.addAttribute("School","SuZhou");
mm.addAttribute("Work","YanFa");
return "userListJson";
}
}
【Spring学习笔记-MVC-4】返回Json数据-方式2
标签:
原文地址:http://www.cnblogs.com/ssslinppp/p/4530002.html