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

Dúvida no Ex. 4 da Aula 1 - Primeiros passos com Spring MVC

Ao tentar abrir o link gera erro 404, e no console:

mar 15, 2016 4:16:24 PM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: Initializing Spring FrameworkServlet 'spring mvc'
16:16:24,827  INFO DispatcherServlet:479 - FrameworkServlet 'spring mvc': initialization started
16:16:24,889  INFO XmlWebApplicationContext:513 - Refreshing WebApplicationContext for namespace 'spring mvc-servlet': startup date [Tue Mar 15 16:16:24 BRT 2016]; root of context hierarchy
16:16:24,984  INFO XmlBeanDefinitionReader:316 - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-context.xml]
16:16:26,121  INFO RequestMappingHandlerMapping:197 - Mapped "{[/tabelas],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String br.com.caelum.contas.controller.InfraController.criaBanco() throws java.sql.SQLException
16:16:26,122  INFO RequestMappingHandlerMapping:197 - Mapped "{[/OlaMundoSpring],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String br.com.caelum.contas.controller.OlaMundoController.execute()
16:16:26,705  INFO DispatcherServlet:498 - FrameworkServlet 'spring mvc': initialization completed in 1874 ms
16:16:26,735  WARN PageNotFound:1110 - No mapping found for HTTP request with URI [/contas/] in DispatcherServlet with name 'spring mvc'
16:18:11,554  WARN PageNotFound:1110 - No mapping found for HTTP request with URI [/contas/olaMundoSpring] in DispatcherServlet with name 'spring mvc'
5 respostas

NINGUÉM???

=> 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>contas</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>

    <!-- Declaracao do servlet do Spring MVC abaixo-->
    <servlet>
        <servlet-name>spring mvc</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>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

=> spring-context:

<?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.contas" />
    <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>

=> OlaMundoController.java:

package br.com.caelum.contas.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";
    }
}

=> ok.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Olá mundo com Spring MVC</title>
    </head>
        <body>
            <h2>Olá mundo com Spring MVC</h2>            
        </body>
</html>

Fica gerando o erro:

09:35:58,297 WARN PageNotFound:1110 - No mapping found for HTTP request with URI [/contas/olaMundoSpring] in DispatcherServlet with name 'mvc-dispatcher'

solução!

Oi Ewerton

No seu request mapping voce fez /OlaMundoSpring, com O com letra maiuscula. Na exception voce acessou /olaMundoSpring com O com letra minuscula. Tente acessa com O maiusculo para ver.

Foi isso mesmo Paulo... Obrigado!!