Spring

3 respostas
peppe

Boa noite,

Estou iniciando o estudo no spring framework e estou acompanhando uma configuração básica através do artigo no link [url]http://www.edsongoncalves.com.br/2010/02/27/spring-mvc-3-0-na-pratica-parte-1/[/url].

Baixei o spring framework do site [url]http://www.springsource.org/download[/url] versão spring-framework-3.0.2.RELEASE

Crieu no eclipse um novo projeto do tipo Dynamic Web Project e segui o artigo.

Adicionei ao projetos os jars listados abaixo, criei um novo diretório dentro do projeto, o renomeando para lib e copiei e colei os jars dentro desse diretório, arrastando posteriormente para a pasta lib do WEB-INF.


org.springframework.asm-sources-3.0.2.RELEASE.jar
org.springframework.asm-3.0.2.RELEASE.jar
org.springframework.beans-3.0.2.RELEASE.jar
org.springframework.context-3.0.2.RELEASE.jar
org.springframework.context.support-3.0.2.RELEASE.jar
org.springframework.core-3.0.2.RELEASE.jar
org.springframework.expression-3.0.2.RELEASE.jar
org.springframework.web-3.0.2.RELEASE.jar
org.springframework.web.servlet-3.0.2.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.commons.logging-sources-1.1.1.jar

Posteriormente, configurei o arquivo web.xml da seguinte forma:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>ProjetoSpring</display-name>

	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>


</web-app>

Criei o arquivo springmvc-servlet.xml da seguinte forma:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="br.com.integrator.web"/>
		<bean
			class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/jsp/" />
			<property name="suffix" value=".jsp" />
		</bean>

</beans>

Depois de configurado os arquivos xml, criei a classe que serve como controller da seguinte forma:

package br.projetospring.olamundo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class OlaMeuController {

	@RequestMapping("/ola")
	public ModelAndView ola() {
		String menssagem = "Conhecendo o Spring Framework";
		ModelAndView modelandview = new ModelAndView("ola");
		modelandview.addObject("menssagem", menssagem);
		return modelandview;
	}

}

Startei o servidor tomcat 6 e tentei acessar a pagina através do browser por meio da url [url]http://localhost:8080/ProjetoSpring/ola[/url], que retorna a seguinte saída no browser:

HTTP Status 404 -

--------------------------------------------------------------------------------

type Status report

message

description The requested resource () is not available.

--------------------------------------------------------------------------------

Apache Tomcat/6.0.26

O console do eclipse exibe as seguintes mensagens:

06/04/2010 20:15:46 org.apache.catalina.core.AprLifecycleListener init INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre6\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\MySQL\MySQL Server 5.1\bin 06/04/2010 20:15:46 org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:ProjetoSpring' did not find a matching property. 06/04/2010 20:15:46 org.apache.coyote.http11.Http11Protocol init INFO: Initializing Coyote HTTP/1.1 on http-8080 06/04/2010 20:15:46 org.apache.catalina.startup.Catalina load INFO: Initialization processed in 572 ms 06/04/2010 20:15:46 org.apache.catalina.core.StandardService start INFO: Starting service Catalina 06/04/2010 20:15:46 org.apache.catalina.core.StandardEngine start INFO: Starting Servlet Engine: Apache Tomcat/6.0.26 06/04/2010 20:15:47 org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring FrameworkServlet 'springmvc' 06/04/2010 20:15:47 org.springframework.web.servlet.FrameworkServlet initServletBean INFO: FrameworkServlet 'springmvc': initialization started 06/04/2010 20:15:47 org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing WebApplicationContext for namespace 'springmvc-servlet': startup date [Tue Apr 06 20:15:47 BRT 2010]; root of context hierarchy 06/04/2010 20:15:47 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/springmvc-servlet.xml] 06/04/2010 20:15:47 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@193f6e2: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0]; root of factory hierarchy 06/04/2010 20:15:48 org.springframework.web.servlet.FrameworkServlet initServletBean INFO: FrameworkServlet 'springmvc': initialization completed in 827 ms 06/04/2010 20:15:48 org.apache.coyote.http11.Http11Protocol start INFO: Starting Coyote HTTP/1.1 on http-8080 06/04/2010 20:15:48 org.apache.jk.common.ChannelSocket init INFO: JK: ajp13 listening on /0.0.0.0:8009 06/04/2010 20:15:48 org.apache.jk.server.JkMain start INFO: Jk running ID=0 time=0/32 config=null 06/04/2010 20:15:48 org.apache.catalina.startup.Catalina start INFO: Server startup in 1423 ms 06/04/2010 20:16:00 org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping found for HTTP request with URI [/ProjetoSpring/ola] in DispatcherServlet with name 'springmvc' 06/04/2010 20:16:04 org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping found for HTTP request with URI [/ProjetoSpring/ola] in DispatcherServlet with name 'springmvc'

Ao que entendi, ele encontra problemas no mapeamento da classe DispatcherServlet, que parece ser acusado no console ao ser carregado o servidor:

org.springframework.web.servlet.DispatcherServlet noHandlerFound

E não encontra a pagina :

No mapping found for HTTP request with URI [/ProjetoSpring/ola] in DispatcherServlet with name 'springmvc'

Estou certo?
Alguém saberia me orientar algum caminho para seguir e encontrar o problema, já que aparentemente segui o artigo apenas tomando alguns outros caminho, mas que acredito não causarem problemas?

Agradeço

3 Respostas

skalinichenko

Onde está o mapeamento ola?
Olhei seus arquivos (web.xml e springmvc-servlet.xml ) e e não o encontrei.
Flw!

peppe

Então,

Eu acompanhei o artigo que citei, lá não fala nada sobre mapear.

Tem alguma dica?

peppe

Bom,

Encontrei o problema, minha classe estava no package abaixo:

E no arquivo springmvc-servlet.xml, estava definindo a linha abaixo acompanhando o artigo:

<?xml version="1.0" encoding="UTF-8"?>

[size=16][color=red]<context:component-scan base-package="br.com.integrator.web"/>[/color][/size]
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

Sendo que o correto seria:

<?xml version="1.0" encoding="UTF-8"?>

[size=16][color=red]<context:component-scan base-package="br.projetospring.olamundo"/>[/color][/size]
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

Obrigado

Criado 6 de abril de 2010
Ultima resposta 6 de abr. de 2010
Respostas 3
Participantes 2