Taglib 原理和实现:第四章 循环的Tag

博客围绕Taglib原理和实现中循环的Tag展开,以在request里显示People对象中Collection里man的名字为例,阐述这是嵌套Tag问题,涉及三个Tag相互作用。还给出思路,如编写不同功能的Tag,最后展示了完整程序,包括编写类、Tag、tld、jsp等。
Taglib原理和实现之循环的Tag
1、问题:在request里的 People 对象,有个属性叫 men ,men 是一个Collection ,有许多个man 。现在,把 collection里的man的名字都显示出来。

  显然,这是一个嵌套Tag的问题。有三个Tag互相作用:最外层的Tag找到People对象,中间的Tag取得Collection,子Tag负责打印。

  例如:

<diego:withObject value="${people}">
 <diego:withCollection property="men">
  <diego:elementout property="name"/>
 </diego:withCollection>
</diego:withObject>

  思路如下:

  1) 编写WithObjectTag,负责从El表达式中取得对象

  2) 编写WithCollectionTag,负责从对象中取得 Collection ,遍历 Collection ,每遍历一次 Collection ,执行一次body

  3) 编写ElementoutTag ,把 Collection 中每个men对象的 name 打印出来

  2. 完整程序如下:

  在上例的diegoyun.vo包内,编写 People 类

package diegoyun.vo;
import java.util.Collection;
public class People
{
 private Collection men = null;
 public Collection getMen()
 {
  return men;
 }
 public void setMen(Collection men)
 {
  this.men = men;
 }
}

  编写 withObject ,这是从request里取得People对象的最外层Tag

package diegoyun;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
public class WithObjectTag extends BodyTagSupport
{
 private Object value = null;

 public Object getValue()
 {
  return value;
 }
 public void setValue(Object value)throws JspException
 {
  this.value = ExpressionEvaluatorManager.evaluate("value", value.toString(), Object.class, this, pageContext);
 }
 public int doStartTag()
 {
  return EVAL_BODY_INCLUDE;
 }
 public int doEndTag()throws JspException
 {
  return EVAL_PAGE;
 }
}

  编写WithCollectionTag,该Tag负责取得Collection,并遍历执行子Tag

package diegoyun;

import java.util.Collection;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.beanutils.PropertyUtils;

public class WithCollectionTag extends BodyTagSupport {
 private Object element = null;

 private Collection list = null;

 private Iterator iterator = null;

 public Object getElement() {
  return element;
 }

 public void setProperty(String property) throws JspException {
  //取得父Tag对象,并且得到Collection
  WithObjectTag parent = (WithObjectTag) getParent();
  if (parent == null)
   throw new JspException("parent tag is null");
   try {
    Object propertyValue = PropertyUtils.getProperty(parent.getValue(),property);
    this.list = (Collection) propertyValue;
    if (list == null)
     throw new JspException("Collection is null");
   } catch (Exception e) {
    throw new JspException(e);
  }
 }

 public int doStartTag() throws JspException {
  //设置第一个元素,然后执行子Tag
  iterator = list.iterator();
  if (iterator.hasNext())
   element = iterator.next();
   return EVAL_BODY_INCLUDE;
 }

 public int doAfterBody() {
  if (iterator.hasNext()) {
   //如果还存在子元素,设置子元素,并且再次执行子Tag
   //循环由此而来
   //否则不再执行子Tag
   element = iterator.next();
   return EVAL_BODY_AGAIN;
  }
  else
   return EVAL_PAGE;
 }
}

  编写 ElementOutputTag

package diegoyun;
import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.beanutils.PropertyUtils;

public class ElementOutputTag extends TagSupport
{
 private Object propertyValue = null;
 public void setProperty(String property)throws JspException
 {
  WithCollectionTag parent = (WithCollectionTag)getParent();
  if(parent == null)
   throw new JspException("parent tag is null");
  try
  {
   //判断上层tag中是否存在该属性名称,如果存在,取得属性值,否则报错
   propertyValue = PropertyUtils.getProperty(parent.getElement(), property);
  }
  catch (Exception e)
  {
   throw new JspException(e);
  }
 }
 public int doEndTag()throws JspException
 {
  try
  {
   //简单的把值打印到jsp页面
   pageContext.getOut().print(propertyValue);
  }
  catch (IOException e)
  {
   throw new JspException(e);
  }
  return EVAL_PAGE;
 }
}

  编写tld

<!--WithObjectTag-->
<tag>
 <name>withObject</name>
 <tag-class>diegoyun.WithObjectTag</tag-class>
 <body-content>JSP</body-content>
 <attribute>
  <name>value</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
</tag>
<!--WithCollectionTag-->
<tag>
 <name>withCollection</name>
 <tag-class>diegoyun.WithCollectionTag</tag-class>
 <body-content>JSP</body-content>
 <attribute>
  <name>property</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
</tag>
<!--ElementOutputTag-->
<tag>
 <name>elementout</name>
 <tag-class>diegoyun.ElementOutputTag</tag-class>
 <body-content>empty</body-content>
 <attribute>
  <name>property</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
</tag>

  编写jsp

<%@ page language="java" %>
<%@ page import="diegoyun.vo.*"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%>

<html>
<body bgcolor="#FFFFFF">
<%
 Collection c = new ArrayList();

 Man man1 = new Man();
 man1.setName("diego");
 c.add(man1);

 Man man2 = new Man();
 man2.setName("Zidane");
 c.add(man2);

 Man man3 = new Man();
 man3.setName("Rui");
 c.add(man3);

 People p =new People();
 p.setMen(c);
 request.setAttribute("people",p);
%>
Test loop tag:
<br>
<diego:withObject value="${people}">
<diego:withCollection property="men">
<diego:elementout property="name"/>
<br>
</diego:withCollection>
</diego:withObject>
</body>
</html>

  运行,则可以看到: 

Test loop tag:
diego
Zidane
Rui
QObject::connect(player,&QMediaPlayer::positionChanged, this, [=](qint64 position) mutable { //滚动显示三行歌词 static QString lastFilePath; QString filePath = playlist->currentMedia().toString(); QString localFilePath = QUrl(filePath).toLocalFile(); std::wstring wpath = localFilePath.toStdWString(); TagLib::FLAC::File file(wpath.c_str()); if (lastFilePath != localFilePath) { lyricsLoaded = false; // 重置加载标志 lyricMap.clear(); // 清空歌词映射 lyricLabel->clear(); // 清空UI显示 lastFilePath = localFilePath; // 更新路径缓存 } if (!lyricsLoaded) { TagLib::Ogg::XiphComment *comments = file.xiphComment(false); const char* lyricFields[] = {"LYRICS", "UNSYNCEDLYRICS", "LYRIC"}; // 支持多个常见键名 for (const char* field : lyricFields) { if (comments->contains(field)) { TagLib::StringList lyricsList = comments->fieldListMap()[field]; QString lyrics =QString::fromStdString(lyricsList.front().to8Bit(true)); QRegularExpression regex(R"(\[(\d+):(\d+)\.(\d+)\](.*))"); QRegularExpressionMatchIterator it = regex.globalMatch(lyrics); while (it.hasNext()) { QRegularExpressionMatch match = it.next(); qint64 min = match.captured(1).toLong(); qint64 sec = match.captured(2).toLong(); qint64 ms = match.captured(3).toLong(); qint64 totalMs = (min * 60000) + (sec * 1000) + ms * 10; lyricMap[totalMs] = match.captured(4).trimmed(); } lyricsLoaded = true; } } } if (!lyricMap.isEmpty()) { // 查找当前歌词 auto it = lyricMap.upperBound(position); // 第一个大于position的键 if (it != lyricMap.begin()) { --it; } QString prevLine, currentLine, nextLine; // 获取三行歌词 if (it != lyricMap.end()) {currentLine = it.value();} // 当前行 auto prevIt = it; // 上一行 if (prevIt != lyricMap.begin()) { --prevIt; prevLine = prevIt.value(); } auto nextIt = it; // 下一行 if (nextIt != lyricMap.end()) { ++nextIt; if (nextIt != lyricMap.end()) { nextLine = nextIt.value(); } } QString displayText = QString("%1<br><font color='red' size='3'>%2</font><br>%3").arg(prevLine, currentLine, nextLine); lyricLabel->setText(displayText); } })现在这个代码只能够读取flac歌曲的歌词,我现在需要他能够同时兼容主流音频格式如mp3、wav等格式的歌词并显示
最新发布
09-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值