`
toknowme
  • 浏览: 137349 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

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

 
阅读更多
最近工作有点忙,不过Redis构建自定义标签也基本已经实现
 
(1)整体设计


 
 
 
(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配置


 
 
 
(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存储
 


 

 

 
 
  • 大小: 24.2 KB
  • 大小: 19.6 KB
  • 大小: 21.5 KB
  • 大小: 57.6 KB
  • 大小: 23.3 KB
0
0
分享到:
评论
1 楼 liyonghui160com 2017-01-11  
请问 redis的结构是什么样的?

相关推荐

    docker-compose

    Compose 将会利用它自动构建这个镜像,然后使用这个镜像启动服务容器 build: # context 选项可以是 Dockerfile 的文件路径,也可以是到链接到 git 仓库的 url # 当提供的值是相对路径时,它被解析为相对于撰写...

    java开源包2

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包1

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包11

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包3

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包6

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包5

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包10

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包4

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包8

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包7

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包9

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包101

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    Java资源包01

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    JAVA上百实例源码以及开源项目源代码

    Java生成自定义控件源代码 2个目标文件 Java实现HTTP连接与浏览,Java源码下载 1个目标文件 摘要:Java源码,网络相关,HTTP  Java实现HTTP连接与浏览,Java源码下载,输入html文件地址或网址,显示页面和HTML源文件...

    JAVA上百实例源码以及开源项目

    Java生成自定义控件源代码 2个目标文件 Java实现HTTP连接与浏览,Java源码下载 1个目标文件 摘要:Java源码,网络相关,HTTP  Java实现HTTP连接与浏览,Java源码下载,输入html文件地址或网址,显示页面和HTML源文件...

    ThinkPHP 3.1.2 - PHP的开发框架MVC - 含Core,Extend,Example

    模板标签,融合了Smarty和JSP标签库的思想,并内置布局模板功能和标签库扩展 支持。通过驱动还可以支持Smarty、EaseTemplate、TemplateLite、Smart等其他第 三方模板引擎。 AJAX支持:内置和客户端无关的AJAX数据...

Global site tag (gtag.js) - Google Analytics