首先,推荐一下站里的这篇经典文章:
http://struts2.group.iteye.com/group/wiki/1463-taglib-the-eternal-debate-topic文中的许多观点到现在还是非常有参考价值。文章写于2009年,又是4年多过去,一些外部环境发生了变化。
>Spring MVC超越了Struts 成为了MVC框架的首选。
>对于前端使用JSP的应用来说,JSTL+EL表达式混合使用是开发的“标准方式”。
>自定义标签的应用依然非常广泛。
按上文的观点,标签分为 逻辑控制类,数据输出类,页面组件类 。
前两种相对简单,本文主要涉及应用中较复杂的 页面组件类标签。
个人认为开发中需要提取共通UI代码应根据实际需求按以下顺序考虑实现。
框架标签 > 标签文件 > taglib标签 > 脚本方式
●框架标签
优点:稳定可靠,代码维护性好。 (相比来说SpringMVC对标签的支持远不如Strus)
缺点:无法与后台交互。
应用例:一般用于简化通用标签的书写
●标签文件
优点:特别适用于复杂的自定义UI封装。
缺点:和框架标签一样,没有给后台的入参,也不适用于动态应用。
应用例:比如分页,排序一类标签(
http://duanhengbin.iteye.com/admin/blogs/1998017)
●taglib标签
优点:封装性较好,装载数据全在标签代码中。
标签入参可以根据需要自定义,也就是前端可以“影响”后台的实现。
缺点:代码的开发和调试都比较繁琐,还需要做 tld文件。
只能用于页面初期展示阶段,对于“纯前端”的动作无能为力了。
应用例:数据字典下拉框 (见下文)
●脚本方式
优点:JS提共通较为简单,灵活性最高,可以应对几乎任何需求
缺点:前端代码会多些。主要是后台做数据加载也比较麻烦,所需数据通过页面自己加载,即相关的所有Controller层代码都需要修改,代码通用性和维护性都比较差。不是特殊需求不建议使用这种方式。
应用例:比如需要将 数据字典下拉框动态加入到table中,只有通过JS拼装的方式
【taglib标签 实现通用数据字典展示例子】
●taglib类(推荐继承SimpleTagSupport,代码比较简洁)
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.apache.commons.jxpath.JXPathContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cn.com.intasect.ots.dto.DictionaryDTO;
import cn.com.intasect.ots.service.IDictionaryService;
public class DictorySelectTag2 extends SimpleTagSupport {
private String id; //必设项目:设置select的id
private String typecode; //必设项目:对应数据字典中"字典类型类型"
private String name; //必设项目:form提交用
private String typeid; //对应数据字典中"字典ID"(不设置时默认显示第一项)
private static List<DictionaryDTO> dictionaryList = null;
/**
* 缓存数据字典
*/
private void loadDictionaryList(){
if ( dictionaryList==null){
ServletContext servletContext = ((PageContext) this.getJspContext()).getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
IDictionaryService dictoryService = (IDictionaryService) wac.getBean("dictionaryServiceImpl");
dictionaryList = dictoryService.selectAll();
}
}
@Override
public void doTag() throws IOException, JspException {
loadDictionaryList();
JspContext jspContext = getJspContext();
JspWriter out = jspContext.getOut();
StringBuffer sb = new StringBuffer();
//拼装select项目内容
sb.append("\n<select")
.append(" id='").append(id).append("'")
.append(" name='").append(name).append("'")
.append(" value='").append(typeid==null?"":typeid).append("'")
.append(">\n");
//此处使用 Commons JXPath 做对象查询
String condition = ".[cDictTypeCode='"+ typecode +"']";
JXPathContext context = JXPathContext.newContext(dictionaryList);
sb.append("<option value=null>--请选择--</option>\n");
//拼装option项目内容
for(@SuppressWarnings("rawtypes")Iterator iter = context.iterate(condition); iter.hasNext();){
DictionaryDTO dto = (DictionaryDTO)iter.next();
sb.append("<option")
.append(" value='").append(dto.getnDictid()).append("'")
.append(dto.getnDictid().equals(typeid)?" selected='selected'":"")
.append(">")
.append(dto.getcDictTypeName())
.append("</option>\n");
}
sb.append("</select>\n");
out.print(sb.toString());
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setTypecode(String typecode) {
this.typecode = typecode;
}
public void setTypeid(String typeid) {
this.typeid = typeid;
}
}
●tld文件
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>dictionary tag</description>
<tlib-version>1.0</tlib-version>
<short-name>dictionary tag</short-name>
<tag>
<name>select</name>
<tag-class>com.test.tags.DictorySelectTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>id</name>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<name>name</name>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<name>typecode</name>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<name>typeid</name>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
●应用代码
JSP的头部加入引用
<%@ taglib prefix="tags" uri="/WEB-INF/tags-tld/dict_tags.tld" %>
标签代码
<tags:select id='levelSelect' name='levelId' typecode='DICT_LEVEL' typeid='${levelId}'/>
【脚本方式 实现通用数据字典展示例子】
后台代码,对于所有应用了该功能的相关Controller类需要加上以下代码
@Resource
private IDictionaryService dictoryService;
@ModelAttribute("dict")
public List<DictionaryDTO> getDictionary(){
return dictoryService.selectAll();
}
前端代码非本文重点,略
分享到:
相关推荐
Uipath注入JS
unity UI脚本生成器!
可移动UI的iTween脚本
使用SeleniumIDE录制UI自动化测试脚本UI自动化录制.docx
提取标签的脚本
dhtmlx ui控件脚本 dhtmlxLayout is a JavaScript DHTML component that provides powerful and efficient solution for building Ajax-enabled web interfaces with ease. This component allows you to ...
jquery ui
unity-ui-typewriter 打字机脚本 文本打字机脚本 这是一个小型C#脚本,用于实时模拟打字文本。非常适合打字机或计算机终端。您可以自由更改脚本并在项目中使用它。如果您决定使用该脚本,我希望您能在项目中提供链接...
unity ugui UI脚本自动生成工具,可以生成C# 和 Lua 脚本,还可以在面板上预览生成的脚本
JavaScript应用实例-脚本管理工具UI示例.js
本资源主要介绍VisionPro脚本简单实现文本显示以及创建标签的功能。
AutoJs源码-脚本管理工具UI示例。本资源购买前提醒:本源码都是实际autojs项目模板,安装好autojs直接运行即可打开。1、支持低版本autojs。2、资源仅供学习与参考,请勿用于商业用途,否则产生的一切后果将由您自己...
unity游戏界面UI动画脚本GUI Animator。unity游戏界面UI动画脚本GUI Animator for Unity UI 0.8.3。GUI Animator for Unity UI v0.8.3.unitypackage
软件测试中如何使用自动化脚本?1)QTP工具。QTP是一个快速测试专业工具。它的优点是可以快速建立企业自动化框架,但不是一个全能的工具,因为利用QTP并不能帮助用户找出更多的BUG,只能提高执行测试用例的效率。QTP...
dw网页设计标签,脚本,可以对初学者们一个借鉴
增加了标签的和注释的bash脚本编程,所有脚本的注释经过验证过。标签能快速定位,注释能更清楚脚本意思。
适合ligerui上实现图片上传
交互方式修改密码 1. ssh 远程到主机; 2. 切换到root账号; [一般都是切换到root进行密码修改,如果普通用户修改自己的密码,要输入... 3. passwd username ... ...方式1: echo password | passwd testuser --stdin > /dev
处理wiki9标签的脚本
unity2019转2018 ui脚本丢失问题自动一键修复 把本程序拷贝到Assets同级目录 ,也就是项目跟目录 ,点击exe程序自动修复完成