我已成功将我的 webapp 部署到 Tomcat localhost。我的应用程序有 2 个页面:localhost:8080/spring-mvc 和 localhost:8080/home。第一页有指向第二页的链接。当我启动我的应用程序时,它成功打开了第一页,但当我按照链接转到第二页时,出现了错误:
HTTP 状态 404 – 未找到。
似乎无法访问 WEB-INF/view 文件夹。您能帮我解决这个问题吗?
网页文件
<?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>
应用程序上下文.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>
调度程序-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>
索引.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>
主页.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>${pageTitle}</title>
</head>
<body>
${message}
</body>
</html>
主页控制器.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;
}
}
答案1
根本原因 tomcat jasper 包中提供了解析 JSP 路径所需的类,但 pom.xml 中未添加依赖项。因此,spring boot 应用程序无法解析 jsp 路径。
解决方案依赖的类在tomcat jasper中可用。
此链接可能有用 http://www.codersdesks.com/spring-boot-path-with-web-inf-or-meta-inf/