- 浏览: 55257 次
文章分类
最新评论
Map List 拼json串
为了得到类似的字符串
serviceContext.xml
{"total":4,"rows":[{"fileId":"2","id":"2","name":"2","companyId":"2","createDate":"2014-07-04 11:07:21","createUserId":"2"},{"fileId":"3","id":"3","name":"3","companyId":"3","createDate":"2014-07-04 11:07:31","createUserId":"3"},{"fileId":"4","id":"4","name":"4","companyId":"4","createDate":"2014-07-04 11:07:41","createUserId":"4"}]}
拼了一下午的Map List……
package com.binhaifast.report.web.controller; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; 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.binhaifast.report.entity.Report; import com.binhaifast.report.service.ReportService; /** * * ClassName:ReportControl * * @author 刘各欢 * @version * @since Ver 1.1 * @Date 2014 2014年7月4日 上午11:33:53 * * @see */ @Controller public class ReportControl { @Autowired private ReportService reportService; @RequestMapping(value="/report/export.do",method=RequestMethod.GET) public void exportGet(){ } @ResponseBody @RequestMapping(value="/report/export.do",method=RequestMethod.POST) public Map<String,Object> exportPost(HttpServletRequest request){ String page = request.getParameter("page"); String rows = request.getParameter("rows"); System.out.println(page); System.out.println(rows); List<Report> reports = new ArrayList<Report>(); reports= reportService.findAllReportsWithPage(1, 10); return reportsToMap(reports); } /** * * convertToMap:将单个Report内的字段转换为单个Map * * @author 刘各欢 * @param report * @return * @since fhd Ver 1.1 */ public Map<String,Object> convertToMap(Report report){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss"); String createDateString = sdf.format(report.getCreateDate()); String id = report.getId(); String name = report.getName(); String createDate = createDateString; String fileId = report.getFileId(); String createUserId = report.getCreateUserId(); String companyId = report.getCompanyId(); Map<String,Object> map = new HashMap<String,Object>(); map.put("id", id); map.put("name", name); map.put("createDate", createDate); map.put("fileId", fileId); map.put("createUserId", createUserId); map.put("companyId", companyId); return map; } /** * * mapToList:将获得的多个Report转换为一个List,List内有多个Map,一个Map为一个Report * * @author 刘各欢 * @param reports * @return * @since fhd Ver 1.1 */ public List<Map<String,Object>> mapToList(List<Report> reports){ List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); for (Report report : reports) { list.add(convertToMap(report)); } return list; } public Map<String,Object> reportsToMap(List<Report> reports){ List<Map<String,Object>> listNew = mapToList(reports); Map<String,Object> map = new HashMap<String,Object>(); map.put("total", reportService.countResult("Report")); map.put("rows", listNew); return map; } }
mvcContext.xml
<?xml version="1.0" encoding="UTF-8"?> <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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:redirectHttp10Compatible="false" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"> <property name="interceptors" > <list> <bean id="contentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor" p:cacheSeconds="0" /> <bean id="localChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="com.common.webbind.WebBinding" /> </property> <property name="messageConverters"> <list> <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> </list> </property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="1125899906842624" /> <property name="defaultEncoding" value="UTF-8" /> </bean> <mvc:default-servlet-handler/> <mvc:resources location="/css/" mapping="/css/**" cache-period="31536000"/> <mvc:resources location="/images/" mapping="/images/**" cache-period="31536000"/> <mvc:resources location="/scripts/" mapping="/scripts/**" cache-period="31536000"/> <context:component-scan base-package="com.binhaifast.sys.web.controller" /> <context:component-scan base-package="com.demo.web.controller" /> <context:component-scan base-package="com.binhaifast.report.web.controller" /> </beans>
serviceContext.xml
<?xml version="1.0" encoding="UTF-8"?> <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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.binhaifast.sys.entity</value> <value>com.binhaifast.report.entity</value> </list> </property> <!-- 加载xml配置 --> <!-- <property name="mappingLocations"> <list> <value>classpath*:*.xml</value> </list> </property> --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.current_session_context_class">thread</prop> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">none</prop> </props> </property> </bean> <aop:aspectj-autoproxy /> <!-- 定义事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 申明annotation 加载事务驱动 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="merge*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="put*" propagation="REQUIRED" /> <tx:method name="use*" propagation="REQUIRED"/> <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到--> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="txPointcut" expression="execution(* com..service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> <!-- 自动扫描包 --> <context:annotation-config /> <context:component-scan base-package="com.binhaifast.sys.dao" annotation-config="true"/> <context:component-scan base-package="com.binhaifast.sys.service" annotation-config="true"/> <context:component-scan base-package="com.binhaifast.report.dao" annotation-config="true"/> <context:component-scan base-package="com.binhaifast.report.service" annotation-config="true"/> </beans>
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关推荐
java,android map形式和数组list转json字符串 可直接调用
NULL 博文链接:https://wuniu2010.iteye.com/blog/1771934
将java对象list或者map转json数据不需要第三方包,直接使用就可以了,如果你传入的数据不确定是map类型还是list类型,那么自己处理一下就可以了(判断下类型,传入参数改为object)。 如果是json数据转成java对象list...
Gson解析(List和Map)格式json数据 - CSDN博客原创2013年11月23日 21:54:46Gson解析(List和Map)格式json数据天
JackSonUtil.obj2json(方法)的使用,等,不了解的可以留言
java转换xml、list、map和json
java对象转为json格式,同javascript进行数据交换。
list map转换json格式数据以及相关的jar包 list转换后格式: [{"address":"广东省惠州","name":"tom","id":1,"age":23},{"address":"广东省深圳市","name":"jone","id":2,"age":84}] map转换后格式: {"address":...
一 传入Map转换为Json格式方法 二 传入Json格式字符串以Map存储并返回Map格式方法 三 传入Json格式字符串返回List<Map>格式方法等
转换json的时候需要的jar包 commons-beanutils.jar commons-collections-3.1.jar commons-lang-2.3.jar commons-logging-1.1.1.jar ezmorph-1.0.4.jar ezmorph-1.0.4.jar
List转换为List<Map>, List转换为List<Object> list转化为 JsonArray ,list转换JsonArray
jackson的jar包,用于转换map与list到json,直接导入即可
sturts框架下返回json数据,可以是 list对象 map对象 或者java类对象等,这是一个完整的实例,里面有jar包。
接受对象转换为JSON数据格式并且作为字符串输出,map转换json,list转换json
主要介绍了在Java 8中将List转换为Map对象方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
java转换xml、list、map和json.docxjava转换xml、list、map和json.docx
一个不用第三方jar的json与object(map、list)互换java类,定界符可以是'或"。提供测试实例。
json与bean,array,list,map,简单类型之间的封装、解析
json转换为String/List/Map/Bean
bean转json字符串,JSONArray对象转换成list集合,json字符串转换成list集合,JSONObject转换成map对象,json字符串转换成map对象等等各种方法