Jsp中解释动态属性的问题

发布于 2024-07-26 19:45:38 字数 2231 浏览 8 评论 0原文

我正在尝试在 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;
}

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

提前致谢。

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

Here's the JSP code:

<%@ 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>

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>

and the tag handler class:

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?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

败给现实 2024-08-02 19:45:38

这一行有一个问题:

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

There is a problem in this line:

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);
}
暗恋未遂 2024-08-02 19:45:38

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

You could put some code in the empty catch blocks and see what happens...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文