自定义—扩展struts2的标签
扩展 自定义 标签 struts2
2023-09-27 14:28:01 时间
最近在做j2ee的项目,需要封装很多标签,发现直接从BodyTagSupport继承的话,无法获取valuestack,也无法借用struts的国际化解决方案。所以需要扩展struts的标签。
看了网上很多的扩展方法,觉得只能做为参考或示例,但却一点也不实用。索性自已用ComponentTagSupport来做个封装。
下面是关于ComponentTagSupport的一些介绍:
在struts2.x中实现自定义标签时,继承的2个类分别是org.apache.struts2.views.jsp.ComponentTagSupport 和 org.apache.struts2.components.Component. ComponentTagSupport: 实际上是对BodyTagSupport的一次封装,继承ComponentTagSupport类是为了获得JSP页面中用户自定义的标签中设置的属性值,并包装成Component对象。 Component:
继承Component类是为了从Struts2中的ValueStack中获得相对应的值。 下面给出我的源码,代码结构我就不讲了。需要它的朋友自然看得懂。
package com.jdgm.platform.common.tag; import java.io.Writer; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.ResourceBundle; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.JspException; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.struts2.components.Component; import org.apache.struts2.views.jsp.ComponentTagSupport; import com.jdgm.framework.utils.BeanUtils; import com.jdgm.framework.utils.ResourceUtil; import com.jdgm.framework.utils.Utils; import com.jdgm.platform.ConstantsPF; import com.jdgm.platform.components.log.LogUtil; import com.opensymphony.xwork2.util.LocalizedTextUtil; import com.opensymphony.xwork2.util.ValueStack;
* 相比于从BodyTagSupport扩展,具有以下优势: * 1)国际化的解决:CommonStrutsTag的子类可以享有和struts自带标签一样的国际化解决方案 * 2)值栈:CommonStrutsTag的子类,可以获取valuestack的实例,也可以通过类属性 valueproperty * 来做与action对应属性的双向绑定,即值提交与值填充 具体参见属性valueproperty的说明 * @author zhangpf public abstract class CommonStrutsTag extends ComponentTagSupport { * 网站的URL地址,系统赋值。 protected String ctx; protected String id; protected String templateFile; protected String showdiv; public String getShowdiv() { return showdiv; public void setShowdiv(String showdiv) { this.showdiv = showdiv; * 所有属性列表 protected List String propertys = new ArrayList String propertys.add("ctx"); propertys.add("id"); propertys.add("showdiv"); * 需要在字类中特殊处理的属性集合 protected List String specproperty = new ArrayList String * URL相关的属性 protected List String urlproperty = new ArrayList String * 需要从valuestack中取值的相关的属性 br/ * 比如:控件具有一属性:titlefield,值为"model.title" br/ * 对应的控件源码为: input name="%{titlefield}" / br/ * 这样的话,就完成了与action中属性model.title的单向绑定,即提交表单时 br/ * input的值会自动填充到action中的对应属性。但是却无法做到把action中属性的值 br/ * 反向填充到input控件。若想做到这一点,可修改控件源码为: br/ * input name="%{titlefield}" value="%{title}"/ br/ * 同时,在tag子类中,再定义一属性title,并把添加到valueproperty中, br/ * 然后在init()中,为title赋值为:"model.title",这样就完成了action属性值与input控件的反向绑定 br/ * 即通过action地址打开jsp页面时,若action中model.title不为空,则会把该属性的值自动赋给控件 br/ protected List String valueproperty = new ArrayList String
public Component getBean(ValueStack arg0, HttpServletRequest arg1, HttpServletResponse arg2) { init(); CommonStrutsComponent cmp=new CommonStrutsComponent(arg0); return cmp; @Override protected void populateParams() { super.populateParams(); CommonStrutsComponent cmp=(CommonStrutsComponent)component; cmp.setHtmlContent(toHTML()); * 该方法用于在构建标签前,对标签内的各属性做预处理用的 * 主要方便子类扩展 protected void init(){ final protected String toHTML() { ctx=ConstantsPF.URL_WEBSITE; try { StringBuilder string = new StringBuilder(IOUtils.toString( CommonTag.class.getResourceAsStream(templateFile), "UTF-8")); for (String pro : propertys) { if(specproperty.contains(pro) ||valueproperty.contains(pro) ||urlproperty.contains(pro)) continue; String regex = "%{" + pro + "}"; while (string.indexOf(regex) != -1) { int index = string.indexOf(regex); String provalue = String.valueOf( PropertyUtils.getProperty(this,pro)); if (StringUtils.isNotBlank(provalue)) { string.replace(index, index + regex.length(),provalue); } else { provalue = ""; string.replace(index, index + regex.length(), provalue); dealUrlProperty(string); dealValueProperty(string); dealSpecProperty(string); return string.toString(); } catch (Exception e) { LogUtil.error("生成控件失败", e); return ""; * url 相关的处理 * @param string * @throws Exception protected void dealUrlProperty(StringBuilder string)throws Exception for (String pro : urlproperty) { String regex = "%{" + pro + "}"; while (string.indexOf(regex) != -1) { int index = string.indexOf(regex); String provalue = String.valueOf( PropertyUtils.getProperty(this,pro)); if (StringUtils.isNotBlank(provalue)) { provalue = ctx + provalue; string.replace(index, index + regex.length(),provalue); } else { provalue = ""; string.replace(index, index + regex.length(), provalue); * 需要从valuestack中取值的相关的属性 protected void dealValueProperty(StringBuilder string) throws Exception for (String pro : valueproperty) { String regex = "%{" + pro + "}"; while (string.indexOf(regex) != -1) { int index = string.indexOf(regex); String field = String.valueOf(PropertyUtils.getProperty(this,pro)); if (StringUtils.isNotBlank(field)) { String fieldValue=this.getStack().findString(field); if(fieldValue==null) fieldValue=""; string.replace(index, index + regex.length(),fieldValue); else { string.replace(index, index + regex.length(), ""); * 处理特殊属性,主要用于子类扩展 protected void dealSpecProperty(StringBuilder string) throws Exception{ public String getId() { return id; public void setId(String id) { this.id = id; public String getCtx() { return ctx; public void setCtx(String ctx1) { ctx = ctx1;对于上面的CommonStrutsTag的类使用,下面给了一个简单例子,看了后,你会觉得因为CommonStrutsTag,再写自定义标签的时候,非常简单,秒秒中的事。哈哈
DemoTag.java
package com.jdgm.platform.common.tag; import java.util.Locale; import java.util.ResourceBundle; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.JspException; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang.StringUtils; import org.apache.struts2.components.Component; import org.apache.struts2.views.jsp.ComponentTagSupport; import com.jdgm.framework.utils.BeanUtils; import com.jdgm.framework.utils.ResourceUtil; import com.jdgm.framework.utils.Utils; import com.opensymphony.xwork2.util.LocalizedTextUtil; import com.opensymphony.xwork2.util.ValueStack;
struts2一些概念介绍和标签的使用 struts.xml的模块包含格式 OGNL 对象导航语言 有个超大的好处就是根据对象访问属性,避免混乱。 OGNL标签分为:data tags和 control tags 一、数据标签 property标签 获取值栈中的普通对象 是访问哪些有get和set的属性的方法 name:name: 访问值栈中的普通对象的普通方法 1.
上面写的那个简单标签来控制页面中标签内容的重复次数是固定的,这就是权限“写死了”,这是非常不好的行为,因为其灵活性太差。所以下面就介绍一种使用属性来控制标签内容输出次数的方法。
创建实现了SimpleTag接口的实现类(或者是继承了SimpleTagSupport类的子类) 在类中为相应的属性字段添加setter方法,注意字段名称一定要一致。
自定义简单的struts2的s标签 一:自定标签前需要了解的知识: BodyTagSupport类的方法:编写标签对应的实现类时,需要重载BodyTagSupport类几个方法:doStartTag(), setBodyContent(), doInitBody(), doAfterBody(), doEndTag(); 他们...
狂想盼盼 2009年毕业于南开大学,目前从事互联网行业,主要担任产品经理和技术经理的职位。
相关文章
- 自定义 Django的User Model,扩展 AbstractUser类注意事项
- 自定义MVC的Helper扩展方法
- 磁盘备份设备将如何扩展?
- Chrome恶意扩展程序可监控用户上网行为
- Android体验高扩展艺术般的适配器
- Atom 检测php错误扩展linter-php
- js深入研究之扩展类,克隆对象,混合类(自定义的extend函数,clone函数,与augment函数)
- 【架构实践】微服务架构全解析:Scale Cube 横向扩展,垂直扩展,纵向扩展
- 扩展排序后重新编号
- vue自定义指令clickoutside扩展--多个元素的并集作为inside
- Kotlin also let 内联扩展函数
- 通过自定义ServiceHost实现对WCF的扩展[实例篇]
- WCF后续之旅(2): 如何对Channel Layer进行扩展——创建自定义Channel
- 【ybt金牌导航8-6-3】【luogu P4195】【模板】扩展BSGS
- mybatis ResultHandler vs ResultSetHandler及自定义扩展
- SpringBoot2.x系列教程(三十五)SpringMVC之ViewResolver及扩展
- 2019-5-28-VisualStudio-扩展开发
- 2019-4-10-win10-uwp-自定义标记扩展
- VisualStudio 扩展开发
- 手把手教您扩展虚拟内存
- [ Shell入门教程 ] 通配符与基础正则表达式、扩展正则表达式
- 扩展Microsoft Graph数据结构(开放扩展)
- c#写扩展方法