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

利用Redis构建自定义标签(2)

程序员文章站 2022-07-13 18:06:37
...
最近工作有点忙,不过Redis构建自定义标签也基本已经实现
 
(1)整体设计

利用Redis构建自定义标签(2)
            
    
    博客分类: Redis  
 
 
 
(2)Jedis配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
>

<context:component-scan base-package="com.crm.redis" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大连接数 -->
<property name="maxTotal" value="2000" />
<!--最大空闲数 -->
<property name="maxIdle" value="300" />
<property name="minIdle" value="100" />
<!--最大等待时间ms -->
<property name="maxWaitMillis" value="30000" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
</bean>

<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"
destroy-method="destroy">
<constructor-arg ref="jedisPoolConfig" />

<constructor-arg>
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg value="localhost" />
<constructor-arg type="int" value="6379" />
<constructor-arg value="instance:02" />
</bean>

</list>
</constructor-arg>
</bean>

</beans>
 
 
 
(2)Redis配置

利用Redis构建自定义标签(2)
            
    
    博客分类: Redis  
 
 
 
(3)Redis加载线程
package com.crm.redis.load;
import java.util.HashMap;
import java.util.List;
import com.crm.application.CrmContext;
import com.crm.common.bo.building.BOBuilding;
import com.crm.model.dictionary.BOCrmStaticData;
import com.crm.model.redis.BOCrmRedisCfg;
import com.crm.redis.JedisManager;
import com.crm.redis.load.interfaces.IRedisLoadSV;
import com.crm.service.base.BaseSVImpl;
import com.crm.service.dictionary.StaticDataSVImpl;
import com.crm.service.redis.RedisCfgSVImpl;
public class RedisLoadThread extends Thread {
 @Override
 public void run() {
  System.out.println("Redis加载线程准备执行....");
  loadDictionarys();
  loadCityControlFunctionCodes();
  loadBuildings();
 }
 private void loadBuildings() {
  JedisManager JedisManager = CrmContext.getBean("JedisManager", JedisManager.class);
  BaseSVImpl BaseSVImpl = CrmContext.getBean("BaseSVImpl", BaseSVImpl.class);
  RedisCfgSVImpl RedisCfgSVImpl = CrmContext.getBean("RedisCfgSVImpl", RedisCfgSVImpl.class);
  String sql = "SELECT * FROM DWB_WEB_PROJECT A";
  List<BOCrmRedisCfg> BOCrmRedisCfgs = RedisCfgSVImpl
    .getEntitys(" from BOCrmRedisCfg a where a.state = 'U' and a.code = 'B'", new HashMap());
  List<BOBuilding> BOBuildings = BaseSVImpl.executeNativeSelect(sql, new HashMap(), BOBuilding.class);
  for (BOCrmRedisCfg c : BOCrmRedisCfgs) {
   IRedisLoadSV sv = (IRedisLoadSV) CrmContext.getBean(c.getImplClass());
   sv.load(c, BOBuildings);
  }
 }
 private void loadCityControlFunctionCodes() {
  JedisManager JedisManager = CrmContext.getBean("JedisManager", JedisManager.class);
  RedisCfgSVImpl RedisCfgSVImpl = CrmContext.getBean("RedisCfgSVImpl", RedisCfgSVImpl.class);
  List<String> cityControlFunctionCodes = RedisCfgSVImpl.getCityControlFunctionCodes();
  JedisManager.lpush("cityControlFunctionCodes", cityControlFunctionCodes);
 }
 private void loadDictionarys() {
  JedisManager JedisManager = CrmContext.getBean("JedisManager", JedisManager.class);
  RedisCfgSVImpl RedisCfgSVImpl = CrmContext.getBean("RedisCfgSVImpl", RedisCfgSVImpl.class);
  StaticDataSVImpl StaticDataSVImpl = CrmContext.getBean("StaticDataSVImpl", StaticDataSVImpl.class);
  List<BOCrmRedisCfg> BOCrmRedisCfgs = RedisCfgSVImpl
    .getEntitys(" from BOCrmRedisCfg a where a.state = 'U' and a.code = 'D'", new HashMap());
  List<BOCrmStaticData> BOCrmStaticDatas = StaticDataSVImpl
    .getEntitys(" from BOCrmStaticData a where a.state = 'U' ", new HashMap());
  for (BOCrmRedisCfg c : BOCrmRedisCfgs) {
   IRedisLoadSV sv = (IRedisLoadSV) CrmContext.getBean(c.getImplClass());
   sv.load(c, BOCrmStaticDatas);
  }
 }
 @Override
 public void destroy() {
  System.out.println("线程销毁....");
 }
}
 
 
(4)编写自定义TLD

 

<tag>
  <name>multipleSelect</name>
  <tag-class>com.crm.taglib.CrmMultipleSelectTag</tag-class>
  <body-content>JSP</body-content>

  <attribute>
   <name>id</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <name>className</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <name>crmTagCode</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
 
  <attribute>
   <name>redisKey</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
 
  <attribute>
   <name>valueColumn</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <name>textColumn</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <name>tableName</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <name>condition</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <name>sql</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <name>cityControl</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
 
 
  <attribute>
   <name>width</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <name>onchange</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <name>selectedValues</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
 </tag>

 


 
(5)自定义标签处理类
package com.crm.taglib;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.tags.RequestContextAwareTag;
import com.crm.common.bo.BOSelect;
import com.crm.common.bo.building.BOBuilding;
import com.crm.common.bo.operator.BOCity;
import com.crm.common.util.CrmConst;
import com.crm.common.util.SessionUtil;
import com.crm.model.dictionary.BOCrmStaticData;
import com.crm.redis.JedisManager;
import com.crm.service.base.BaseSVImpl;
@SuppressWarnings("serial")
public class CrmMultipleSelectTag extends RequestContextAwareTag {
 private Log log = LogFactory.getLog(CrmMultipleSelectTag.class);
 private String id;
 private String className;
 private String crmTagCode;
 private String redisKey;
 private String valueColumn;
 private String textColumn;
 private String tableName;
 private String condition;
 private String sql;
 private String cityControl;
 private String width;
 private String onchange;
 private String selectedValues;
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getClassName() {
  return className;
 }
 public void setClassName(String className) {
  this.className = className;
 }
 public String getCrmTagCode() {
  return crmTagCode;
 }
 public void setCrmTagCode(String crmTagCode) {
  this.crmTagCode = crmTagCode;
 }
 public String getRedisKey() {
  return redisKey;
 }
 public void setRedisKey(String redisKey) {
  this.redisKey = redisKey;
 }
 public String getValueColumn() {
  return valueColumn;
 }
 public void setValueColumn(String valueColumn) {
  this.valueColumn = valueColumn;
 }
 public String getTextColumn() {
  return textColumn;
 }
 public void setTextColumn(String textColumn) {
  this.textColumn = textColumn;
 }
 public String getTableName() {
  return tableName;
 }
 public void setTableName(String tableName) {
  this.tableName = tableName;
 }
 public String getCondition() {
  return condition;
 }
 public void setCondition(String condition) {
  this.condition = condition;
 }
 public String getSql() {
  return sql;
 }
 public void setSql(String sql) {
  this.sql = sql;
 }
 public String getCityControl() {
  return cityControl;
 }
 public void setCityControl(String cityControl) {
  this.cityControl = cityControl;
 }
 public String getWidth() {
  return width;
 }
 public void setWidth(String width) {
  this.width = width;
 }
 public String getOnchange() {
  return onchange;
 }
 public void setOnchange(String onchange) {
  this.onchange = onchange;
 }
 public String getSelectedValues() {
  return selectedValues;
 }
 public void setSelectedValues(String selectedValues) {
  this.selectedValues = selectedValues;
 }
 @Override
 protected int doStartTagInternal() throws Exception {
  HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
  BOCity c = SessionUtil.getCityInSession(req);
  long cityId = c.getCityId();
  log.info("开始执行【crm:multipleSelect】标签");
  JspWriter out = pageContext.getOut();
  try {
   String str = null;
   if (CrmConst.CrmTag.SELECT_REDIS.equals(crmTagCode)) {
    str = doSelectRedis(cityId);
   }
   if (CrmConst.CrmTag.SELECT_TABLE.equals(crmTagCode)) {
    str = doSelectSql(cityId);
   }
   out.write(str);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return 0;
 }
 public String geneSQL(long cityId) {
  StringBuilder sql = new StringBuilder();
  sql.append(" SELECT T.").append(valueColumn).append(" SELECT_VALUE, ");
  sql.append(" T.").append(textColumn).append(" SELECT_TEXT ");
  sql.append(" FROM ").append(tableName).append(" T ");
  sql.append(" where 1=1 ");
  if (CrmConst.commonYesOrNoString.YES.equals(cityControl)) {
   sql.append(" AND T.CITY_ID = ").append(cityId);
  }
  if (!StringUtils.isEmpty(condition)) {
   sql.append(condition);
  }
  return sql.toString();
 }
 private String doSelectSql(long cityId) {
  BaseSVImpl BaseSVImpl = this.getRequestContext().getWebApplicationContext().getBean("BaseSVImpl",
    BaseSVImpl.class);
  List<String> selectValuesList = new ArrayList<String>();
  if (selectedValues != null) {
   selectValuesList = Arrays.asList(selectedValues.split(","));
  }
  StringBuffer sb = new StringBuffer();
  List<BOSelect> list = BaseSVImpl.executeNativeSelect(geneSQL(cityId), new HashMap(), BOSelect.class);
  sb.append("<select id='" + id + "' onchange='" + onchange + "' class='" + className
    + "' multiple='' data-placeholder='请选择...'>");
  for (BOSelect s : list) {
   if (selectValuesList.contains(s.getSelectValue())) {
    sb.append("<option value='" + s.getSelectValue() + "' selected='selected'>" + s.getSelectText()
      + "</option>");
   } else {
    sb.append("<option value='" + s.getSelectValue() + "'>" + s.getSelectText() + "</option>");
   }
  }
  sb.append("</select>");
  return sb.toString();
 }
 private String doSelectRedis(long cityId) {
  List<String> selectValuesList = new ArrayList<String>();
  selectValuesList = Arrays.asList(selectedValues.split(","));
  JedisManager JedisManager = this.getRequestContext().getWebApplicationContext().getBean("JedisManager",
    JedisManager.class);
  List<String> cityControlFunctionCodes = JedisManager.lrange("cityControlFunctionCodes", 0, -1);
  String[] redisKeyArray = redisKey.split(":");
  StringBuffer _redisKey = new StringBuffer(redisKey);
  if (cityControlFunctionCodes.contains(redisKeyArray[1])) {
   _redisKey.append(":").append(cityId);
  } else {
   _redisKey.append(":").append(0);
  }
  StringBuffer sb = new StringBuffer();
  if (CrmConst.RedisCfgCode.DICTIONARY.equals(redisKeyArray[0])) {
   List<BOCrmStaticData> list = (List<BOCrmStaticData>) JedisManager.getObject(_redisKey.toString());
   sb.append("<select id='" + id + "' onchange='" + onchange + "' class='" + className
     + "' multiple='' data-placeholder='请选择...'>");
   for (BOCrmStaticData s : list) {
    if (selectValuesList.contains(s.getValue())) {
     sb.append("<option value='" + s.getValue() + "' selected='selected'>" + s.getName() + "</option>");
    } else {
     sb.append("<option value='" + s.getValue() + "'>" + s.getName() + "</option>");
    }
   }
   sb.append("</select>");
  }
  if (CrmConst.RedisCfgCode.BUILDING.equals(redisKeyArray[0])) {
   List<BOBuilding> list = (List<BOBuilding>) JedisManager.getObject(_redisKey.toString());
   sb.append("<select id='" + id + "' onchange='" + onchange + "' class='" + className
     + "' multiple='' data-placeholder='请选择...'>");
   for (BOBuilding s : list) {
    if (selectValuesList.contains(s.getPrjId())) {
     sb.append("<option value='" + s.getPrjId() + "' selected='selected'>" + s.getPrjItemname()
       + "</option>");
    } else {
     sb.append("<option value='" + s.getPrjId() + "'>" + s.getPrjItemname() + "</option>");
    }
   }
   sb.append("</select>");
  }
  return sb.toString();
 }
}
 
 
(6)页面使用
 
<crm:singleSelect id="buildingId" crmTagCode="SELECT_REDIS" redisKey="B:100"
          className="chosen-select col-xs-10 col-sm-5" selectedValues="${BOCrmProject.buildingId}">
</crm:singleSelect>
 
(7)页面效果以及Redis存储
 

利用Redis构建自定义标签(2)
            
    
    博客分类: Redis  
 
利用Redis构建自定义标签(2)
            
    
    博客分类: Redis  
 
利用Redis构建自定义标签(2)
            
    
    博客分类: Redis  
 
 
  • 利用Redis构建自定义标签(2)
            
    
    博客分类: Redis  
  • 大小: 24.2 KB
  • 利用Redis构建自定义标签(2)
            
    
    博客分类: Redis  
  • 大小: 19.6 KB
  • 利用Redis构建自定义标签(2)
            
    
    博客分类: Redis  
  • 大小: 21.5 KB
  • 利用Redis构建自定义标签(2)
            
    
    博客分类: Redis  
  • 大小: 57.6 KB
  • 利用Redis构建自定义标签(2)
            
    
    博客分类: Redis  
  • 大小: 23.3 KB