到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.comwww.javaresearch.org无关,不对该信息负责.通过http://www.javaresearch.org/article//showarticle.jsp?column=23&thread=56084访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
Struts标签库中〈bean:write 截取标题的长度的最佳方法
作者:liutianjin 来源:www.javaresearch.org  发布时间:2006-09-25 01:46:42.14


在strtus下显示完整标题的一种方法 
我觉得在读数据的时候截取标题的长度有一些不妥当,我刚才试图写了一个很简单的自定义标签,在struts中有这样一个标签<bean:write name="article" property="title"/>上面标签的意思是读取article对象中的title属性的值,现在对title的长度要求限制在一定范围之内,定义此标签的类是org.apache.struts.taglib.bean.WriteTag如果对这个类扩展,加一个属性cut ,再根据cut的大小来截取标题的长度,我对WriteTag继承,重写doStartTag(), package org.apache.struts.taglib.bean; //注意,要这样写,不然会出错的 
StringTag extends WriteTag{....
public String setValue(String value) {
    String tempProperty=value;
    if(cut>0){
        if(tempProperty.length()>=(cut+1)){
            tempProperty=tempProperty.substring(0, cut) +"...";        }
    }
    return tempProperty;
}
public int doStartTag() throws JspException {    // Look up the requested bean (if necessary)
    if (ignore) {
        if (TagUtils.getInstance().lookup(pageContext, name, scope) == null) {
            return (SKIP_BODY); // Nothing to output
        }
    }    // Look up the requested property value
    Object value = TagUtils.getInstance().lookup(pageContext, name, property, scope);    if (value == null) {
        return (SKIP_BODY); // Nothing to output
    }    // Convert value to the String with some formatting
    String output = formatValue(value);
    output=setValue(output);  //这句是加的,别的都是原来的
    // Print this property value to our output writer, suitably filtered
    if (filter) {
        TagUtils.getInstance().write(pageContext, TagUtils.getInstance().filter(output));
    } else {
        TagUtils.getInstance().write(pageContext, output);
    }    // Continue processing this page
    return (SKIP_BODY);}对标签重写完毕,然后写一个dtd文件 <tag>
<name>write</name>
<tagclass>org.apache.struts.taglib.bean.StringTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>bundle</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>filter</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>format</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>formatKey</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>ignore</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>locale</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>property</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cut</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>写完了之后,我们就可以在jsp页面中调用它了,首先引用定义标签 <%@ taglib uri="/WEB-INF/titlecut.tld" prefix="title" %>然后对链接代码进行重写,我写的是下面一段 <html:link action="/admin/view" paramName="article" 
paramProperty="id" paramId="id" 
title="<bean:write name='article' property='title'/>">
<title:write name="article" property="title" cut="18"/>
</html:link>用以上方法当鼠标放到标题上时回显示完整的标题,在页面中显示部分标题 

 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读