Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Erro no tipoPreco ao usar o form:input

Estou com um problema ao trocar o código no form.jsp. Quando uso:

<input type="hidden" name="precos[${status.index}].tipo" value="${tipoPreco}" />

tudo funciona normalmente. Ao alterar somente essa linha pelo sugerido no exercício:

<form:input type="hidden"path="precos[${status.index}].tipo" value="${tipoPreco}" />

a página para de ser exibida e retorna um erro 500:

Type Exception Report

Message An exception occurred processing [/WEB-INF/views/produtos/form.jsp] at line [46]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing [/WEB-INF/views/produtos/form.jsp] at line [46]

43:             <div>
44:                 <label>${tipoPreco}</label> 
45:                 <form:input path="precos[${status.index}].valor" />
46:                 <form:input type="hidden" path="precos[${status.index}].tipo" value="${tipoPreco}" />
47:             </div>
48:         </c:forEach>
49:         <button type="submit">Incluir</button>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:593)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:482)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

org.springframework.beans.NotReadablePropertyException: Invalid property 'precos[0].tipo' of bean class [br.com.alura.loja.models.Produto]: Bean property 'precos[0].tipo' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:631)
    org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622)
    org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)
    org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:229)
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
    org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
    org.springframework.web.servlet.tags.form.InputTag.writeTagContent(Inpu
3 respostas

José,

O atributo tipo possui getter e setter? Estão retornando o mesmo tipo declarado para o atributo tipo?

Sim.

@Embeddable
public class Preco {

    private BigDecimal valor;
    private TipoPreco tipo;

    public BigDecimal getValor() {
        return valor;
    }

    public void setValor(BigDecimal valor) {
        this.valor = valor;
    }

    public TipoPreco getTipoPreco() {
        return tipo;
    }

    public void setTipoPreco(TipoPreco tipoPreco) {
        this.tipo = tipoPreco;
    }

}
solução!

José,

Na linha:

<form:input type="hidden"path="precos[${status.index}].tipo" value="${tipoPreco}" />

Substitui por:

<form:input type="hidden"path="precos[${status.index}].tipoPreco" value="${tipoPreco}" />

Se não der certo, cola aqui a tua classe que tem essa Preco.java embutida.