Spring MVC annotations not working on Maven

Asked

Viewed 728 times

0

Hi, I’m having a problem setting up Spring MVC in the Maven, when I try to access the http://localhost:8080/spring/olaMundoSpring it returns in browser:

STATUS 404 The requested Resource is not available.

AND THE WARNING:

No Mapping found for HTTP request with URI [/spring/olaMundoSpring] in Dispatcherservlet with name 'Spring MVC Dispatcher Servlet'.

The context is okay:

Sep 05, 2016 9:45:24 pm org.springframework.Beans.factory.xml.Xmlbeandefinitionreader reader loadBeanDefinitions INFORMATION: Loading XML bean Definitions from Servletcontext Resource [/WEB-INF/spring-context.xml]

Controller:

package br.com.jayybe.spring;

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

@Controller
public class Controle {

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

Diretório IDE Eclipse

web xml.:

<web-app>
  <display-name>Archetype Created Web Application</display-name>

<servlet>
      <servlet-name>Spring MVC Dispatcher Servlet</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>Spring MVC Dispatcher Servlet</servlet-name>
      <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

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.jayybe.spring" />
  <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>

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jayybe</groupId>
  <artifactId>spring</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
    </dependency>
    <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>4.2.4.RELEASE</version>
              </dependency>

              <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-webmvc</artifactId>
                    <version>4.2.4.RELEASE</version>
              </dependency>
              <!-- Servlet -->
              <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                    <version>3.1.0</version>
              </dependency>
              <dependency>
                    <groupId>javax.servlet.jsp</groupId>
                    <artifactId>jsp-api</artifactId>
                    <version>2.1</version>
                    <scope>provided</scope>
              </dependency>
              <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>jstl</artifactId>
                    <version>1.2</version>
              </dependency>
  </dependencies>
  <build>
    <finalName>spring</finalName>
  </build>
</project>

1 answer

0

You need to create the Spring configuration class

@Configuration
@ComponentScan(basePackageClasses = { Controle.class })
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

}

There you will need to import some methods and you will need to configure them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.