代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
java自定义标签
作者:
BlackPotato
/ 发布于
2015/8/3
/
474
自定义标签,简单的配置下表名,显示的字段名,值字段名,附加条件,即可展现~
CrmSelectTag.java package com.crm.taglib; import java.util.HashMap; import java.util.List; 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.stereotype.Component; import org.springframework.web.servlet.tags.RequestContextAwareTag; import com.crm.common.bo.BOSelect; import com.crm.common.util.CrmConst; import com.crm.service.base.CommonSVImpl; @SuppressWarnings("serial") @Component("CrmSelectTag") public class CrmSelectTag extends RequestContextAwareTag { private Log log = LogFactory.getLog(CrmSelectTag.class); private String id; private String crmTagCode; private String valueColumn; private String textColumn; private String tableName; private String condition; private String sql; private String width; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCrmTagCode() { return crmTagCode; } public void setCrmTagCode(String crmTagCode) { this.crmTagCode = crmTagCode; } 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 getWidth() { return width; } public void setWidth(String width) { this.width = width; } public String geneSQL() { 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 "); sql.append(condition); return sql.toString(); } @Override protected int doStartTagInternal() throws Exception { if(StringUtils.isEmpty(width)) { width = "170"; } CommonSVImpl CommonSV = this.getRequestContext().getWebApplicationContext().getBean("CommonSVImpl",CommonSVImpl.class); log.info("开始执行【crm:select】标签"); JspWriter out = pageContext.getOut(); try { StringBuffer sb = new StringBuffer(); if (CrmConst.crmTag.SELECT_TABLE.equals(crmTagCode)) { List<BOSelect> list = CommonSV.executeNativeSelect(geneSQL(), new HashMap(), BOSelect.class); sb.append("<select id=\""+id+"\" style=\"width: "+width+"px\">"); sb.append("<option value=\"-1\">---请选择---</option>"); for(BOSelect s : list) { sb.append("<option value=\""+s.getSelectValue()+"\">"+s.getSelectText()+"</option>"); } sb.append("</select>"); } out.write(sb.toString()); } catch (Exception e) { e.printStackTrace(); } return 0; } } CrmRadioTag.java package com.crm.taglib; import java.util.HashMap; import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.crm.common.util.CrmConst; @SuppressWarnings("serial") public class CrmRadioTag extends TagSupport { private Log log = LogFactory.getLog(CrmRadioTag.class); private String crmTagCode; private String name; public String getCrmTagCode() { return crmTagCode; } public void setCrmTagCode(String crmTagCode) { this.crmTagCode = crmTagCode; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int doStartTag() throws JspException { log.info("开始执行标签"); JspWriter out = pageContext.getOut(); try { StringBuffer sb = new StringBuffer(); if (CrmConst.crmTag.RADIO_COMMON.equals(crmTagCode)) { Map<String,String> m = new HashMap<String,String>(); m.put("0", "否"); m.put("1", "是"); for (String key : m.keySet()) { sb.append(" <input type=\"radio\" name=\"").append(name).append("\"") .append(" value=\"").append(key).append("\"") .append(" \\>").append(m.get(key)); } //sb.append("<script type=\"text/javascript\">"); //sb.append("</script>"); } out.write(sb.toString()); } catch (Exception e) { e.printStackTrace(); } return super.doStartTag(); } @Override public int doEndTag() throws JspException { return super.doEndTag(); } } spring-mvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd "> <context:component-scan base-package="com.crm.controller" /> <mvc:annotation-driven /> <bean id="crmViewResolver" class="com.crm.resolver.MultViewResolver"> <property name="resolvers"> <map> <!-- 定义跳转的文件的前后缀 ,视图模式配置 --> <entry key="jsp"> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean> </entry> <entry key="ftl"> <bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /> <property name="contentType" value="text/html; charset=utf-8" /> <property name="cache" value="false" /> <property name="prefix" value="" /> <property name="suffix" value=".ftl" /> </bean> </entry> </map> </property> </bean> <!-- ******************************freeMarker配置【START】****************************** --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/ftl/" /> <property name="freemarkerVariables"> <map> <entry key="xml_escape" value-ref="fmXmlEscape" /> </map> </property> <property name="defaultEncoding" value="utf-8" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">3600</prop> </props> </property> </bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" /> <!-- ******************************freeMarker配置【END】****************************** --> <mvc:interceptors> <!-- 多个拦截器,顺序执行 --> <mvc:interceptor> <mvc:mapping path="/**" /> <mvc:exclude-mapping path="/resource/**" /> <bean id="CrmInterceptor" class="com.crm.interceptor.CrmInterceptor"> <property name="allowUrls"> <list> <value>/crm/</value> <value>/crm/login</value> </list> </property> </bean> </mvc:interceptor> </mvc:interceptors> <mvc:resources mapping="/resource/**" location="/resource/" /> </beans> demo.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix ="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ include file="../common/commonHead.jsp"%> <%@ taglib prefix="crm" uri="../../tld/crm.tld"%> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="content-type" content="text/html; charset=gbk" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>TAG DEMO</title> </head> <body> <div class="container"> <div class="row"> <table class="table"> <thead> <tr> <th width="50%">API</th> <th width="50%">DEMO</th> </tr> <tr> <td> <textarea style="width: 100%"> <crm:radio name="aa" crmTagCode="RADIO_COMMON"></crm:radio> </textarea> </td> <td> <crm:radio name="aa" crmTagCode="RADIO_COMMON"></crm:radio> </td> </tr> <tr> <td> <textarea style="width: 100%"> <crm:radio name="aa" crmTagCode="RADIO_COMMON"></crm:radio> </textarea> </td> <td> <crm:select tableName="CRM_OPERATOR" textColumn="NAME" valueColumn="CODE" crmTagCode="SELECT_TABLE" id="test"> </crm:select> </td> </tr> </thead> </table> </div> </div> </body> <script type="text/javascript"> </script> </html>
试试其它关键字
自定义标签
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
BlackPotato
贡献的其它代码
(
3
)
.
圆盘记速表
.
统计数字
.
java自定义标签
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3