For the last couple of days, I checked out articles on stackoverflow for my problem about configuration Spring unsuccessfully.
When I request my url "/republicanos-web/main/index" the Dispatcher Servlet resolve my request for my controller, as should be. The return of my function on Controller call the dispatcher servlet again, returning 404 for me, because there's no mapping, only the HTML file.
Mostly of the articles was talking about the parameter url-pattern on Dispatcher Servlet, alerting to use '/', instead of '/*', but in my code the url-pattern is correct: '/'. See my code:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>republicanos</display-name>
<servlet>
<servlet-name>republicanos</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>republicanos</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/republicanos-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
republicanos-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.wearedevelopers.*" />
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="characterEncoding" value="UTF-8" />
<property name="cacheable" value="false" />
</bean>
MainController.java
package com.wearedevelopers.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/main")
public class MainController {
@RequestMapping("/index")
public String paginaInicial(Model model){
model.addAttribute("mensagem", "Mensagem");
return "/home";
}
}
My HTML file is defined on webapp/WEB-INF/templates/home.html
On resume, my servlet is always trying to find a controller for my views, even if its returned by controller. A example on console log is:
No mapping found for HTTP request with URI [/republicanos-web/home] in DispatcherServlet with name 'republicanos'
Edit: By the way, the server is tomcat. Anyone could help?