Implantei com sucesso meu webapp no host local do Tomcat. Meu aplicativo tem 2 páginas: localhost:8080/spring-mvc e localhost:8080/home. A primeira página contém um link para a segunda. Quando inicio meu aplicativo, ele abre a primeira página com sucesso, mas quando sigo o link para a segunda página, aparece o erro:
Status HTTP 404 – Não encontrado.
Parece que não há acesso à pasta WEB-INF/view. Você pode me ajudar a resolver esse problema?
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
aplicaçãoContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
despachante-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/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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.stoliarenko"/>
<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>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Index Page</title>
</head>
<body>
<a href="/home">Go To Home Page!!</a>
</body>
</html>
home.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>${pageTitle}</title>
</head>
<body>
${message}
</body>
</html>
HomeController.java
package com.stoliarenko;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@RequestMapping(value = "/home")
public ModelAndView home(){
ModelAndView modelAndView = new ModelAndView("home");
modelAndView.addObject("pageTitle", "Home Page");
modelAndView.addObject("message", "Hi, Welcome! This is HomePage!");
return modelAndView;
}
}
Responder1
Causa raiz A classe necessária para resolver o caminho JSP está disponível no pacote tomcat jasper e a dependência não é adicionada em pom.xml. Conseqüentemente, o aplicativo Spring Boot não é capaz de resolver o caminho jsp.
Solução As classes dependentes estão disponíveis no Tomcat Jasper.
este link pode ser útil http://www.codersdesks.com/spring-boot-path-with-web-inf-or-meta-inf/