• spring mvc:输出json,输出多个json


    spring mvc:输出xml/输出json

    用到的注解@ResponseBody

    @ResponseBody用来输出json/xml等格式数据(非html)

    controller输出用到的类

    org.springframework.web.bind.annotation.ResponseBody

    需要bean解析支持

    <!-- bean视图解析 -->			
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>	
    

      

    用到的json插件

    <!-- json插件 -->
    	<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    	<dependency>
    	    <groupId>com.fasterxml.jackson.core</groupId>
    	    <artifactId>jackson-annotations</artifactId>
    	    <version>2.7.4</version>
    	</dependency>
    		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    	<dependency>
    	    <groupId>com.fasterxml.jackson.core</groupId>
    	    <artifactId>jackson-core</artifactId>
    	    <version>2.7.4</version>
    	</dependency>
    		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    	<dependency>
    	    <groupId>com.fasterxml.jackson.core</groupId>
    	    <artifactId>jackson-databind</artifactId>
    	    <version>2.7.4</version>
    	</dependency>
    	
    

      

    项目访问地址:http://localhost:8080/gugua3/person/list

    包名:json

    配置文件:web.xml, applicationContext.xml, json-servlet.xml

    web.xml

    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      
    <!--配置文件路径-->
    <context-param>
     	<param-name>contextConfigLocation</param-name>
    	<param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    
    <!-- 字符过滤器 -->  
    <filter>
       <filter-name>encodingFilter</filter-name>
       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
       <init-param>
           <param-name>encoding</param-name>
           <param-value>UTF-8</param-value>
       </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping> 
    
    
    <!-- 监听转发 -->
    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>  
        <servlet-name>json</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>json</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>    
      
      
    </web-app>
    

      

    applicationContext.xml

    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    					http://www.springframework.org/schema/beans/spring-beans.xsd 
    					http://www.springframework.org/schema/mvc 
    					http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    					http://www.springframework.org/schema/context 
    					http://www.springframework.org/schema/context/spring-context.xsd">
    
    
    <!-- 默认:注解映射支持 -->		
    <mvc:annotation-driven/>
    <!-- 静态资源配置 -->
    <mvc:resources location="/pages/**" mapping="/pages/"/>
    
    <!-- 自动扫描包名,controller -->
    <context:component-scan base-package="json"/>		
    
    
    
    </beans>
    

      

    json-servlet.xml

    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    					http://www.springframework.org/schema/beans/spring-beans.xsd 
    					http://www.springframework.org/schema/mvc 
    					http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    					http://www.springframework.org/schema/context 
    					http://www.springframework.org/schema/context/spring-context.xsd">
    			
    <!-- bean视图解析 -->			
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>					
    					
    </beans>	
    

      

    Person.java

    package json;
    
    public class Person {
    
    	String name;
    	String passwd;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public String getPasswd() {
    		return passwd;
    	}
    	public void setPasswd(String passwd) {
    		this.passwd = passwd;
    	}
    	
    	
    }
    

      

    PersonController.java

    package json;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    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;
    
    @Controller
    @RequestMapping(value="/person")
    public class PersonController {
    
    	@RequestMapping(value="/list", method=RequestMethod.GET)
    	@ResponseBody
    	public Map<String, Object> getPerson()
    	{
    		List<Person> list = new ArrayList<Person>();
    		Person person1 = new Person();
    		person1.setName("张三");
    		person1.setPasswd("123456");
    		list.add(person1);
    		
    		Person person2 = new Person();
    		person2.setName("李四");
    		person2.setPasswd("456789");
    		list.add(person2);
    		
    		Map<String, Object> maps = new HashMap<String, Object>(3);
    		maps.put("work", "研发");
    		maps.put("school", "suzhou");
    		maps.put("list", list);
    		return maps;
    		
    	}
    	
    }
    

      

  • 相关阅读:
    【USACO1.6.3】Prime Palindromes【数论,数学】【模拟】
    【HDU6345】子串查询【前缀和】【线段树】
    【HDU6345】子串查询【前缀和】【线段树】
    【HDU6344】调查问卷【状压】【模拟】
    【HDU6344】调查问卷【状压】【模拟】
    获取一个处理程序函数到一个特定的弹出菜单
    一个剪贴板增强工具
    将checklistbox控件与DataView绑定
    探索者命令式的三分
    ColorListBox
  • 原文地址:https://www.cnblogs.com/achengmu/p/9046926.html
Copyright © 2020-2023  润新知