我正在使用Java 8和Spring 5.1.5。我对此并不陌生,因此尝试遵循这些准则,但有一个问题似乎是跳过了DispatcherServlet和/或Controller映射,我在Tomcat中收到以下错误:
原始服务器找不到目标资源的当前表示,或者不愿意透露其存在。
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<description>
Configuration file for the Store Application
</description>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Store-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>Store</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Store</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我的Store-servlet.xml
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-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.store.controller" />
<context:annotation-config/>
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp" />
<property name = "suffix" value = ".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/"/>
</beans>
我的控制器
package com.store.controller;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class CarMartController {
private transient Logger log = Logger.getLogger( "CarMartController" );
@GetMapping("/store/carMart")
public String navigateToCarMart( ModelMap model) {
log.info("CarMartController [navigatetoCarMart");
return "carMart";
}
}
我的CarMart.jsp和标题
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/resources/css/styles.css" />
<title>Car Mart</title>
</head>
<body style="background-image:url(/resources/images/carBackground.jpg);background-size:cover">
<jsp:include page="./header.jsp" />
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<div class="navbar">
<a href="<c:url value='/customer/customerDetail.jsp' />" >Customers</a>
<a href="<c:url value='/car/carDetail.jsp' />" >Cars</a>
</div>
我拨打的网址是http://localhost:8088/store/carMart.jsp
资源目录位于war文件的webapps / store目录中
jsp目录位于WEB-INF目录中
答案 0 :(得分:0)
首先,为控制器类添加@RequestMapping
:
@Controller
@RequestMapping("/store")
public class CarMartController {
@GetMapping("/carMart")
public String navigateToCarMart( ModelMap model) {
...
}
}
并尝试使用http://localhost:8088/store/carMart
访问您的端点
最后没有 .jsp 。
答案 1 :(得分:0)
有关RequestMapping和添加
的评论<mvc:annotation-driven/>
解决了问题