<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><!-- 导入资源文件 --><context:property-placeholder location="classpath:db.properties" /><!-- 自动扫描 --><beanclass="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /><!-- C3P0 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="initialPoolSize" value="${jdbc.initPoolSize}"></property><property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property></bean><!-- 事务控制 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"abstract="false" lazy-init="false" autowire="default"><property name="dataSource"><ref bean="dataSource" /></property></bean><bean id="zbDAO" class="com.heu.dao.ZBDAO"></bean><bean id="zbService" class="com.heu.service.ZBService"><property name="zbDAO" ref="zbDAO"></property></bean><bean id="zbAction" class="com.heu.action.ZBAction" scope="prototype"><property name="zbService" ref="zbService"></property></bean></beans>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.i18n.encoding" value="GBK" /><package name="json" extends="json-default" namespace="/"><action name="zb-*" class="zbAction" method="{1}"><result name="getAllZBs" type="json"><param name="root">dataMap</param><param name="callbackParameter">callback</param></result></action></package></struts>
jdbc.user=rootjdbc.password=rootjdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc:mysql:///ci?useUnicode=true&characterEncoding=UTF-8jdbc.initPoolSize=5jdbc.maxPoolSize=10
public class ZBService {private ZBDAO zbDAO;public ZBDAO getZbDAO() {return zbDAO;}public void setZbDAO(ZBDAO zbDAO) {this.zbDAO = zbDAO;}public List<ZB> getAllZBs() throws ClassNotFoundException, SQLException{List<ZB> zbs = ZBDAO.getZBs();return zbs;}}
public class ZBAction extends ActionSupport{private static final long serialVersionUID = 1L;private Map<Integer, Object> dataMap; //struct把这个map,转成jsonprivate ZBService zbService;private List<ZB> zbList;public ZBAction(){dataMap = new HashMap<Integer, Object>();//一定要在构造函数进行map的初始化,否则会报空指针}public ZBService getZbService() {return zbService;}public void setZbService(ZBService zbService) {this.zbService = zbService;}public String getAllZBs() throws ClassNotFoundException, SQLException{dataMap.clear();zbList = zbService.getAllZBs();for(Iterator<ZB> it = zbList.iterator(); it.hasNext();){ZB zb = it.next();int id = zb.getFieldId();String name = zb.getName();int type = zb.getType();String fieldName = zb.getFieldName();Map<String,Object> map = new HashMap<String,Object>();map.put("zbName", name);map.put("zbType",type);map.put("zbFieldName", fieldName);dataMap.put(id,map);}return "getAllZBs";}public Map<Integer, Object> getDataMap() {return dataMap;}
//分页查指标function getZBsByPage(){var strJson = {};strJson.name=zbPage;$.ajax({type:"get",url:"http://localhost:8080/DemoPlatform/zb-getZBsByPage.action",data:strJson,dataType:"jsonp",error:function(error){swal("失败","获取指标信息失败","question");},success:function(data){for(var zb in data){var zbRow = ‘<tr id=‘+zb+‘><td>‘+data[zb].zbName+‘</td><td>‘+data[zb].zbType+‘</td></tr>‘$(‘#zbTable‘).append(zbRow)}}});}
public String getZBsByPage() throws NumberFormatException, UnsupportedEncodingException, ClassNotFoundException, SQLException{int pageNum = Integer.parseInt(java.net.URLDecoder.decode(getName(), "UTF-8"));dataMap.clear();zbList = zbService.getZBsByPage(pageNum);for(Iterator<ZB> it = zbList.iterator(); it.hasNext();){ZB zb = it.next();int id = zb.getFieldId();String name = zb.getName();int type = zb.getType();String fieldName = zb.getFieldName();Map<String,Object> map = new HashMap<String,Object>();map.put("zbName", name);map.put("zbType",type);map.put("zbFieldName", fieldName);dataMap.put(id,map);}return "getZBsByPage";}
public String getName() {return name;}public void setName(String name) {this.name = name;}
使用Spring+Struts框架,实现用json与前台进行数据交换
原文:http://www.cnblogs.com/mario-nb/p/6473200.html