我成功地將我的 web 應用程式部署到 Tomcat 本地主機。我的應用程式有 2 個頁面:localhost:8080/spring-mvc 和 localhost:8080/home。第一頁有第二頁的連結。當我啟動我的應用程式時,它成功打開第一頁,但是當我點擊連結進入第二頁時,出現錯誤:
HTTP 狀態 404 – 找不到。
似乎無法存取 WEB-INF/view 資料夾。你能幫我解決這個問題嗎?
網路.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>
應用程式上下文.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>
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;
}
}
答案1
根本原因 tomcat jasper 套件中提供了解析 JSP 路徑所需的類,並且 pom.xml 中未添加依賴項。因此,Spring Boot應用程式無法解析jsp路徑。
解決方案 依賴類別在tomcat jasper中可用。
這個連結可能有用 http://www.codersdesks.com/spring-boot-path-with-web-inf-or-meta-inf/