使用Spring+Struts框架,实现用json与前台进行数据交换
框架搭建
Eclipse中对应的W工程是Dynamic Web Project
引入jar包
一股脑都加进来了,多总比少了强;
配置文件
建一个con文件夹,右键Build Path
Use as Source Folder
一般来说需要三个配置文件:
applicationContext
<?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.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 自动扫描 -->
<bean
class="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>
黄色的都是套嗑儿;
青色的是数据连接池,视情况使用;
紫色的是要交给Spring管理的类,Spring会在运行时,自动进行类的注入;由于Spring是利用set()函数进行自动注入类的初始化的,set函数必须要有;
若在一个类型需要进行他所调用类的自动注入,则需要在配置文件中,在该类的bean中,进行需要注入类的说明,和利用注解的效果是一样的。可以理解为向Spring说明一声,我需要你为我注入这个类;
struts.xml
<?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>
例如访问的路径为:
http://localhost:8080/DemoPlatform/zb-getAllZBs.action
db.properties
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///ci?useUnicode=true&characterEncoding=UTF-8
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
就是为了使用方便
整体流程
entity
就是正常的实体类,一堆属性,一堆get,set
dao
正常的dao
service
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;
}
}
别忘记在applicationContext类中,进行配置;
set方法很重要;
action
public class ZBAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private Map<Integer, Object> dataMap; //struct把这个map,转成json
private 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;
}
dataMap的get方法很重要,struts根据这个get得到这个map,并自动转化为json;
接收前台传来的json
前台采用ajax的形式来进行json的传递;
注意:当ajax的访问路径不是同源路径时,需要使用jsonp进行跨域访问;
前台ajax:
//分页查指标
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)
}
}
});
}
注意:ajax传输的json名字是name,在接下来的struts的对应action中,我们需要为这个名字为name的json创建一个get方法,方便struts框架将这个json进行取值;
后台action:
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;
}
注意:
- get方法千万别忘记了;
- java.net.URLDecoder.decode(getName(), "UTF-8")
附件列表