Problema em interpretar atributos dinâmicos em JSP
-
16-09-2019 - |
Pergunta
Eu estou tentando atributos processo dinâmico em JSP, mas estou ficando exibição nada em resposta.
Aqui está o código 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>
O arquivo TLD, que está na pasta 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>
e a classe manipulador de 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;
}
o que é a modificação que eu tenho que fazer para dinâmico exibir atributos de valor?
Agradecemos antecipadamente.
Solução
Há um problema nesta linha:
String attrd = String.format("%s='%s'", tagAttrs.get(attr));
Você especifica dois argumentos de cadeia, mas apenas fornecer um.
Algo como isso deve funcionar melhor:
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);
}
Outras dicas
Você poderia colocar algum código nos blocos catch vazio e ver o que acontece ...
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow