使用Spring+Struts框架,实现用json与前台进行数据交换

框架搭建

Eclipse中对应的W工程是Dynamic Web Project

引入jar包

一股脑都加进来了,多总比少了强;

配置文件

建一个con文件夹,右键Build Path
Use as Source Folder

一般来说需要三个配置文件:

applicationContext

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  11. <!-- 导入资源文件 -->
  12. <context:property-placeholder location="classpath:db.properties" />
  13. <!-- 自动扫描 -->
  14. <bean
  15. class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
  16. <!-- C3P0 -->
  17. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  18. <property name="user" value="${jdbc.user}"></property>
  19. <property name="password" value="${jdbc.password}"></property>
  20. <property name="driverClass" value="${jdbc.driverClass}"></property>
  21. <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  22. <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
  23. <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
  24. </bean>
  25. <!-- 事务控制 -->
  26. <bean id="transactionManager"
  27. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  28. <property name="dataSource" ref="dataSource" />
  29. </bean>
  30. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
  31. abstract="false" lazy-init="false" autowire="default">
  32. <property name="dataSource">
  33. <ref bean="dataSource" />
  34. </property>
  35. </bean>
  36. <bean id="zbDAO" class="com.heu.dao.ZBDAO">
  37. </bean>
  38. <bean id="zbService" class="com.heu.service.ZBService">
  39. <property name="zbDAO" ref="zbDAO"></property>
  40. </bean>
  41. <bean id="zbAction" class="com.heu.action.ZBAction" scope="prototype">
  42. <property name="zbService" ref="zbService"></property>
  43. </bean>
  44. </beans>
黄色的都是套嗑儿;
青色的是数据连接池,视情况使用;
紫色的是要交给Spring管理的类,Spring会在运行时,自动进行类的注入;由于Spring是利用set()函数进行自动注入类的初始化的,set函数必须要有;
若在一个类型需要进行他所调用类的自动注入,则需要在配置文件中,在该类的bean中,进行需要注入类的说明,和利用注解的效果是一样的。可以理解为向Spring说明一声,我需要你为我注入这个类;

struts.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  3. "http://struts.apache.org/dtds/struts-2.0.dtd">
  4. <struts>
  5. <constant name="struts.i18n.encoding" value="GBK" />
  6. <package name="json" extends="json-default" namespace="/">
  7. <action name="zb-*" class="zbAction" method="{1}">
  8. <result name="getAllZBs" type="json">
  9. <param name="root">dataMap</param>
  10. <param name="callbackParameter">callback</param>
  11. </result>
  12. </action>
  13. </package>
  14. </struts>
例如访问的路径为:
http://localhost:8080/DemoPlatform/zb-getAllZBs.action

db.properties

  1. jdbc.user=root
  2. jdbc.password=root
  3. jdbc.driverClass=com.mysql.jdbc.Driver
  4. jdbc.jdbcUrl=jdbc:mysql:///ci?useUnicode=true&characterEncoding=UTF-8
  5. jdbc.initPoolSize=5
  6. jdbc.maxPoolSize=10
就是为了使用方便

整体流程

entity

就是正常的实体类,一堆属性,一堆get,set

dao

正常的dao

service

  1. public class ZBService {
  2. private ZBDAO zbDAO;
  3. public ZBDAO getZbDAO() {
  4. return zbDAO;
  5. }
  6. public void setZbDAO(ZBDAO zbDAO) {
  7. this.zbDAO = zbDAO;
  8. }
  9. public List<ZB> getAllZBs() throws ClassNotFoundException, SQLException{
  10. List<ZB> zbs = ZBDAO.getZBs();
  11. return zbs;
  12. }
  13. }
别忘记在applicationContext类中,进行配置;
set方法很重要;

action

  1. public class ZBAction extends ActionSupport{
  2. private static final long serialVersionUID = 1L;
  3. private Map<Integer, Object> dataMap; //struct把这个map,转成json
  4. private ZBService zbService;
  5. private List<ZB> zbList;
  6. public ZBAction(){
  7. dataMap = new HashMap<Integer, Object>();//一定要在构造函数进行map的初始化,否则会报空指针
  8. }
  9. public ZBService getZbService() {
  10. return zbService;
  11. }
  12. public void setZbService(ZBService zbService) {
  13. this.zbService = zbService;
  14. }
  15. public String getAllZBs() throws ClassNotFoundException, SQLException{
  16. dataMap.clear();
  17. zbList = zbService.getAllZBs();
  18. for(Iterator<ZB> it = zbList.iterator(); it.hasNext();)
  19. {
  20. ZB zb = it.next();
  21. int id = zb.getFieldId();
  22. String name = zb.getName();
  23. int type = zb.getType();
  24. String fieldName = zb.getFieldName();
  25. Map<String,Object> map = new HashMap<String,Object>();
  26. map.put("zbName", name);
  27. map.put("zbType",type);
  28. map.put("zbFieldName", fieldName);
  29. dataMap.put(id,map);
  30. }
  31. return "getAllZBs";
  32. }
  33. public Map<Integer, Object> getDataMap() {
  34. return dataMap;
  35. }


dataMap的get方法很重要,struts根据这个get得到这个map,并自动转化为json;

接收前台传来的json

前台采用ajax的形式来进行json的传递;

注意:当ajax的访问路径不是同源路径时,需要使用jsonp进行跨域访问;

前台ajax:

  1. //分页查指标
  2. function getZBsByPage(){
  3. var strJson = {};
  4. strJson.name=zbPage;
  5. $.ajax({
  6. type:"get",
  7. url:"http://localhost:8080/DemoPlatform/zb-getZBsByPage.action",
  8. data:strJson,
  9. dataType:"jsonp",
  10. error:function(error){
  11. swal("失败","获取指标信息失败","question");
  12. },
  13. success:function(data){
  14. for(var zb in data){
  15. var zbRow = '<tr id='+zb+'><td>'+data[zb].zbName+'</td><td>'+data[zb].zbType+'</td></tr>'
  16. $('#zbTable').append(zbRow)
  17. }
  18. }
  19. });
  20. }

注意:ajax传输的json名字是name,在接下来的struts的对应action中,我们需要为这个名字为name的json创建一个get方法,方便struts框架将这个json进行取值;

后台action:

  1. public String getZBsByPage() throws NumberFormatException, UnsupportedEncodingException, ClassNotFoundException, SQLException{
  2. int pageNum = Integer.parseInt(java.net.URLDecoder.decode(getName(), "UTF-8"));
  3. dataMap.clear();
  4. zbList = zbService.getZBsByPage(pageNum);
  5. for(Iterator<ZB> it = zbList.iterator(); it.hasNext();)
  6. {
  7. ZB zb = it.next();
  8. int id = zb.getFieldId();
  9. String name = zb.getName();
  10. int type = zb.getType();
  11. String fieldName = zb.getFieldName();
  12. Map<String,Object> map = new HashMap<String,Object>();
  13. map.put("zbName", name);
  14. map.put("zbType",type);
  15. map.put("zbFieldName", fieldName);
  16. dataMap.put(id,map);
  17. }
  18. return "getZBsByPage";
  19. }
  1. public String getName() {
  2. return name;
  3. }
  4. public void setName(String name) {
  5. this.name = name;
  6. }

注意:
  • get方法千万别忘记了;
  • java.net.URLDecoder.decode(getName(), "UTF-8"
        这个是对前台传来的中文json进行转码,例子中传输的是一个数字,不用也行;











附件列表

     

    posted @ 2017-02-27 12:16  -Mario-  阅读(486)  评论(0编辑  收藏  举报