为什么不在 Spring web MVC 中调用 Controller

Why isn't Controller being called in Spring web MVC

我正在开发一个新的 Spring 网络 MVC。对我的第一页的调用是 returning 404。我主要是从一个更复杂的项目中复制这段代码。我看不出有任何理由不应该 return 预期的页面。我找到了另外两篇讨论 context:component-scan 的文章,但我已经检查了好几次。我可以在启动日志中看到控制器正在映射,但是对默认页面的任何调用都是 returning 404。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>inventory-scan</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/dispatcher-servlet.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <session-config>
        <session-timeout>0</session-timeout>
        <cookie-config>
            <http-only>true</http-only>
        </cookie-config>
    </session-config>
    <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>

调度员-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    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
                                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                                     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.kable.product.web.inventory_scan" />
    <beans:bean id="dsOP" class="org.springframework.jndi.JndiObjectFactoryBean">
        <beans:property name="jndiName" value="java:comp/env/jdbc/DBAS400"/>
        <beans:property name="resourceRef" value="true" />
    </beans:bean>

    <beans:bean id="tilesViewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <beans:property name="viewClass">
            <beans:value>org.springframework.web.servlet.view.tiles3.TilesView
            </beans:value>
        </beans:property>
        <beans:property name="order">
            <beans:value>1</beans:value>
        </beans:property>
    </beans:bean>

    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix">
            <beans:value>/WEB-INF/views/</beans:value>
        </beans:property>
        <beans:property name="suffix">
            <beans:value>.jsp</beans:value>
        </beans:property>
        <beans:property name="order">
            <beans:value>#{tilesViewResolver.order+1}</beans:value>
        </beans:property>
    </beans:bean>

    <beans:bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <beans:property name="definitions">
            <beans:list>
                <beans:value>/WEB-INF/tiles-defs.xml</beans:value>
            </beans:list>
        </beans:property>
    </beans:bean>

    <beans:bean id="sfOP"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dsOP" />
        <beans:property name="annotatedClasses">
            <beans:list>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <tx:annotation-driven transaction-manager="tmOP" />
    <beans:bean id="tmOP"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sfOP" />
    </beans:bean>

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <beans:bean class="org.springframework.security.web.method.annotation.CsrfTokenArgumentResolver"/>
        </mvc:argument-resolvers>
    </mvc:annotation-driven>
</beans:beans>

方块-defs.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> 
<tiles-definitions>
    <definition name="main_layout" template="/WEB-INF/views/layout.jsp">
        <put-attribute name="title" value="/WEB-INF/views/empty.jsp" />
        <put-attribute name="head" value="/WEB-INF/views/head.jsp" />
        <put-attribute name="header" value="/WEB-INF/views/header.jsp" />
        <put-attribute name="menu" value="/WEB-INF/views/empty.jsp" />
        <put-attribute name="rightPanel" value="/WEB-INF/views/empty.jsp" />
        <put-attribute name="body" value="/WEB-INF/views/empty.jsp" />
        <put-attribute name="footer" value="/WEB-INF/views/empty.jsp" />
    </definition>

    <definition extends="main_layout" name="welcome">
        <put-attribute name="title" value="Inventory Scan" />
        <put-attribute name="pagename" value="pageWelcome" />
        <put-attribute name="menu" value="/WEB-INF/views/menuwelcome.jsp" />
        <put-attribute name="body" value="/WEB-INF/views/welcome.jsp" />
    </definition>

</tiles-definitions>

控制器

package com.kable.product.web.inventory_scan.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class InventoryScanController {

    @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
    public String welcome() {
        return "welcome";
    }
}

catalina 日志中的控制器映射

09:02:19,439 [http-apr-8080-exec-94] INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping- Mapped "{[ || /],methods=[GET]}" onto public java.lang.String com.kable.product.web.inventory_scan.controller.InventoryScanController.welcome()

所以我可以看到 tomcat 映射了方法,但是当我尝试导航到 http://localhost:8080/inventory-scan/ 时,出现 404 错误。

localhost_access_log

127.0.0.1 - - [11/Jul/2020:09:05:49 -0500] "GET /inventory-scan/ HTTP/1.1" 404 -

我将 @RequestMapping 更改为 @RequestMapping(value = {"/test"}, method = RequestMethod.GET)。这导致我进入默认页面加载,但 /test/ 页面会 return 404。这导致我深入研究 tiles,我发现我丢失了 layout.jsp。把这个文件放进去之后,一切似乎都正常了。