【问题标题】:Problem in interpreting dynamic attributes in Jsp在Jsp中解释动态属性的问题
【发布时间】:2010-11-11 16:19:11
【问题描述】:

我正在尝试在 Jsp 中处理动态属性,但我没有得到任何响应。

JSP 代码如下:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="mine" uri="DiceFunctions" %>


<html><body>

<mine:advice  suggest="yo haa haa" >

</mine:advice>
</body></html>

TLD 文件,位于 WEB-INF 文件夹中:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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_0.xsd" version="2.0">

<tlib-version>1.2</tlib-version>
<jsp-version>2.0</jsp-version>
<uri>DiceFunctions</uri>

<tag>

<name>advice</name>
<tag-class>foo.AdvisorTagHandler</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>optionList</name>
<type>java.util.List</type>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
</attribute>
<attribute>
<name>size</name>
<required>false</required>
</attribute>
<dynamic-attributes>true</dynamic-attributes>

</tag>

和标签处理类:

package foo;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import javax.servlet.jsp.*;
import java.util.*;

public class AdvisorTagHandler extends TagSupport implements DynamicAttributes {



private Map<String,Object> tagAttrs=new HashMap<String,Object>();

public int doStartTag() throws JspException{
//movieCounter=0;
try{


for(String attr: tagAttrs.keySet())
{
String attrd=String.format("%s='%s'",tagAttrs.get(attr));

pageContext.getOut().print(attrd);
}

}
catch(Exception e)
{
}

return SKIP_BODY;
}

public void setDynamicAttribute(String uri, String name, Object value){

tagAttrs.put(name,value);
}
public int doEndTag() throws JspException{
return EVAL_PAGE;
}

我需要做哪些修改才能显示动态属性值?

提前致谢。

【问题讨论】:

    标签: java jsp custom-tags


    【解决方案1】:

    这行有问题:

    String attrd = String.format("%s='%s'", tagAttrs.get(attr));
    

    您指定了两个字符串参数,但只提供了一个。

    这样的事情应该会更好:

    try {
      for (Map.Entry<String, Object> attr : tagAttrs.entrySet()) {
        String attrd = String.format("%s='%s'", attr.getKey(), attr
            .getValue().toString());
        pageContext.getOut().print(attrd);
      }
    } catch (IOException e) {
      throw new JspException(e);
    }
    

    【讨论】:

      【解决方案2】:

      你可以在空的 catch 块中放一些代码,看看会发生什么......

      【讨论】:

      • 我在 catch 块中尝试了 e.getMessage() 或 e.printStackTrace() 方法,但仍然没有得到任何响应。
      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      相关资源
      最近更新 更多