欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在基于Spring架构的通用查询系统中用DWR实现查询条件的动态显示

程序员文章站 2022-07-08 09:27:15
...
一、首先有必要对通用查询系统做个简单介绍:该系统基于Spring+Hibernate+Struts架构,主要包括查询设计器(查询脚本维护、查询分类排序、查询条件维护、查询类型维护)和信息查询两大功能,其主要目的是为应用于软件项目开发当中,解决其全部查询模块和功能模块的条件部分,以加快项目开发速度。
二、说说系统开发中的难点:第一、在应用于项目当中去时怎样保持自己的独立性,也就是解耦的问题,第二、怎样根据各个项目的特点动态地扩展其跟业务逻辑相关的查询条件,这里就涉及到一个查询条件的动态显示问题。
三、接下去就讲正题,用DWR实现查询条件的动态显示
   1、构建查询条件服务:业务逻辑相关的查询条件就通过实现ICxtjValues接口的插件通过Spring配置文件注入给查询条件服务CxtjService,这样就解决了在解耦的情况下实现查询条件扩展。
CxtjService.java代码:
package gov.nbgs.sql.service.impl;
import gov.nbgs.common.bean.Entity;
import gov.nbgs.common.dao.IEntityDao;
import gov.nbgs.common.service.impl.EntityService;
import gov.nbgs.sql.plugin.ICxtjValues;
import java.util.Collection;
import java.util.Map;
import org.apache.log4j.Logger;

public class CxtjService extends EntityService {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CxtjService.class);
	private Map cxtjValuesMap;
	protected IEntityDao getDao(Entity arg0) {
		return null;
	}
	public Map getCxtjValuesMap() {
		return cxtjValuesMap;
	}
	public void setCxtjValuesMap(Map cxtjValuesMap) {
		this.cxtjValuesMap = cxtjValuesMap;
	}
	public Collection getCollectionCxtjValues(String key) {
		if (logger.isDebugEnabled()) {
			logger.debug("getCollectionCxtjValues(String)=>"+key);
		}
		ICxtjValues cxtjValues = (ICxtjValues)this.getCxtjValuesMap().get(key);
		if(cxtjValues==null) return null;
		if (logger.isDebugEnabled()) {
			logger.debug("getCollectionCxtjValues(String)=>"+cxtjValues.getCollectionCxtjValues().size());
		}
		return cxtjValues.getCollectionCxtjValues();
	}
	public Map getMapCxtjValues(String key) {
		if (logger.isDebugEnabled()) {
			logger.debug("getCxtjValuesMap(String)=>"+key);
		}
		ICxtjValues cxtjValues = (ICxtjValues)this.getCxtjValuesMap().get(key);
		if(cxtjValues==null) return null;
		if (logger.isDebugEnabled()) {
			logger.debug("getCxtjValuesMap(String)=>"+cxtjValues.getMapCxtjValues().size());
		}
		return cxtjValues.getMapCxtjValues();
	}	
}


Spring相关配置:
<beans default-autowire="byName">
  <bean id="swjgCxtjValues" class="gov.nbgs.demo.plugin.SwjgCxtjValues"/>  
  <bean id="cxtjService" class="gov.nbgs.sql.service.impl.CxtjService">
    <property name="cxtjValuesMap">
      <map>
        <entry key="dddd">
          <ref bean="swjgCxtjValues"/>
        </entry>  
        <entry key="djzclxdm">
          <ref bean="swjgCxtjValues"/>
        </entry>             
      </map>
    </property>        
  </bean>
</beans>


DWR配置文件:
<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
    "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
	<allow>	
		<convert converter="bean"
			match="org.apache.struts.util.LabelValueBean" />
		<create creator="spring" javascript="CxtjService">
			<param name="beanName">cxtjService</param>
			<include method="getCollectionCxtjValues" />
			<include method="getMapCxtjValues" />
		</create>
	</allow>
</dwr>


查询条件显示CxLayout.jsp部分代码:
<%@ page contentType="text/html; charset=gb2312"%>
<%@ taglib uri="struts-bean" prefix="bean"%>
<%@ taglib uri="struts-html" prefix="html"%>
<%@ taglib uri="struts-logic" prefix="logic"%>
<%@ taglib uri="struts-tiles" prefix="tiles"%>

<jsp:useBean id="cxtjForm" scope="session"
	class="gov.nbgs.sql.web.form.CxtjForm" />

<html:html>
<head>
<title><tiles:getAsString name="title" /></title>

<script src='dwr/interface/CxtjService.js'></script>
<script src='dwr/engine.js'></script>
<script src='dwr/util.js'></script>
<script>
  var cxtjAreaId = "cxtjArea";
  var cxtjTagName = "span";
  
  function onload() {   
    var cxtjList = getPluginCxtjList();
    for(var i = 0; i < cxtjList.length; i++){
      var cxtj = cxtjList[i];
      var cxtjId = cxtj.cxtjId;        
      CxtjService.getMapCxtjValues(cxtjId, {
  	  		callback:function(dataFromServer) {
    			populateCxtjValues(dataFromServer, cxtj);
  			},
  			async:false
	  });      
    }
  }  
  
  function getPluginCxtjList() { 
    var result = new Array();
  
    var cxtjArea = document.getElementById(cxtjAreaId);
   
    var cxtjTagList = document.getElementsByTagName(cxtjTagName);
    //alert("cxtjTagList.length=>" + cxtjTagList.length);
    
    for(var i = 0; i < cxtjTagList.length; i++){
      var cxtjTag = cxtjTagList[i];
      //alert("cxtjTag.className=>" + cxtjTag.className);
      if(cxtjTag.className=="PluginCxtj"){
        var cxtjId = cxtjTag.childNodes[0].nodeValue;
        var cxtjTagId = cxtjTag.id;
        var cxtjType = cxtjTag.getAttribute("type");
        //alert("cxtjType=>" + cxtjType);
        
        var cxtj = {cxtjId: cxtjId, cxtjTagId: cxtjTagId, cxtjType: cxtjType};
        result[result.length] = cxtj;       		
      }
    }
    return result;
  }

  function createCxtjObject(cxtjId,cxtjTagId,cxtjType){
    var select = document.createElement("select");
    select.setAttribute("id",cxtjId);

    var tagCxtj = document.getElementById(cxtjTagId);
    tagCxtj.removeChild(tagCxtj.childNodes[0]);
    tagCxtj.appendChild(select);
  }    

  function createCxtjInputBox(dataFromServer,cxtjId,cxtjTagId,cxtjType){
    var tagCxtj = document.getElementById(cxtjTagId);
    tagCxtj.removeChild(tagCxtj.childNodes[0]);
    var i = 0;
    for (var key in dataFromServer) {
      var val = dataFromServer[key];
      //alert(key + "=" + val);
      var input_id = cxtjId + "_" + i;
      //alert(input_id);
      if(cxtjType=='radio'){
        var input = createElement("input", cxtjId);   
      }else{
        var input = createElement("input", input_id); 
      } 
      input.setAttribute("type",cxtjType); 
      input.setAttribute("value",key); 
      input.setAttribute("id",input_id);
      tagCxtj.appendChild(input);
      
      var label = document.createElement("label");
      label.setAttribute("for",input_id);
      label.appendChild(document.createTextNode(val));    
      tagCxtj.appendChild(label);  
      
      i++;
    }         
  }    
       
  function populateCxtjValues(dataFromServer, cxtj){  
    var cxtjId = cxtj.cxtjId;
    var cxtjTagId = cxtj.cxtjTagId;
    var cxtjType = cxtj.cxtjType;  
    if(cxtjType=='select'){
      createCxtjObject(cxtjId,cxtjTagId,cxtjType);
      DWRUtil.removeAllOptions(cxtjId);
      DWRUtil.addOptions(cxtjId, dataFromServer);      
    }else{
      createCxtjInputBox(dataFromServer,cxtjId,cxtjTagId,cxtjType);
    }
  }
  
  function createElement(type, name) {
    var element = null;

    try {
      // First try the IE way; if this fails then use the standard way
      element = document.createElement('<'+type+' name="'+name+'">');
    } catch (e) {
      // Probably failed because we’re not running on IE
    }
    if (!element) {
      element = document.createElement(type);
      element.name = name;
    }
    return element;
  }      
</script>
</head>
<body onload="onload()">
................
................
  <div id="cxtjArea">	
    <logic:present name="cxtjForm" property="cxtjMap">
    <bean:define id="cxtjMap" name="cxtjForm" property="cxtjMap" type="java.util.Map" />
    <logic:iterate id="cxtj" name="cxtjMap">
      <tr class=jnfont1>
 	 <td><bean:write name="cxtj" property="key"/></td>
 	 <td>
 	      <bean:define id="cxtjvalue" name="cxtj" property="value"/>
 	       <% out.println(cxtjvalue); %>
 	 </td>
       </tr>
     </logic:iterate> 
    </logic:present>	
  </div>
..................
.................
</body>
</html:html>


四、初次写技术博客文章,不当之处望各位网友提醒指正!