在Jsp中解释动态属性时出现问题 [英] Problem in interpreting dynamic attributes in Jsp

查看:142
本文介绍了在Jsp中解释动态属性时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to process dynamic attributes in Jsp, but I'm getting display nothing in response.

这是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文件夹中:

The TLD file, which is in WEB-INF folder:

<?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;
}

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

what's modification do I've to do to display dynamic attributes value?

谢谢.

推荐答案

此行有问题:

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

您指定两个字符串参数,但仅提供一个.

You specify two string arguments, but only provide one.

类似这样的方法应该可以更好地工作:

Something like this should work better:

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);
}

这篇关于在Jsp中解释动态属性时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆