guilhermebhte 19 de mar. de 2017
Daniel_Dias 19 de mar. de 2017 1 like
Isso aconteceu comigo alguns dias atrás. você precisa fazer um converter para pode funcionar.
Como não sei se esta usando por configuração java ou XML vou posta os dois ai você modifica para o seu.
package br.com.converter ;
import org.springframework.core.convert.converter.Converter ;
import br.com.prova.mobicare.model.Departamento ;
final class StringToDepartamento implements Converter < String , Departamento > {
public Departamento convert ( String id ) {
Departamento departamento = new Departamento ();
departamento . setId ( Integer . valueOf ( id ));
return departamento ;
}
}
XML
no seu XML voce vai colocar isso :
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id= "conversionService" class= "org.springframework.context.support.ConversionServiceFactoryBean" >
<property name= "converters" >
<set>
<bean class= "br.com.converter.StringToDepartamento" />
</set>
</property>
</bean>
por configuração Java, dentro da sua classe que extend WebMvcConfigurerAdapter
@Override
public void addFormatters ( FormatterRegistry registry ) {
registry . addConverter ( getMyConverter ());
}
@Bean
public StringToDepartamento getMyConverter () {
return new StringToDepartamento ();
}
{ },s.
guilhermebhte 19 de mar. de 2017
Obrigado, Acho que é por xml, pois não foi eu quem configurei.
E não achei nada estendendo de WebMvcConfigurerAdapter
Daniel_Dias 19 de mar. de 2017
adicione isso no topo do seu xml :
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi : schemaLocation = xsi : schemaLocation = http :// www . springframework . org /schema/ mvc
http :// www . springframework . org /schema/mvc/s pring - mvc . xsd
guilhermebhte 20 de mar. de 2017
Arquivo root-context.xml
< beans xmlns : mvc = "http://www.springframework.org/schema/mvc"
xmlns = "http://www.springframework.org/schema/beans" xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns : tx = "http://www.springframework.org/schema/tx" xmlns : context = "http://www.springframework.org/schema/context"
xsi : schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
xsi:schemaLocation=http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
< context : annotation - config />
< bean
class =" org . springframework . orm . jpa . support . PersistenceAnnotationBeanPostProcessor " />
<bean id=" entityManagerFactory "
class=" org . springframework . orm . jpa . LocalContainerEntityManagerFactoryBean ">
<property name=" dataSource " ref=" dataSource " />
<property name=" jpaVendorAdapter ">
<bean class=" org . springframework . orm . jpa . vendor . HibernateJpaVendorAdapter ">
<property name=" showSql " value=" true " />
</bean>
</property>
<property name=" jpaPropertyMap ">
<map>
<entry key=" hibernate . dialect " value=" org . hibernate . dialect . PostgreSQLDialect " />
<entry key=" hibernate . hbm2ddl . auto " value=" none " />
<entry key=" hibernate . format_sql " value=" true " />
</map>
</property>
</bean>
<bean id=" transactionManager " class=" org . springframework . orm . jpa . JpaTransactionManager ">
<property name=" entityManagerFactory " ref=" entityManagerFactory " />
</bean>
<tx:annotation-driven transaction-manager=" transactionManager " />
<bean id=" dataSource "
class=" org . springframework . jdbc . datasource . DriverManagerDataSource ">
<property name=" driverClassName " value=" org . postgresql . Driver " />
<property name=" url " value=" jdbc : postgresql : //localhost:5432/erp_utf8" />
< property name = "username" value = "postgres" />
< property name = "password" value = "eeee" />
</ bean >
< bean id = "txManager" class =" org . springframework . orm . jpa . JpaTransactionManager ">
<property name=" entityManagerFactory " ref=" entityManagerFactory " />
</bean>
<mvc:annotation-driven conversion-service=" conversionService " />
<bean id=" conversionService "
class=" org . springframework . context . support . ConversionServiceFactoryBean ">
<property name=" converters ">
<set>
<bean class=" br . com . netsoft . desif . converter . StringCosif " />
</set>
</property>
</bean>
</beans>
Este erro agora
ERROR : org . springframework . web . context . ContextLoader - Context initialization failed
org . springframework . beans . factory . xml . XmlBeanDefinitionStoreException : Line 52 in XML document from ServletContext resource [ / WEB - INF / spring / root - context . xml ] is invalid ; nested exception is org . xml . sax . SAXParseException ; lineNumber : 52 ; columnNumber : 66 ; cvc - complex - type . 2.4 . c : O curinga correspondente é restrito , mas nenhuma declaração pode ser encontrada para o elemento ‘ mvc : annotation - driven ’ .
at org . springframework . beans . factory . xml . XmlBeanDefinitionReader . doLoadBeanDefinitions ( XmlBeanDefinitionReader . java : 398 )
at org . springframework . beans . factory . xml . XmlBeanDefinitionReader . loadBeanDefinitions ( XmlBeanDefinitionReader . java : 335 )
at org . springframework . beans . factory . xml . XmlBeanDefinitionReader . loadBeanDefinitions ( XmlBeanDefinitionReader . java : 303 )
at org . springframework . beans . factory . support . AbstractBeanDefinitionReader . loadBeanDefinitions ( AbstractBeanDefinitionReader . java : 180 )
at org . springframework . beans . factory . support . AbstractBeanDefinitionReader . loadBeanDefinitions ( AbstractBeanDefinitionReader . java : 216 )
at org . springframework . beans . factory . support . AbstractBeanDefinitionReader . loadBeanDefinitions ( AbstractBeanDefinitionReader . java : 187 )
at org . springframework . web . context . support . XmlWebApplicationContext . loadBeanDefinitions ( XmlWebApplicationContext . java : 125 )
at org . springframework . web . context . support . XmlWebApplicationContext . loadBeanDefinitions ( XmlWebApplicationContext . java : 94 )
at org . springframework . context . support . AbstractRefreshableApplicationContext . refreshBeanFactory ( AbstractRefreshableApplicationContext . java : 129 )
at org . springframework . context . support . AbstractApplicationContext . obtainFreshBeanFactory ( AbstractApplicationContext . java : 537 )
at org . springframework . context . support . AbstractApplicationContext . refresh ( AbstractApplicationContext . java : 452 )
at org . springframework . web . context . ContextLoader . configureAndRefreshWebApplicationContext ( ContextLoader . java : 403 )
at org . springframework . web . context . ContextLoader . initWebApplicationContext ( ContextLoader . java : 306 )
at org . springframework . web . context . ContextLoaderListener . contextInitialized ( ContextLoaderListener . java : 106 )
at org . apache . catalina . core . StandardContext . listenerStart ( StandardContext . java : 4939 )
at org . apache . catalina . core . StandardContext . startInternal ( StandardContext . java : 5434 )
at org . apache . catalina . util . LifecycleBase . start ( LifecycleBase . java : 150 )
at org . apache . catalina . core . ContainerBase $ StartChild . call ( ContainerBase . java : 1559 )
at org . apache . catalina . core . ContainerBase $ StartChild . call ( ContainerBase . java : 1549 )
at java . util . concurrent . FutureTask . run ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor . runWorker ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor $ Worker . run ( Unknown Source )
at java . lang . Thread . run ( Unknown Source )
Caused by : org . xml . sax . SAXParseException ; lineNumber : 52 ; columnNumber : 66 ; cvc - complex - type . 2.4 . c : O curinga correspondente é restrito , mas nenhuma declaração pode ser encontrada para o elemento ‘ mvc : annotation - driven ’ .
at com . sun . org . apache . xerces . internal . util . ErrorHandlerWrapper . createSAXParseException ( Unknown Source )
at com . sun . org . apache . xerces . internal . util . ErrorHandlerWrapper . error ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLErrorReporter . reportError ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLErrorReporter . reportError ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLErrorReporter . reportError ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . xs . XMLSchemaValidator $ XSIErrorReporter . reportError ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . xs . XMLSchemaValidator . reportSchemaError ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . xs . XMLSchemaValidator . handleStartElement ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . xs . XMLSchemaValidator . emptyElement ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLNSDocumentScannerImpl . scanStartElement ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLDocumentFragmentScannerImpl $ FragmentContentDriver . next ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLDocumentScannerImpl . next ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLNSDocumentScannerImpl . next ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLDocumentFragmentScannerImpl . scanDocument ( Unknown Source )
at com . sun . org . apache . xerces . internal . parsers . XML11Configuration . parse ( Unknown Source )
at com . sun . org . apache . xerces . internal . parsers . XML11Configuration . parse ( Unknown Source )
at com . sun . org . apache . xerces . internal . parsers . XMLParser . parse ( Unknown Source )
at com . sun . org . apache . xerces . internal . parsers . DOMParser . parse ( Unknown Source )
at com . sun . org . apache . xerces . internal . jaxp . DocumentBuilderImpl . parse ( Unknown Source )
at org . springframework . beans . factory . xml . DefaultDocumentLoader . loadDocument ( DefaultDocumentLoader . java : 76 )
at org . springframework . beans . factory . xml . XmlBeanDefinitionReader . doLoadDocument ( XmlBeanDefinitionReader . java : 428 )
at org . springframework . beans . factory . xml . XmlBeanDefinitionReader . doLoadBeanDefinitions ( XmlBeanDefinitionReader . java : 390 )
… 22 more
mar 19 , 2017 9 : 00 : 30 PM org . apache . catalina . core . StandardContext listenerStart
GRAVE : Exception sending context initialized event to listener instance of class org . springframework . web . context . ContextLoaderListener
org . springframework . beans . factory . xml . XmlBeanDefinitionStoreException : Line 52 in XML document from ServletContext resource [ / WEB - INF / spring / root - context . xml ] is invalid ; nested exception is org . xml . sax . SAXParseException ; lineNumber : 52 ; columnNumber : 66 ; cvc - complex - type . 2.4 . c : O curinga correspondente é restrito , mas nenhuma declaração pode ser encontrada para o elemento ‘ mvc : annotation - driven ’ .
at org . springframework . beans . factory . xml . XmlBeanDefinitionReader . doLoadBeanDefinitions ( XmlBeanDefinitionReader . java : 398 )
at org . springframework . beans . factory . xml . XmlBeanDefinitionReader . loadBeanDefinitions ( XmlBeanDefinitionReader . java : 335 )
at org . springframework . beans . factory . xml . XmlBeanDefinitionReader . loadBeanDefinitions ( XmlBeanDefinitionReader . java : 303 )
at org . springframework . beans . factory . support . AbstractBeanDefinitionReader . loadBeanDefinitions ( AbstractBeanDefinitionReader . java : 180 )
at org . springframework . beans . factory . support . AbstractBeanDefinitionReader . loadBeanDefinitions ( AbstractBeanDefinitionReader . java : 216 )
at org . springframework . beans . factory . support . AbstractBeanDefinitionReader . loadBeanDefinitions ( AbstractBeanDefinitionReader . java : 187 )
at org . springframework . web . context . support . XmlWebApplicationContext . loadBeanDefinitions ( XmlWebApplicationContext . java : 125 )
at org . springframework . web . context . support . XmlWebApplicationContext . loadBeanDefinitions ( XmlWebApplicationContext . java : 94 )
at org . springframework . context . support . AbstractRefreshableApplicationContext . refreshBeanFactory ( AbstractRefreshableApplicationContext . java : 129 )
at org . springframework . context . support . AbstractApplicationContext . obtainFreshBeanFactory ( AbstractApplicationContext . java : 537 )
at org . springframework . context . support . AbstractApplicationContext . refresh ( AbstractApplicationContext . java : 452 )
at org . springframework . web . context . ContextLoader . configureAndRefreshWebApplicationContext ( ContextLoader . java : 403 )
at org . springframework . web . context . ContextLoader . initWebApplicationContext ( ContextLoader . java : 306 )
at org . springframework . web . context . ContextLoaderListener . contextInitialized ( ContextLoaderListener . java : 106 )
at org . apache . catalina . core . StandardContext . listenerStart ( StandardContext . java : 4939 )
at org . apache . catalina . core . StandardContext . startInternal ( StandardContext . java : 5434 )
at org . apache . catalina . util . LifecycleBase . start ( LifecycleBase . java : 150 )
at org . apache . catalina . core . ContainerBase $ StartChild . call ( ContainerBase . java : 1559 )
at org . apache . catalina . core . ContainerBase $ StartChild . call ( ContainerBase . java : 1549 )
at java . util . concurrent . FutureTask . run ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor . runWorker ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor $ Worker . run ( Unknown Source )
at java . lang . Thread . run ( Unknown Source )
Caused by : org . xml . sax . SAXParseException ; lineNumber : 52 ; columnNumber : 66 ; cvc - complex - type . 2.4 . c : O curinga correspondente é restrito , mas nenhuma declaração pode ser encontrada para o elemento ‘ mvc : annotation - driven ’ .
at com . sun . org . apache . xerces . internal . util . ErrorHandlerWrapper . createSAXParseException ( Unknown Source )
at com . sun . org . apache . xerces . internal . util . ErrorHandlerWrapper . error ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLErrorReporter . reportError ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLErrorReporter . reportError ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLErrorReporter . reportError ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . xs . XMLSchemaValidator $ XSIErrorReporter . reportError ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . xs . XMLSchemaValidator . reportSchemaError ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . xs . XMLSchemaValidator . handleStartElement ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . xs . XMLSchemaValidator . emptyElement ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLNSDocumentScannerImpl . scanStartElement ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLDocumentFragmentScannerImpl $ FragmentContentDriver . next ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLDocumentScannerImpl . next ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLNSDocumentScannerImpl . next ( Unknown Source )
at com . sun . org . apache . xerces . internal . impl . XMLDocumentFragmentScannerImpl . scanDocument ( Unknown Source )
at com . sun . org . apache . xerces . internal . parsers . XML11Configuration . parse ( Unknown Source )
at com . sun . org . apache . xerces . internal . parsers . XML11Configuration . parse ( Unknown Source )
at com . sun . org . apache . xerces . internal . parsers . XMLParser . parse ( Unknown Source )
at com . sun . org . apache . xerces . internal . parsers . DOMParser . parse ( Unknown Source )
at com . sun . org . apache . xerces . internal . jaxp . DocumentBuilderImpl . parse ( Unknown Source )
at org . springframework . beans . factory . xml . DefaultDocumentLoader . loadDocument ( DefaultDocumentLoader . java : 76 )
at org . springframework . beans . factory . xml . XmlBeanDefinitionReader . doLoadDocument ( XmlBeanDefinitionReader . java : 428 )
at org . springframework . beans . factory . xml . XmlBeanDefinitionReader . doLoadBeanDefinitions ( XmlBeanDefinitionReader . java : 390 )
… 22 more
mar 19 , 2017 9 : 00 : 30 PM org . apache . catalina . core . StandardContext startInternal
GRAVE : Error listenerStart
mar 19 , 2017 9 : 00 : 30 PM org . apache . catalina . core . StandardContext startInternal
GRAVE : Context [ / desif ] startup failed due to previous errors
mar 19 , 2017 9 : 00 : 30 PM org . apache . catalina . core . ApplicationContext log
INFORMAÇÕES : Closing Spring root WebApplicationContext
INFO : org . springframework . web . context . support . XmlWebApplicationContext - Closing Root WebApplicationContext : startup date [ Sun Mar 19 21 : 00 : 30 BRT 2017 ] ; root of context hierarchy
WARN : org . springframework . web . context . support . XmlWebApplicationContext - Exception thrown from ApplicationListener handling ContextClosedEvent
java . lang . IllegalStateException : ApplicationEventMulticaster not initialized - call ‘ refresh ’ before multicasting events via the context : Root WebApplicationContext : startup date [ Sun Mar 19 21 : 00 : 30 BRT 2017 ] ; root of context hierarchy
at org . springframework . context . support . AbstractApplicationContext . getApplicationEventMulticaster ( AbstractApplicationContext . java : 344 )
at org . springframework . context . support . AbstractApplicationContext . publishEvent ( AbstractApplicationContext . java : 331 )
at org . springframework . context . support . AbstractApplicationContext . doClose ( AbstractApplicationContext . java : 869 )
at org . springframework . context . support . AbstractApplicationContext . close ( AbstractApplicationContext . java : 836 )
at org . springframework . web . context . ContextLoader . closeWebApplicationContext ( ContextLoader . java : 579 )
at org . springframework . web . context . ContextLoaderListener . contextDestroyed ( ContextLoaderListener . java : 115 )
at org . apache . catalina . core . StandardContext . listenerStop ( StandardContext . java : 4980 )
at org . apache . catalina . core . StandardContext . stopInternal ( StandardContext . java : 5626 )
at org . apache . catalina . util . LifecycleBase . stop ( LifecycleBase . java : 232 )
at org . apache . catalina . util . LifecycleBase . start ( LifecycleBase . java : 160 )
at org . apache . catalina . core . ContainerBase $ StartChild . call ( ContainerBase . java : 1559 )
at org . apache . catalina . core . ContainerBase $ StartChild . call ( ContainerBase . java : 1549 )
at java . util . concurrent . FutureTask . run ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor . runWorker ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor $ Worker . run ( Unknown Source )
at java . lang . Thread . run ( Unknown Source )
WARN : org . springframework . web . context . support . XmlWebApplicationContext - Exception thrown from LifecycleProcessor on context close
java . lang . IllegalStateException : LifecycleProcessor not initialized - call ‘ refresh ’ before invoking lifecycle methods via the context : Root WebApplicationContext : startup date [ Sun Mar 19 21 : 00 : 30 BRT 2017 ] ; root of context hierarchy
at org . springframework . context . support . AbstractApplicationContext . getLifecycleProcessor ( AbstractApplicationContext . java : 357 )
at org . springframework . context . support . AbstractApplicationContext . doClose ( AbstractApplicationContext . java : 877 )
at org . springframework . context . support . AbstractApplicationContext . close ( AbstractApplicationContext . java : 836 )
at org . springframework . web . context . ContextLoader . closeWebApplicationContext ( ContextLoader . java : 579 )
at org . springframework . web . context . ContextLoaderListener . contextDestroyed ( ContextLoaderListener . java : 115 )
at org . apache . catalina . core . StandardContext . listenerStop ( StandardContext . java : 4980 )
at org . apache . catalina . core . StandardContext . stopInternal ( StandardContext . java : 5626 )
at org . apache . catalina . util . LifecycleBase . stop ( LifecycleBase . java : 232 )
at org . apache . catalina . util . LifecycleBase . start ( LifecycleBase . java : 160 )
at org . apache . catalina . core . ContainerBase $ StartChild . call ( ContainerBase . java : 1559 )
at org . apache . catalina . core . ContainerBase $ StartChild . call ( ContainerBase . java : 1549 )
at java . util . concurrent . FutureTask . run ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor . runWorker ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor $ Worker . run ( Unknown Source )
at java . lang . Thread . run ( Unknown Source )
mar 19 , 2017 9 : 00 : 30 PM org . apache . catalina . core . StandardContext listenerStop
GRAVE : Exception sending context destroyed event to listener instance of class org . springframework . web . context . ContextLoaderListener
java . lang . IllegalStateException : BeanFactory not initialized or already closed - call ‘ refresh ’ before accessing beans via the ApplicationContext
at org . springframework . context . support . AbstractRefreshableApplicationContext . getBeanFactory ( AbstractRefreshableApplicationContext . java : 170 )
at org . springframework . context . support . AbstractApplicationContext . destroyBeans ( AbstractApplicationContext . java : 908 )
at org . springframework . context . support . AbstractApplicationContext . doClose ( AbstractApplicationContext . java : 884 )
at org . springframework . context . support . AbstractApplicationContext . close ( AbstractApplicationContext . java : 836 )
at org . springframework . web . context . ContextLoader . closeWebApplicationContext ( ContextLoader . java : 579 )
at org . springframework . web . context . ContextLoaderListener . contextDestroyed ( ContextLoaderListener . java : 115 )
at org . apache . catalina . core . StandardContext . listenerStop ( StandardContext . java : 4980 )
at org . apache . catalina . core . StandardContext . stopInternal ( StandardContext . java : 5626 )
at org . apache . catalina . util . LifecycleBase . stop ( LifecycleBase . java : 232 )
at org . apache . catalina . util . LifecycleBase . start ( LifecycleBase . java : 160 )
at org . apache . catalina . core . ContainerBase $ StartChild . call ( ContainerBase . java : 1559 )
at org . apache . catalina . core . ContainerBase $ StartChild . call ( ContainerBase . java : 1549 )
at java . util . concurrent . FutureTask . run ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor . runWorker ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor $ Worker . run ( Unknown Source )
at java . lang . Thread . run ( Unknown Source )
Daniel_Dias 20 de mar. de 2017 1 like
guilhermebhte 20 de mar. de 2017
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns : tx = "http://www.springframework.org/schema/tx"
xmlns : context = "http://www.springframework.org/schema/context"
xmlns : mvc = "http://www.springframework.org/schema/mvc"
xsi : schemaLocation = "
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
< context : annotation - config />
< bean
class =" org . springframework . orm . jpa . support . PersistenceAnnotationBeanPostProcessor " />
<bean id=" entityManagerFactory "
class=" org . springframework . orm . jpa . LocalContainerEntityManagerFactoryBean ">
<property name=" dataSource " ref=" dataSource " />
<property name=" jpaVendorAdapter ">
<bean class=" org . springframework . orm . jpa . vendor . HibernateJpaVendorAdapter ">
<property name=" showSql " value=" true " />
</bean>
</property>
<property name=" jpaPropertyMap ">
<map>
<entry key=" hibernate . dialect " value=" org . hibernate . dialect . PostgreSQLDialect " />
<entry key=" hibernate . hbm2ddl . auto " value=" none " />
<entry key=" hibernate . format_sql " value=" true " />
</map>
</property>
</bean>
<bean id=" transactionManager " class=" org . springframework . orm . jpa . JpaTransactionManager ">
<property name=" entityManagerFactory " ref=" entityManagerFactory " />
</bean>
<tx:annotation-driven transaction-manager=" transactionManager " />
<bean id=" dataSource "
class=" org . springframework . jdbc . datasource . DriverManagerDataSource ">
<property name=" driverClassName " value=" org . postgresql . Driver " />
<property name=" url " value=" jdbc : postgresql : //localhost:5432/erp_utf8" />
< property name = "username" value = "postgres" />
< property name = "password" value = "eeee" />
</ bean >
< bean id = "txManager" class =" org . springframework . orm . jpa . JpaTransactionManager ">
<property name=" entityManagerFactory " ref=" entityManagerFactory " />
</bean>
<mvc:annotation-driven conversion-service=" conversionService " />
<bean id=" conversionService "
class=" org . springframework . context . support . ConversionServiceFactoryBean ">
<property name=" converters ">
<set>
<bean class=" br . com . netsoft . desif . converter . StringCosif " />
</set>
</property>
</bean>
</beans>
Este erro
INFORMAÇÕES : Closing Spring root WebApplicationContext
INFO : org . springframework . web . context . support . XmlWebApplicationContext - Closing Root WebApplicationContext : startup date [ Sun Mar 19 21 : 14 : 44 BRT 2017 ]; root of context hierarchy
WARN : org . springframework . web . context . support . XmlWebApplicationContext - Exception thrown from ApplicationListener handling ContextClosedEvent
java . lang . IllegalStateException : ApplicationEventMulticaster not initialized - call ‘ refresh ’ before multicasting events via the context : Root WebApplicationContext : startup date [ Sun Mar 19 21 : 14 : 44 BRT 2017 ]; root of context hierarchy
at org . springframework . context . support . AbstractApplicationContext . getApplicationEventMulticaster ( AbstractApplicationContext . java : 344 )
at org . springframework . context . support . AbstractApplicationContext . publishEvent ( AbstractApplicationContext . java : 331 )
at org . springframework . context . support . AbstractApplicationContext . doClose ( AbstractApplicationContext . java : 869 )
at org . springframework . context . support . AbstractApplicationContext . close ( AbstractApplicationContext . java : 836 )
at org . springframework . web . context . ContextLoader . closeWebApplicationContext ( ContextLoader . java : 579 )
at org . springframework . web . context . ContextLoaderListener . contextDestroyed ( ContextLoaderListener . java : 115 )
at org . apache . catalina . core . StandardContext . listenerStop ( StandardContext . java : 4980 )
at org . apache . catalina . core . StandardContext . stopInternal ( StandardContext . java : 5626 )
at org . apache . catalina . util . LifecycleBase . stop ( LifecycleBase . java : 232 )
at org . apache . catalina . util . LifecycleBase . start ( LifecycleBase . java : 160 )
at org . apache . catalina . core . ContainerBase $StartChild . call ( ContainerBase . java : 1559 )
at org . apache . catalina . core . ContainerBase $StartChild . call ( ContainerBase . java : 1549 )
at java . util . concurrent . FutureTask . run ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor . runWorker ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor $Worker . run ( Unknown Source )
at java . lang . Thread . run ( Unknown Source )
WARN : org . springframework . web . context . support . XmlWebApplicationContext - Exception thrown from LifecycleProcessor on context close
java . lang . IllegalStateException : LifecycleProcessor not initialized - call ‘ refresh ’ before invoking lifecycle methods via the context : Root WebApplicationContext : startup date [ Sun Mar 19 21 : 14 : 44 BRT 2017 ]; root of context hierarchy
at org . springframework . context . support . AbstractApplicationContext . getLifecycleProcessor ( AbstractApplicationContext . java : 357 )
at org . springframework . context . support . AbstractApplicationContext . doClose ( AbstractApplicationContext . java : 877 )
at org . springframework . context . support . AbstractApplicationContext . close ( AbstractApplicationContext . java : 836 )
at org . springframework . web . context . ContextLoader . closeWebApplicationContext ( ContextLoader . java : 579 )
at org . springframework . web . context . ContextLoaderListener . contextDestroyed ( ContextLoaderListener . java : 115 )
at org . apache . catalina . core . StandardContext . listenerStop ( StandardContext . java : 4980 )
at org . apache . catalina . core . StandardContext . stopInternal ( StandardContext . java : 5626 )
at org . apache . catalina . util . LifecycleBase . stop ( LifecycleBase . java : 232 )
at org . apache . catalina . util . LifecycleBase . start ( LifecycleBase . java : 160 )
at org . apache . catalina . core . ContainerBase $StartChild . call ( ContainerBase . java : 1559 )
at org . apache . catalina . core . ContainerBase $StartChild . call ( ContainerBase . java : 1549 )
at java . util . concurrent . FutureTask . run ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor . runWorker ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor $Worker . run ( Unknown Source )
at java . lang . Thread . run ( Unknown Source )
mar 19 , 2017 9 : 14 : 45 PM org . apache . catalina . core . StandardContext listenerStop
GRAVE : Exception sending context destroyed event to listener instance of class org . springframework . web . context . ContextLoaderListener
java . lang . IllegalStateException : BeanFactory not initialized or already closed - call ‘ refresh ’ before accessing beans via the ApplicationContext
at org . springframework . context . support . AbstractRefreshableApplicationContext . getBeanFactory ( AbstractRefreshableApplicationContext . java : 170 )
at org . springframework . context . support . AbstractApplicationContext . destroyBeans ( AbstractApplicationContext . java : 908 )
at org . springframework . context . support . AbstractApplicationContext . doClose ( AbstractApplicationContext . java : 884 )
at org . springframework . context . support . AbstractApplicationContext . close ( AbstractApplicationContext . java : 836 )
at org . springframework . web . context . ContextLoader . closeWebApplicationContext ( ContextLoader . java : 579 )
at org . springframework . web . context . ContextLoaderListener . contextDestroyed ( ContextLoaderListener . java : 115 )
at org . apache . catalina . core . StandardContext . listenerStop ( StandardContext . java : 4980 )
at org . apache . catalina . core . StandardContext . stopInternal ( StandardContext . java : 5626 )
at org . apache . catalina . util . LifecycleBase . stop ( LifecycleBase . java : 232 )
at org . apache . catalina . util . LifecycleBase . start ( LifecycleBase . java : 160 )
at org . apache . catalina . core . ContainerBase $StartChild . call ( ContainerBase . java : 1559 )
at org . apache . catalina . core . ContainerBase $StartChild . call ( ContainerBase . java : 1549 )
at java . util . concurrent . FutureTask . run ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor . runWorker ( Unknown Source )
at java . util . concurrent . ThreadPoolExecutor $Worker . run ( Unknown Source )
at java . lang . Thread . run ( Unknown Source )
Daniel_Dias 20 de mar. de 2017 1 like
vê se agora vai :
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns : tx = "http://www.springframework.org/schema/tx"
xmlns : context = "http://www.springframework.org/schema/context"
xmlns : mvc = "http://www.springframework.org/schema/mvc"
xsi : schemaLocation = "
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" >
< context : annotation - config />
< bean
class =" org . springframework . orm . jpa . support . PersistenceAnnotationBeanPostProcessor " />
<bean id=" entityManagerFactory "
class=" org . springframework . orm . jpa . LocalContainerEntityManagerFactoryBean ">
<property name=" dataSource " ref=" dataSource " />
<property name=" jpaVendorAdapter ">
<bean class=" org . springframework . orm . jpa . vendor . HibernateJpaVendorAdapter ">
<property name=" showSql " value=" true " />
</bean>
</property>
<property name=" jpaPropertyMap ">
<map>
<entry key=" hibernate . dialect " value=" org . hibernate . dialect . PostgreSQLDialect " />
<entry key=" hibernate . hbm2ddl . auto " value=" none " />
<entry key=" hibernate . format_sql " value=" true " />
</map>
</property>
</bean>
<bean id=" transactionManager " class=" org . springframework . orm . jpa . JpaTransactionManager ">
<property name=" entityManagerFactory " ref=" entityManagerFactory " />
</bean>
<tx:annotation-driven transaction-manager=" transactionManager " />
<bean id=" dataSource "
class=" org . springframework . jdbc . datasource . DriverManagerDataSource ">
<property name=" driverClassName " value=" org . postgresql . Driver " />
<property name=" url " value=" jdbc : postgresql : //localhost:5432/erp_utf8" />
< property name = "username" value = "postgres" />
< property name = "password" value = "eeee" />
</ bean >
< bean id = "txManager" class =" org . springframework . orm . jpa . JpaTransactionManager ">
<property name=" entityManagerFactory " ref=" entityManagerFactory " />
</bean>
<mvc:annotation-driven conversion-service=" conversionService " />
<bean id=" conversionService "
class=" org . springframework . context . support . ConversionServiceFactoryBean ">
<property name=" converters ">
<set>
<bean class=" br . com . netsoft . desif . converter . StringCosif " />
</set>
</property>
</bean>
</beans>
guilhermebhte 20 de mar. de 2017
Funcionou.
Agora como meu controller vai saber que tem que executar este código antes ?
Daniel_Dias 20 de mar. de 2017
Quando você rodar a sua aplicação Spring ira saber automaticamente como fazer essa solicitação. Você não precisa fazer nada no seu controller.
guilhermebhte 20 de mar. de 2017
Então não funcionou, pois o erro inicial continua.
Field error in object ‘cosifEntity’ on field ‘cosif’: rejected value []; codes [typeMismatch.cosifEntity.cosif,typeMismatch.cosif,typeMismatch.br.com.netsoft.desif.model.desif.federal.CosifEntity,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [cosifEntity.cosif,cosif]; arguments []; default message [cosif]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘br.com.netsoft.desif.model.desif.federal.CosifEntity’ for property ‘cosif’; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [br.com.netsoft.desif.model.desif.federal.CosifEntity] for property ‘cosif’: no matching editors or conversion strategy found]
Daniel_Dias 20 de mar. de 2017
Eu testei aqui na minha app é o modo que lhe passei esta funcionando correta, inclusive ate alterei a parte do select do meu JSP que antes estava usando do proprio Spring e alterei para o mesmo modelo do seu.
em todo caso vou posta as classes como esta formada a minha :
Empregado.java
@Entity
public class Empregado {
@Id
@GeneratedValue ( strategy = GenerationType . IDENTITY )
private ObjectId id ;
@NotEmpty ( message = "nome é obrigatorio." )
@NotNull
private String nome ;
@NotNull
@NotEmpty ( message = "Função é obrigatorio." )
private String funcao ;
@DecimalMin ( message = "Valor minimo é 10.0" , value = "10.0" )
private double salario ;
@ManyToOne
@JoinColumn ( name = "idDepartamento" )
private Departamento departamento ;
public ObjectId getId () {
return id ;
}
public void setId ( ObjectId id ) {
this . id = id ;
}
public String getNome () {
return nome ;
}
public void setNome ( String nome ) {
this . nome = nome ;
}
public String getFuncao () {
return funcao ;
}
public void setFuncao ( String funcao ) {
this . funcao = funcao ;
}
public double getSalario () {
return salario ;
}
public void setSalario ( double salario ) {
this . salario = salario ;
}
public Departamento getDepartamento () {
return departamento ;
}
public void setDepartamento ( Departamento departamento ) {
this . departamento = departamento ;
}
}
Departamento.java
@Entity
public class Departamento {
@Id
@GeneratedValue ( strategy = GenerationType . SEQUENCE )
private int id ;
@Column ( name = "Departamento" )
private String nome ;
private String descricao ;
@OneToMany ( fetch = FetchType . LAZY , cascade = { CascadeType . ALL }, mappedBy = "departamento" )
private List < Empregado > empregados ;
public int getId () {
return id ;
}
public void setId ( int id ) {
this . id = id ;
}
public String getNome () {
return nome ;
}
public void setNome ( String nome ) {
this . nome = nome ;
}
public String getDescricao () {
return descricao ;
}
public void setDescricao ( String descricao ) {
this . descricao = descricao ;
}
public List < Empregado > getEmpregados () {
return empregados ;
}
public void setEmpregados ( List < Empregado > empregados ) {
this . empregados = empregados ;
}
}
EmpregadoController
@Controller
@RequestMapping ( "/empregado" )
public class EmpregadoController {
@Autowired
private EmpregadoServico empregadoServico ;
@Autowired
private DepartamentoServico departamentoServico ;
@GetMapping ( "/inserirEmpregado" )
public ModelAndView paginaEmpregado () {
ModelAndView modelAndView = new ModelAndView ( "empregados/inserirEmpregado" );
modelAndView . addObject ( "empregado" , new Empregado ());
try {
modelAndView . addObject ( "listaDepartamento" , departamentoServico . listarDepartamentos ());
modelAndView . addObject ( "listaEmpregados" , empregadoServico . listarEmpregados ());
} catch ( Exception e ) {
e . printStackTrace ();
}
return modelAndView ;
}
@PostMapping ( "/inserirEmpregado" )
public ModelAndView cadastrarEmpregado ( @ModelAttribute ( "empregado" ) @Valid Empregado empregado ,
BindingResult bindingResult ) throws Exception {
ModelAndView modelAndView = new ModelAndView ( "empregados/inserirEmpregado" );
if ( bindingResult . hasErrors ()) {
return modelAndView ;
} else {
empregadoServico . adicionaEmpregado ( empregado );
}
modelAndView . addObject ( "listaDepartamento" , departamentoServico . listarDepartamentos ());
modelAndView . setViewName ( "redirect:/empregado/listarEmpregado" );
return modelAndView ;
}
@GetMapping ( "/alterarEmpregado/{id}" )
public ModelAndView editarEmpregado ( @PathVariable ( "id" ) ObjectId id ) throws Exception {
ModelAndView modelAndView = new ModelAndView ( "/empregados/editarEmpregado" );
modelAndView . addObject ( "listaDepartamento" , departamentoServico . listarDepartamentos ());
modelAndView . addObject ( "empregado" , empregadoServico . getEmpregadoPorId ( id ));
return modelAndView ;
}
@PostMapping ( "/alterarEmpregado" )
public ModelAndView editarEmpregado ( @ModelAttribute ( "empregado" ) Empregado empregado ) throws Exception {
empregadoServico . atualizaEmpregado ( empregado );
return new ModelAndView ( "redirect:/empregado/listarEmpregado" );
}
@GetMapping ( "/listarEmpregado" )
public ModelAndView listarEmpregado () throws Exception {
return new ModelAndView ( "empregados/listarEmpregados" ,
"empregados" , empregadoServico . listarEmpregados ());
}
@GetMapping ( "/remover/{id}" )
public ModelAndView excluirEmpregado ( @PathVariable ( "id" ) ObjectId id ) throws Exception {
empregadoServico . excluirEmpregado ( id );
return new ModelAndView ( "redirect:/empregado/listarEmpregado" );
}
}
inserirEmpregado.jsp
<% @page contentType = "text/html" language = "java" pageEncoding = "UTF-8" %>
<% @taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<% @ taglib prefix = "spring" uri = "http://www.springframework.org/tags" %>
<% @taglib uri = "http://www.springframework.org/tags/form" prefix = "form" %>
<!DOCTYPE html>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
<!-- <link href="@/../recursos/css/estilo.css" rel="stylesheet" type="text/css"/> -->
< link href = "<c:url value='/recursos/css/estilo.css'/> " rel = "stylesheet" type = "text/css" >
< link href = "<c:url value='/recursos/css/ozark.css'/> " rel = "stylesheet" type = "text/css" >
< title > Cadastrar Empregado</ title >
</ head >
< body >
< fieldset style = "width: 400px" >
< legend > Dados do Empregado</ legend >
< form:form method = "POST" action = "${pageContext.request.contextPath}/empregado/inserirEmpregado" modelAttribute = "empregado" >
< form:errors path = "*" cssClass = "errorblock" element = "div" />
< table >
< tr >
< td >< form:label path = "nome" > Nome:</ form:label ></ td >
< td >< form:input path = "nome" /></ td >
< td >< form:errors path = "nome" cssClass = "error" /></ td >
</ tr >
< tr >
< td >< form:label path = "funcao" > Função:</ form:label ></ td >
< td >< form:input path = "funcao" /></ td >
< td >< form:errors path = "funcao" cssClass = "error" /></ td >
</ tr >
< tr >
< td >< form:label path = "salario" > Salário:</ form:label ></ td >
< td >< form:input path = "salario" /></ td >
< td >< form:errors path = "salario" cssClass = "error" /></ td >
</ tr >
< tr >
< td >
<%- - < form : select path = "departamento" >
< form : option value = "0" label = "--Select--" />
<form:options items="${listaDepartamento}" itemValue="id" itemLabel="nome"/ >
< /form:select> - -%>
< select id = "departamento" name = "departamento" >
< option value = "" ></ option >
< c:forEach var = "lista" items = "${listaDepartamento}" >
< option value = "${lista.id}" >
${lista.nome}
</ option >
</ c:forEach >
</ select >
</ td >
</ tr >
< tr >
< td colspan = "2" >< input type = "submit" value = "Cadastrar Empregado" /></ td >
</ tr >
</ table >
</ form:form >
</ fieldset >
</ body >
</ html >