为了得到类似的字符串
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>