设为首页 收藏本站
查看: 452|回复: 0

[经验分享] 利用Redis构建自定义标签(2)

[复制链接]

尚未签到

发表于 2016-12-20 09:28:28 | 显示全部楼层 |阅读模式
最近工作有点忙,不过Redis构建自定义标签也基本已经实现

 

(1)整体设计

DSC0000.png
 

 

 

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



DSC0001.png
 


 

 

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


 


DSC0002.png
 
DSC0003.png
 
DSC0004.png
 

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-316796-1-1.html 上篇帖子: redis高级(分布式缓存实现,spring integration) 下篇帖子: phpRedisAdmin:Redis的WEB界面管理工具
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表