danieldestro 30 de jun. de 2005
SimpleDateFormat formatoData = new SimpleDateFormat("dd/MM/yyyy");
pedido.setDataPedido( formatoData.parse(formDinamico.get("dataPedido")) );
smorigo 30 de jun. de 2005
Daniel eu tentei como vc falo só que está dando uma mensagem…
The method parse(String, ParsePosition) in the type SimpleDateFormat is not applicable for the arguments (Object)
Tentei fazer assim:
pedido.setDataPedido( formatoData.parse((String)formDinamico.get("dataPedido")) );
Só que ao realizar o Submit:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request .
exception
javax . servlet . ServletException : java . util . Date
org . apache . struts . action . RequestProcessor . processException ( RequestProcessor . java : 516 )
org . apache . struts . action . RequestProcessor . processActionPerform ( RequestProcessor . java : 423 )
org . apache . struts . action . RequestProcessor . process ( RequestProcessor . java : 226 )
org . apache . struts . action . ActionServlet . process ( ActionServlet . java : 1164 )
org . apache . struts . action . ActionServlet . doPost ( ActionServlet . java : 415 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 709 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 802 )
root cause
java . lang . ClassCastException : java . util . Date
br . com . fts . exp . action . PedidoAction . salva ( PedidoAction . java : 45 )
sun . reflect . NativeMethodAccessorImpl . invoke0 ( Native Method )
sun . reflect . NativeMethodAccessorImpl . invoke ( Unknown Source )
sun . reflect . DelegatingMethodAccessorImpl . invoke ( Unknown Source )
java . lang . reflect . Method . invoke ( Unknown Source )
org . apache . struts . actions . DispatchAction . dispatchMethod ( DispatchAction . java : 276 )
org . apache . struts . actions . DispatchAction . execute ( DispatchAction . java : 196 )
org . apache . struts . action . RequestProcessor . processActionPerform ( RequestProcessor . java : 421 )
org . apache . struts . action . RequestProcessor . process ( RequestProcessor . java : 226 )
org . apache . struts . action . ActionServlet . process ( ActionServlet . java : 1164 )
org . apache . struts . action . ActionServlet . doPost ( ActionServlet . java : 415 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 709 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 802 )
note The full stack trace of the root cause is available in the Apache Tomcat / 5.5.9 logs .
Apache Tomcat / 5.5.9
Mauricio_Linhares 30 de jun. de 2005
Em vez de fazer get() faça getString():
pedido.setDataPedido( formatoData.parse((String)formDinamico.getString("dataPedido")) );
smorigo 30 de jun. de 2005
Bom pessoal eu modifiquei seguindo oq vcs me passaram mais o erro ainda continua…
Para complementar meu Bean está assim
public class Pedido implements Serializable {
private Long codigo ;
private Date dataPedido ;
public Pedido () {
this . dataPedido = new Date ();
}
// metodos get e set
}
Na minha pagina o formato apresentado da Data é o seguinte “EEE MMM dd HH:mm:ss zzz yyyy” meu Action está assim:
public ActionForward salva ( ActionMapping mapping , ActionForm form ,
HttpServletRequest request , HttpServletResponse response )
throws Exception {
DynaActionForm formDinamico = ( DynaActionForm ) form ;
SimpleDateFormat formatoData = new SimpleDateFormat ( "EEE MMM dd HH:mm:ss zzz yyyy" );
Pedido pedido = new Pedido ();
System . out . println ( "Data: " + formDinamico . get ( "dataPedido" ));
pedido . setCodigo (( Long ) formDinamico . get ( "codigo" ));
pedido . setTotal (( Double ) formDinamico . get ( "total" ));
pedido . setDataPedido ( formatoData . parse (( String ) formDinamico . getString ( "dataPedido" )) );
PedidoDAO . getInstance (). salva ( pedido );
formDinamico . reset ( mapping , request );
return mapping . findForward ( "sucesso" );
}
Ele está me apresentand o seguinte erro:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request .
exception
javax . servlet . ServletException : BeanUtils . populate
org . apache . struts . util . RequestUtils . populate ( RequestUtils . java : 497 )
org . apache . struts . action . RequestProcessor . processPopulate ( RequestProcessor . java : 798 )
org . apache . struts . action . RequestProcessor . process ( RequestProcessor . java : 205 )
org . apache . struts . action . ActionServlet . process ( ActionServlet . java : 1164 )
org . apache . struts . action . ActionServlet . doPost ( ActionServlet . java : 415 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 709 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 802 )
root cause
org . apache . commons . beanutils . ConversionException : Cannot assign value of type ' java . lang . String ' to property ' dataPedido ' of type ' java . util . Date '
org . apache . struts . action . DynaActionForm . set ( DynaActionForm . java : 424 )
org . apache . commons . beanutils . PropertyUtils . setSimpleProperty ( PropertyUtils . java : 1769 )
org . apache . commons . beanutils . PropertyUtils . setNestedProperty ( PropertyUtils . java : 1684 )
org . apache . commons . beanutils . PropertyUtils . setProperty ( PropertyUtils . java : 1713 )
org . apache . commons . beanutils . BeanUtils . setProperty ( BeanUtils . java : 1019 )
org . apache . commons . beanutils . BeanUtils . populate ( BeanUtils . java : 808 )
org . apache . struts . util . RequestUtils . populate ( RequestUtils . java : 495 )
org . apache . struts . action . RequestProcessor . processPopulate ( RequestProcessor . java : 798 )
org . apache . struts . action . RequestProcessor . process ( RequestProcessor . java : 205 )
org . apache . struts . action . ActionServlet . process ( ActionServlet . java : 1164 )
org . apache . struts . action . ActionServlet . doPost ( ActionServlet . java : 415 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 709 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 802 )
note The full stack trace of the root cause is available in the Apache Tomcat / 5.5.9 logs .
Apache Tomcat / 5.5.9
debersom 1 de jul. de 2005
smorigo, boa noite.
Você deve estar colocando no seu form dentro do struts-config seu property como date, por isso quando no action você usa o getString ele te da esse erro.
org . apache . commons . beanutils . ConversionException : Cannot assign value of type ' java . lang . String ' to property ' dataPedido ' of type ' java . util . Date '
Experimenta colocar como string e faz como o Mauricio te disse que deve funcionar.
Ex:
<form-bean name="pedidoForm" dynamic="true" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="dataPedido" type="java.lang.String"/>
<form-property name="codigo" type="java.lang.String"/>
<form-property name="total" type="java.lang.String"/>
</form-bean>
smorigo 1 de jul. de 2005
Deberson deu certo mudando para String lá no DynaActionForm, agora tira uma duvida minha eu estou colocando no Construtor do meu JavaBen para ele pegar a data atual, desta maneira quando o usuario criar um novo pedido a data já vem preenchida…
Mais ele coloca no formato “EEE MMM dd HH:mm:ss zzz yyyy” você tem alguma dica do q posso fazer para ele colocar no formato “dd/mm/yyyy” ?
Com JSTL eu consegui fazer mais pelo q entendi ele so muda o formato na apresentacao quando do um Submit o formato ainda está o “EEE MMM dd HH:mm:ss zzz yyyy”…
debersom 1 de jul. de 2005
Você disse que o formato que recebe do form é EEE MMM dd HH:mm:ss zzz yyyy. O que aparece naquele println que você colocou?
System . out . println ( "Data: " + formDinamico . getString ( "dataPedido" ));
O formatoData.parse(“01/01/2005”) vai te retornar um Date a partir de uma string no formato “dd/MM/yyyy”, se você tiver passando outro formato na string coloque-o no construtor do SimpleDateFormat, assim ele saberá como converter sua string em Date:
SimpleDateFormat formatoData = new SimpleDateFormat ( "EEE MMM dd HH:mm:ss zzz yyyy" );
debersom 1 de jul. de 2005
O seu atributo dataPedido do bean é um Date, então ele realmente guarda a data neste formato, o que vc deve fazer é sempre que preciso mostrar essa data usar as tags jstl para formatar a apresentação ou no seu código o SimpleDateFormat.
Desta forma você mantem o atributo dataPedido completo para persistir na sua base. Se você realmente quiser :? mude o atributo no bean para string e quando precisar use o “parse” para muda-lo para Date.
smorigo 1 de jul. de 2005
Bom dia Deberson tudo bom ?
Então eu estou fazendo isso mesmo no meu JSP estou usando a tag do JSTL da seguinte forma:
< fmt:formatDate value="${ pedido . dataPedido } " type="date"
pattern="dd/MM/yyyy" dateStyle="short" />
Modifiquei meu form
& lt ; form - bean name = "pedidoForm" type = "org.apache.struts.action.DynaActionForm" dynamic = "true" & gt ;
& lt ; form - property name = "codigo" type = "java.lang.Long" /& gt ;
& lt ; form - property name = "dataPedido" type = "java.lang.String" /& gt ;
& lt ; form - property name = "total" type = "java.lang.Double" /& gt ;
& lt ; / form - bean & gt ;
Meu Action está definido da seguinte forma
public ActionForward salva ( ActionMapping mapping , ActionForm form ,
HttpServletRequest request , HttpServletResponse response )
throws Exception {
DynaActionForm formDinamico = ( DynaActionForm ) form ;
SimpleDateFormat formatoData = new SimpleDateFormat ( "EEE MMM dd HH:mm:ss zzz yyy" );
Pedido pedido = new Pedido ();
System . out . println ( "Data: " + formDinamico . get ( "dataPedido" ));
pedido . setCodigo (( Long ) formDinamico . get ( "codigo" ));
pedido . setTotal (( Double ) formDinamico . get ( "total" ));
pedido . setDataPedido ( formatoData . parse (( String ) formDinamico . getString ( "dataPedido" )) );
PedidoDAO . getInstance (). salva ( pedido );
formDinamico . reset ( mapping , request );
return mapping . findForward ( "sucesso" );
}
Quando tento salvar ele me da este erro:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request .
exception
javax . servlet . ServletException : Unparseable date : ""
org . apache . struts . action . RequestProcessor . processException ( RequestProcessor . java : 516 )
org . apache . struts . action . RequestProcessor . processActionPerform ( RequestProcessor . java : 423 )
org . apache . struts . action . RequestProcessor . process ( RequestProcessor . java : 226 )
org . apache . struts . action . ActionServlet . process ( ActionServlet . java : 1164 )
org . apache . struts . action . ActionServlet . doPost ( ActionServlet . java : 415 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 709 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 802 )
root cause
java . text . ParseException : Unparseable date : ""
java . text . DateFormat . parse ( Unknown Source )
br . com . fts . exp . action . PedidoAction . salva ( PedidoAction . java : 49 )
sun . reflect . NativeMethodAccessorImpl . invoke0 ( Native Method )
sun . reflect . NativeMethodAccessorImpl . invoke ( Unknown Source )
sun . reflect . DelegatingMethodAccessorImpl . invoke ( Unknown Source )
java . lang . reflect . Method . invoke ( Unknown Source )
org . apache . struts . actions . DispatchAction . dispatchMethod ( DispatchAction . java : 276 )
org . apache . struts . actions . DispatchAction . execute ( DispatchAction . java : 196 )
org . apache . struts . action . RequestProcessor . processActionPerform ( RequestProcessor . java : 421 )
org . apache . struts . action . RequestProcessor . process ( RequestProcessor . java : 226 )
org . apache . struts . action . ActionServlet . process ( ActionServlet . java : 1164 )
org . apache . struts . action . ActionServlet . doPost ( ActionServlet . java : 415 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 709 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 802 )
note The full stack trace of the root cause is available in the Apache Tomcat / 5.5.9 logs .
Apache Tomcat / 5.5.9
Nossa to totalmente perdido rsss
danieldestro 1 de jul. de 2005
O que este cara imprime no console?
System . out . println ( "Data: " + formDinamico . getString ( "dataPedido" ));
debersom 1 de jul. de 2005
Tudo bom, ainda mais que é sexta. :D
Olha o código abaixo.
Date d1 = new Date ();
System . out . println ( "d1: " + d1 );
SimpleDateFormat sdf = new SimpleDateFormat ( "EEE MMM dd HH:mm:ss zzz yyyy" );
System . out . println ( "d1: " + sdf . format ( d1 ));
Date d2 = sdf . parse ( "Sex Jul 01 11:56:20 GMT-03:00 2005" );
System . out . println ( "d2: " + d2 );
Ele tem como saída:
d1 : Fri Jul 01 12 : 04 : 37 GMT - 03 : 00 2005 // imprime o Date
d1 : Sex Jul 01 12 : 04 : 37 GMT - 03 : 00 2005 // formatado pelo sdf
d2 : Fri Jul 01 11 : 56 : 20 GMT - 03 : 00 2005
Você precisa ver o formato que possui a string que você está recebendo do form. Tenta agora para ver se agora funciona.
smorigo 1 de jul. de 2005
Então pessoal no meu textfield o valor apresentado é o seguinte: Fri Jul 01 13:19:12 BRT 2005
Meu formulário está com o dataPedido como String…
Tentando converter dessa maneira:
SimpleDateFormat formatoData = new SimpleDateFormat ( "EEE MMM dd HH:mm:ss zzz yyyy" );
pedido . setDataPedido ( formatoData . parse (( String ) formDinamico . getString ( "dataPedido" )) );
Dessa maneira o “fdp” continua dando a msg:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request .
exception
javax . servlet . ServletException : Unparseable date : "Fri Jul 01 13:21:07 BRT 2005"
org . apache . struts . action . RequestProcessor . processException ( RequestProcessor . java : 516 )
org . apache . struts . action . RequestProcessor . processActionPerform ( RequestProcessor . java : 423 )
org . apache . struts . action . RequestProcessor . process ( RequestProcessor . java : 226 )
org . apache . struts . action . ActionServlet . process ( ActionServlet . java : 1164 )
org . apache . struts . action . ActionServlet . doPost ( ActionServlet . java : 415 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 709 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 802 )
root cause
java . text . ParseException : Unparseable date : "Fri Jul 01 13:21:07 BRT 2005"
java . text . DateFormat . parse ( Unknown Source )
br . com . fts . exp . action . PedidoAction . salva ( PedidoAction . java : 49 )
sun . reflect . NativeMethodAccessorImpl . invoke0 ( Native Method )
sun . reflect . NativeMethodAccessorImpl . invoke ( Unknown Source )
sun . reflect . DelegatingMethodAccessorImpl . invoke ( Unknown Source )
java . lang . reflect . Method . invoke ( Unknown Source )
org . apache . struts . actions . DispatchAction . dispatchMethod ( DispatchAction . java : 276 )
org . apache . struts . actions . DispatchAction . execute ( DispatchAction . java : 196 )
org . apache . struts . action . RequestProcessor . processActionPerform ( RequestProcessor . java : 421 )
org . apache . struts . action . RequestProcessor . process ( RequestProcessor . java : 226 )
org . apache . struts . action . ActionServlet . process ( ActionServlet . java : 1164 )
org . apache . struts . action . ActionServlet . doPost ( ActionServlet . java : 415 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 709 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 802 )
note The full stack trace of the root cause is available in the Apache Tomcat / 5.5.9 logs .
--------------------------------------------------------------------------------
Apache Tomcat / 5.5.9
O formato no SimpleDateFormat está correto…
smorigo 1 de jul. de 2005
Daniel o System.out.println("Data: "+formDinamico.getString(“dataPedido”)); está imprimindo assim:
Data: Fri Jul 01 13:21:07 BRT 2005
danieldestro 1 de jul. de 2005
Não deveria imprimir “01/07/2005” ???
smorigo 1 de jul. de 2005
Acho que não pq eu to fazendo o seguinte…
Meu Bean
public class Pedido implements Serializable {
private Long codigo ;
private Date dataPedido ;
private Double total ;
private Set pedidoItens = new HashSet ();
public Pedido () {
this . dataPedido = new Date ();
}
// get e set..
Meu Form
& lt ; form - bean name = "pedidoForm" type = "org.apache.struts.action.DynaActionForm" dynamic = "true" & gt ;
& lt ; form - property name = "codigo" type = "java.lang.Long" /& gt ;
& lt ; form - property name = "dataPedido" type = "java.lang.String" /& gt ;
& lt ; form - property name = "total" type = "java.lang.Double" /& gt ;
& lt ; / form - bean & gt ;
Meu Action
public ActionForward salva ( ActionMapping mapping , ActionForm form ,
HttpServletRequest request , HttpServletResponse response )
throws Exception {
DynaActionForm formDinamico = ( DynaActionForm ) form ;
SimpleDateFormat formatoData = new SimpleDateFormat ( "EEE MMM dd HH:mm:ss zzz yyyy" );
Pedido pedido = new Pedido ();
System . out . println ( "Data: " + formDinamico . getString ( "dataPedido" ));
pedido . setCodigo (( Long ) formDinamico . get ( "codigo" ));
pedido . setTotal (( Double ) formDinamico . get ( "total" ));
pedido . setDataPedido ( formatoData . parse (( String ) formDinamico . getString ( "dataPedido" )) );
PedidoDAO . getInstance (). salva ( pedido );
formDinamico . reset ( mapping , request );
return mapping . findForward ( "sucesso" );
}
Na minha pagina JSP o valor que está sendo apresentado no textfield é o seguinte “Fri Jul 01 13:37:03 BRT 2005” ao meu ver isso é o formato “EEE MMM dd HH:mm:ss zzz yyyy” mais na hora que vai transformar…
javax.servlet.ServletException: Unparseable date: "Fri Jul 01 13:38:10 BRT 2005"
danieldestro 1 de jul. de 2005
Tá, mas você tem um input no seu form HTML ou oq?
smorigo 1 de jul. de 2005
Tenho um sim
<html:text property=“dataPedido” name=“pedido” size=“30” readonly =“true”/>
Tentei usar JSTL mais dai quando do o Submit ele nao consegue recuperar o valor… o System.out.println retorna “”
<fmt:formatDate value="${pedido.dataPedido}" type=“date” pattern=“dd/MM/yyyy” dateStyle=“short” />
danieldestro 1 de jul. de 2005
<html:text property=“dataPedido” name=“pedido” size=“30” readonly =“true” format=“dd/MM/yyyy”/>
E voi lá!
smorigo 1 de jul. de 2005
Daniel usando da maneira que voce coloco a pagina não é nem exibida
<html:text property=“dataPedido” name=“pedido” size=“30” readonly =“true” format=“dd/MM/yyyy”/>
Testei da seguinte maneira:
<bean:write name=“pedido” property=“dataPedido” format=“dd/MM/yyyy”/>
Ele exibiu igual o JSTL da maneira que eu quero mais dai quando do um Submit parece q o valor não retorna…
danieldestro 1 de jul. de 2005
Com a tag <bean:write /> você apenas grava os dados na saída, não tem nada a ver com form html.
Verifique se sua versão do Struts suporta o atributo format.
smorigo 1 de jul. de 2005
eu estou usando a ultima e estive olhando lá na documentacao nao encontrei a propriedade format no <html:text>
http://struts.apache.org/userGuide/struts-html.html#text
danieldestro 1 de jul. de 2005
Vai ver é só no <bean:write/> mesmo.
smorigo 1 de jul. de 2005
Sabe eu acho que to “comendo bola” em alguma coisa com o formato pq… se eu deixo o textfield aberto para eu ditgitar e digito no formato dd/MM/yyyy e mudo lá no meu Action o SimpleDateFormat para dd/MM/yyyy ele funciona perfeitamente…
danieldestro 1 de jul. de 2005
Claro que sim… pq o seu problema é com o formato da data que está sendo enviado.
smorigo 1 de jul. de 2005
Fri Jul 01 14:17:37 BRT 2005 = EEE MMM dd HH:mm:ss zzz yyyy não é ?
smorigo 1 de jul. de 2005
Tentei fazer esse teste:
SimpleDateFormat formatoData = new SimpleDateFormat(“EEE MMM dd HH:mm:ss zzz yyyy”);
pedido.setDataPedido( formatoData.parse(“Tue Nov 04 21:53:43 EST 2003”) );
ele nao conseguiu converter… ai ai ai
smorigo 1 de jul. de 2005
Bem pessoal resolvi o problema assim:
& lt ; input type = "text" name = "dataPedido" class = "textfield_desabilitado" value = "<bean:write name=" pedido " property=" dataPedido " format=" dd / MM / yyyy "/>" size = "15" maxlength = "3" / >