在过去的几天里,我查看了有关配置Spring问题的stackoverflow文章,但是没有成功。
当我请求我的URL“/ republicanos-web / main / index”时,Dispatcher Servlet解析我对我的控制器的请求,应该是。在Controller上返回我的函数再次调用调度程序servlet,为我返回404,因为没有映射,只有HTML文件。
大部分文章都讨论了Dispatcher Servlet上的参数url-pattern,警告使用'/'而不是'/ *',但在我的代码中url-pattern是正确的:'/'。看我的代码:
的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";
}
}
我的HTML文件在webapp / WEB-INF / templates / home.html上定义
在恢复时,我的servlet总是试图为我的视图找到一个控制器,即使它是由控制器返回的。控制台日志的一个示例是:
找不到带有URI [/ republicanos-web / home]的HTTP请求的映射 DispatcherServlet,名称为'republicanos'
编辑: 顺便说一句,服务器是tomcat。 有人可以帮忙吗?