javax.el.ELException: No se puede convertir [luz, ámbar, marrón, oscuro] de la clase de tipo java.util.ArrayList a la clase [Ljava.lang.Cadena;

Estoy practicando los ejemplos en la Cabeza Primero Servlets y JSP libro.

Estoy tratando de crear una simple etiqueta que actúa como html select etiqueta. He creado el tld, tag handler y el resto de los servlets así como la Jsp de acuerdo a los ejemplos que se dan en el libro. Para referencia, por favor consulte la página número 542.

Pero, yo estoy haciendo el de arriba dijo excepción. Cualquier ayuda será muy apreciada.

TLD:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>1.2</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>Forms Taglib</short-name>
    <uri>http://example.com/tags/forms</uri>
    <description>
        An example tab library of replacements for the HTML form tags.
    </description>
    <tag>
        <name>select</name>
        <tag-class>com.jstl.select.SelectTagHandler</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>optionList</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <type>java.util.List</type>
        </attribute>
        <attribute>
            <name>name</name>
            <required>true</required>
        </attribute>
        <attribute>
            <name>size</name>
            <required>true</required>
        </attribute>
    </tag>
</taglib>

ColorServlet – Invoca primero:

public class ColorServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List colorList = new ArrayList();

        colorList.add("light");
        colorList.add("amber");
        colorList.add("brown");
        colorList.add("dark");

        getServletContext().setAttribute("colorList", colorList);
        RequestDispatcher view = request.getRequestDispatcher("select-beer.jsp");
        view.forward(request, response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

La jsp(seleccione la cerveza.jsp);

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="my" uri="http://example.com/tags/forms" %>

<form action="select.do" method="POST">
    <p>Select Beer Characteristics: </p>
    Color:
    <my:select optionList="${applicationScope.colorList}" name="color" size="1"/>
    <br><br>
    <input type="submit" value="Submit"/>
</form>

La TagHandler clase:

public class SelectTagHandler extends SimpleTagSupport {
    private String name;
    private String size;
    private List optionList;

    private static final String ATTR_TEMPLATE = "%s='%s'";
    private static final String OPTION_TEMPLATE = "<option value='1$s'>%1$s</option>";

    public void setName(String name) {
        this.name = name;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public void setOptionList(List optionList) {
        this.optionList = optionList;
    }

    @Override
    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();
        out.print("<select ");
        out.print(String.format(ATTR_TEMPLATE, "name", this.name));
        out.print(String.format(ATTR_TEMPLATE, "size", this.size));
        out.println('>');

        for (Object option : this.optionList) {
            String optionTag = String.format(OPTION_TEMPLATE, option.toString());
            out.println(optionTag);
        }
    }
}

StackTrace

20:41:53,383 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/jee_war_exploded].[jsp]] (http--127.0.0.1-8000-1) Servlet.service() for servlet jsp threw exception: javax.el.ELException: Cannot convert [light, amber, brown, dark] of type class java.util.ArrayList to class [Ljava.lang.String;
    at org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:420) [jbossweb-7.0.13.Final.jar:]
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:191) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jsp.select_002dbeer_jsp._jspx_meth_my_005fselect_005f0(select_002dbeer_jsp.java:96)
    at org.apache.jsp.select_002dbeer_jsp._jspService(select_002dbeer_jsp.java:68)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [jbossweb-7.0.13.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253) [jbossweb-7.0.13.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:840) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:622) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:560) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:488) [jbossweb-7.0.13.Final.jar:]
    at com.jstl.select.ColorServlet.doPost(ColorServlet.java:26) [classes:]
    at com.jstl.select.ColorServlet.doGet(ColorServlet.java:31) [classes:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]

20:41:53,391 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/jee_war_exploded].[ColorServlet]] (http--127.0.0.1-8000-1) Servlet.service() for servlet ColorServlet threw exception: javax.el.ELException: Cannot convert [light, amber, brown, dark] of type class java.util.ArrayList to class [Ljava.lang.String;
    at org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:420) [jbossweb-7.0.13.Final.jar:]
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:191) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jsp.select_002dbeer_jsp._jspx_meth_my_005fselect_005f0(select_002dbeer_jsp.java:96)
    at org.apache.jsp.select_002dbeer_jsp._jspService(select_002dbeer_jsp.java:68)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [jbossweb-7.0.13.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253) [jbossweb-7.0.13.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:840) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:622) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:560) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:488) [jbossweb-7.0.13.Final.jar:]
    at com.jstl.select.ColorServlet.doPost(ColorServlet.java:26) [classes:]
    at com.jstl.select.ColorServlet.doGet(ColorServlet.java:31) [classes:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]
InformationsquelleAutor | 2014-02-28

1 Kommentar

  1. -1

    El problema es que intenta convertir un ArrayList en una matriz de cadenas’ (la JVM utiliza la notación [Ljava.lang.String como el nombre de la clase para una "matriz de cadenas").

    javax.el.ELException: Cannot convert [light, amber, brown, dark] of type class java.util.ArrayList to class [Ljava.lang.String;

    Intentar usar # en lugar de $ al evaluar la declaración. Tipo también puede ser cambiado, debe ser String[] no ArrayList.

    String[] color = {"light", "amber", "brown", "dark"};

    Si usted tiene que utilizar ArrayLists intente esto:

    getServletContext().setAttribute("colorList", colorList.toArray(new String[colorList.size()]));
    • ¿Qué entiende usted por el uso de # en lugar de $? Es para la expresión EL derecho?
    • Quiero que se establece como ArrayList. Por qué sólo String[]?
    • Uso # de expresión EL
    • Lo siento. Yo no se usted.
    • uso #{} y no ${}. Aunque debo confesar que yo no sería capaz de responder a lo que la diferencia sería, para mí son sólo "viejo estilo EL" y "nuevo estilo, EL" y debería funcionar de la misma.
    • Uso #{} porque usted puede evaluar las funciones dentro de estas, sólo se puede obtener valores con ${} las expresiones. Tal vez pruebe a sustituir el código que te di Phoenix? se debe trabajar con el colorList.toArray Función.

Kommentieren Sie den Artikel

Bitte geben Sie Ihren Kommentar ein!
Bitte geben Sie hier Ihren Namen ein