11.6 - Exercícios: Configurando o Spring MVC

3 respostas
muehlner

Estou desenvolvendo o projeto da apostila da caelum que usa Spring

Ao chegar no exercício 11.6 apareceu erro 404

alguém sabe as possíveis causas do erro ?

obs: Estou usando Tomcat e coloquei todos os “jar” do spring no Build Path (O tomcat não imprime nada no log, e ao menos chega a entra no método em debug)

spring-context.xml

<?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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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.caelum.tarefas" />
	<mvc:annotation-driven />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

web.xml

<?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>fj21-tarefas</display-name>
	<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>

	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
	/WEB-INF/spring-context.xml
	</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

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

</web-app>

Classe OlaMundoController

package br.com.caelum.tarefas.controller;

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

@Controller
public class OlaMundoController {

	@RequestMapping("/olaMundoSpring")
	public String execute() {
		System.out.println("Executando a lógica com Spring MVC");
		return "ok";
	}
}

pagina ok.jsp (Diretorio /WEB-INF/views/)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h2>Olá mundo com Spring MVC!</h2>
</body>
</html>

3 Respostas

muehlner

Bom dia pessoal

Alguma ajuda por favor ? :smiley:

ERVER

Boa tarde muehlner,

Olhando por cima, parece que tá tudo certo com as suas configurações. Pode ser algo que demorei muito pra descobrir quando fiz essa apostila (e também dava 404).

Tente o seguinte: na declaração do beans xmlns, os .xsd tão com a versão 3.0

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

Veja qual versão do spring você tá usando (se for a 3.0, tá correto, se for a 3.1 pra frente terá que alterar):

<?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"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/mvc  
http://www.springframework.org/schema/mvc/spring-mvc-3.X.xsd  
http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.X.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.X.xsd">  
    <context:component-scan base-package="br.com.caelum.tarefas" />  
    <mvc:annotation-driven />  
    <bean  
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/views/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  
</beans>

Substitua os X pela versão do seu spring.

Testa ai, se não funfar, avisa que tento dar uma analisada melhor.

Abraços.

muehlner

$ERVER boa Tarde !

Desde ja agradeço pela ajuda…muito obrigado

bom, fiz alguns alterações como você disse e mesmo assim continua o erro, a versão que eu estou usando é 3.0.5

você teria como me mandar um arquivo .zip com todos os “.jar” do projeto nele ?

se puder ! [email removido]

mais uma vez muito obrigado

Criado 25 de setembro de 2012
Ultima resposta 26 de set. de 2012
Respostas 3
Participantes 2